Passed
Pull Request — master (#1038)
by René
04:15
created

ShareService::createPersonalShare()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 23
c 3
b 0
f 1
dl 0
loc 36
ccs 0
cts 26
cp 0
rs 9.552
cc 4
nc 4
nop 2
crap 20

1 Method

Rating   Name   Duplication   Size   Complexity  
A ShareService::setEmailAddress() 0 10 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Service;
25
26
use Exception;
27
use OCA\Polls\Exceptions\NotAuthorizedException;
28
use OCA\Polls\Exceptions\InvalidUsername;
29
use OCA\Polls\Exceptions\InvalidShareType;
30
31
use OCP\Security\ISecureRandom;
32
33
use OCA\Polls\Controller\SystemController;
34
use OCA\Polls\Db\ShareMapper;
35
use OCA\Polls\Db\Share;
36
use OCA\Polls\Service\MailService;
37
use OCA\Polls\Model\Acl;
38
39
class ShareService {
40
41
	/** @var SystemController */
42
	private $systemController;
43
44
	/** @var ShareMapper */
45
	private $shareMapper;
46
47
	/** @var Share */
48
	private $share;
49
50
	/** @var MailService */
51
	private $mailService;
52
53
	/** @var Acl */
54
	private $acl;
55
56
	/**
57
	 * ShareController constructor.
58
	 * @param SystemController $systemController
59
	 * @param ShareMapper $shareMapper
60
	 * @param Share $share
61
	 * @param MailService $mailService
62
	 * @param Acl $acl
63
	 */
64
	public function __construct(
65
		SystemController $systemController,
66
		ShareMapper $shareMapper,
67
		Share $share,
68
		MailService $mailService,
69
		Acl $acl
70
	) {
71
		$this->systemController = $systemController;
72
		$this->shareMapper = $shareMapper;
73
		$this->share = $share;
74
		$this->mailService = $mailService;
75
		$this->acl = $acl;
76
	}
77
78
	/**
79
	 * Read all shares of a poll based on the poll id and return list as array
80
	 * @NoAdminRequired
81
	 * @param int $pollId
82
	 * @return array array of Share
83
	 * @throws NotAuthorizedException
84
	 */
85
	public function list($pollId, $token) {
86
		if ($token) {
87
			return array($this->get($token));
88
		}
89
90
		if (!$this->acl->set($pollId)->getAllowEdit()) {
91
			throw new NotAuthorizedException;
92
		}
93
94
		return $this->shareMapper->findByPoll($pollId);
95
	}
96
97
	/**
98
	 * Get share by token
99
	 * @NoAdminRequired
100
	 * @param string $token
101
	 * @return Share
102
	 */
103
	public function get($token) {
104
		return $this->shareMapper->findByToken($token);
105
	}
106
107
	/**
108
	 * Add share
109
	 * @NoAdminRequired
110
	 * @param int $pollId
111
	 * @param string $type
112
	 * @param string $userId
113
	 * @param string $userEmail
114
	 * @return Share
115
	 * @throws NotAuthorizedException
116
	 */
117
	public function add($pollId, $type, $userId, $userEmail = '') {
118
119
		if (!$this->acl->set($pollId)->getAllowEdit()) {
120
			throw new NotAuthorizedException;
121
		}
122
123
		if ($type === 'contact') {
124
			$type = 'external';
125
		}
126
127
		$this->share = new Share();
128
		$this->share->setType($type);
129
		$this->share->setPollId($pollId);
130
		$this->share->setUserId($userId);
131
		$this->share->setUserEmail($userEmail);
132
		$this->share->setInvitationSent(0);
133
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
134
			16,
135
			ISecureRandom::CHAR_DIGITS .
136
			ISecureRandom::CHAR_LOWER .
137
			ISecureRandom::CHAR_UPPER
138
		));
139
140
		return $this->shareMapper->insert($this->share);
141
	}
142
143
	/**
144
	 * Set emailAddress to personal share
145
	 * or update an email share with the username
146
	 * @NoAdminRequired
147
	 * @param string $token
148
	 * @param string $emailAddress
149
	 * @return Share
150
	 * @throws NotAuthorizedException
151
	 */
152
	public function setEmailAddress($token, $emailAddress) {
153
154
		$this->share = $this->shareMapper->findByToken($token);
155
		if ($this->share->getType() === 'external') {
156
			// TODO: Simple validate email address
157
			$this->share->setUserEmail($emailAddress);
158
			// TODO: Send confirmation
159
			return $this->shareMapper->update($this->share);
160
		} else {
161
			throw new InvalidShareType('Email address can only be set in external shares.');
162
		}
163
	}
164
165
	/**
166
	 * Create a personal share from a public share
167
	 * or update an email share with the username
168
	 * @NoAdminRequired
169
	 * @param string $token
170
	 * @param string $userName
171
	 * @return Share
172
	 * @throws NotAuthorizedException
173
	 * @throws InvalidUsername
174
	 */
175
	public function personal($token, $userName, $emailAddress) {
176
		$this->share = $this->shareMapper->findByToken($token);
177
178
		// Return of validatePublicUsername is a DataResponse
179
		$checkUsername = $this->systemController->validatePublicUsername($this->share->getPollId(), $userName, $token);
180
181
		// if status is not 200, return DataResponse from validatePublicUsername
182
		if ($checkUsername->getStatus() !== 200) {
183
			throw new InvalidUsername;
184
		}
185
186
		if ($this->share->getType() === 'public') {
187
188
			$pollId = $this->share->getPollId();
189
			$this->share = new Share();
190
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
191
				16,
192
				ISecureRandom::CHAR_DIGITS .
193
				ISecureRandom::CHAR_LOWER .
194
				ISecureRandom::CHAR_UPPER
195
			));
196
			$this->share->setType('external');
197
			$this->share->setPollId($pollId);
198
			$this->share->setUserId($userName);
199
			$this->share->setUserEmail($emailAddress);
200
			$this->share->setInvitationSent(time());
201
			$this->shareMapper->insert($this->share);
202
			$this->mailService->sendInvitationMail($this->share->getToken());
203
			return $this->share;
204
205
		} elseif ($this->share->getType() === 'email') {
206
207
			$this->share->setType('external');
208
			$this->share->setUserId($userName);
209
			$this->share->setUserEmail($emailAddress);
210
			return $this->shareMapper->update($this->share);
211
212
		} else {
213
			throw new NotAuthorizedException;
214
		}
215
	}
216
217
	/**
218
	 * Delete share
219
	 * remove share
220
	 * @NoAdminRequired
221
	 * @param string $token
222
	 * @return Share
223
	 * @throws NotAuthorizedException
224
	 */
225
226
	public function delete($token) {
227
		$this->share = $this->shareMapper->findByToken($token);
228
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
229
			throw new NotAuthorizedException;
230
		}
231
232
		$this->shareMapper->delete($this->share);
233
234
		return $this->share;
235
	}
236
}
237