Passed
Pull Request — master (#1128)
by René
08:09 queued 03:52
created

ShareService::setEmailAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
rs 10
ccs 0
cts 9
cp 0
cc 2
nc 2
nop 2
crap 6
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 OCA\Polls\Exceptions\NotAuthorizedException;
27
use OCA\Polls\Exceptions\InvalidShareType;
28
29
use OCP\Security\ISecureRandom;
30
31
use OCA\Polls\Db\ShareMapper;
32
use OCA\Polls\Db\Share;
33
use OCA\Polls\Model\Acl;
34
use OCA\Polls\Model\UserGroupClass;
35
use OCA\Polls\Model\Circle;
36
use OCA\Polls\Model\Contact;
37
use OCA\Polls\Model\ContactGroup;
38
use OCA\Polls\Model\Email;
39
use OCA\Polls\Model\Group;
40
use OCA\Polls\Model\User;
41
42
class ShareService {
43
44
	/** @var SystemService */
45
	private $systemService;
46
47
	/** @var ShareMapper */
48
	private $shareMapper;
49
50
	/** @var Share */
51
	private $share;
52
53
	/** @var MailService */
54
	private $mailService;
55
56
	/** @var Acl */
57
	private $acl;
58
59
	/**
60
	 * ShareController constructor.
61
	 * @param SystemService $systemService
62
	 * @param ShareMapper $shareMapper
63
	 * @param Share $share
64
	 * @param MailService $mailService
65
	 * @param Acl $acl
66
	 */
67
	public function __construct(
68
		SystemService $systemService,
69
		ShareMapper $shareMapper,
70
		Share $share,
71
		MailService $mailService,
72
		Acl $acl
73
	) {
74
		$this->systemService = $systemService;
75
		$this->shareMapper = $shareMapper;
76
		$this->share = $share;
77
		$this->mailService = $mailService;
78
		$this->acl = $acl;
79
	}
80
81
	/**
82
	 * Read all shares of a poll based on the poll id and return list as array
83
	 * @NoAdminRequired
84
	 * @param int $pollId
85
	 * @return array array of Share
86
	 * @throws NotAuthorizedException
87
	 */
88
	public function list($pollId, $token) {
89
		if ($token) {
90
			return [$this->get($token)];
91
		}
92
93
		if (!$this->acl->set($pollId)->getAllowEdit()) {
94
			throw new NotAuthorizedException;
95
		}
96
97
		return $this->shareMapper->findByPoll($pollId);
98
	}
99
100
	/**
101
	 * Get share by token
102
	 * @NoAdminRequired
103
	 * @param string $token
104
	 * @return Share
105
	 */
106
	public function get($token) {
107
		$this->share = $this->shareMapper->findByToken($token);
108
		return $this->share;
109
	}
110
111
	/**
112
	 * Add share
113
	 * @NoAdminRequired
114
	 * @param int $pollId
115
	 * @param array $user
116
	 * @return Share
117
	 * @throws NotAuthorizedException
118
	 * @throws InvalidShareType
119
	 */
120
	public function add($pollId, $type, $userId = '', $emailAddress = '') {
121
		if (!$this->acl->set($pollId)->getAllowEdit()) {
122
			throw new NotAuthorizedException;
123
		}
124
125
		$this->share = new Share();
126
127
		switch ($type) {
128
			case Group::TYPE:
129
				$share = new Group($userId);
130
				break;
131
			case Circle::TYPE:
132
				$share = new Circle($userId);
133
				break;
134
			case Contact::TYPE:
135
				$share = new Contact($userId);
136
				break;
137
			case ContactGroup::TYPE:
138
				$share = new ContactGroup($userId);
139
				break;
140
			case User::TYPE:
141
				$share = new User($userId);
142
				break;
143
			case Email::TYPE:
144
				$share = new Email($userId, $emailAddress);
145
				break;
146
			case UserGroupClass::TYPE_PUBLIC:
147
				break;
148
			default:
149
				throw new InvalidShareType('Invalid share type (' . $type . ')');
150
		}
151
152
		$this->share->setPollId($pollId);
153
		if ($type = UserGroupClass::TYPE_PUBLIC) {
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
154
			$this->share->setType(UserGroupClass::TYPE_PUBLIC);
155
		} else {
156
			$this->share->setType($share->getType());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $share does not seem to be defined for all execution paths leading up to this point.
Loading history...
157
			$this->share->setUserId($share->getId());
158
			$this->share->setDisplayName($share->getDisplayName());
159
			$this->share->setUserEmail($share->getEmailAddress());
160
		}
161
		$this->share->setInvitationSent(0);
162
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
163
			16,
164
			ISecureRandom::CHAR_DIGITS .
165
			ISecureRandom::CHAR_LOWER .
166
			ISecureRandom::CHAR_UPPER
167
		));
168
169
		return $this->shareMapper->insert($this->share);
170
	}
171
172
	/**
173
	 * Set emailAddress to personal share
174
	 * or update an email share with the username
175
	 * @NoAdminRequired
176
	 * @param string $token
177
	 * @param string $emailAddress
178
	 * @return Share
179
	 * @throws InvalidShareType
180
	 */
181
	public function setEmailAddress($token, $emailAddress) {
182
		$this->share = $this->shareMapper->findByToken($token);
183
		if ($this->share->getType() === Share::TYPE_EXTERNAL) {
184
			$this->systemService->validateEmailAddress($emailAddress);
185
			$this->share->setUserEmail($emailAddress);
186
			// TODO: Send confirmation
187
			return $this->shareMapper->update($this->share);
188
		} else {
189
			throw new InvalidShareType('Email address can only be set in external shares.');
190
		}
191
	}
192
193
	/**
194
	 * Create a personal share from a public share
195
	 * or update an email share with the username
196
	 * @NoAdminRequired
197
	 * @param string $token
198
	 * @param string $userName
199
	 * @return Share
200
	 * @throws NotAuthorizedException
201
	 */
202
	public function personal($token, $userName, $emailAddress = '') {
203
		$this->share = $this->shareMapper->findByToken($token);
204
205
		$this->systemService->validatePublicUsername($this->share->getPollId(), $userName, $token);
206
207
		if ($emailAddress) {
208
			$this->systemService->validateEmailAddress($emailAddress);
209
		}
210
211
		if ($this->share->getType() === Share::TYPE_PUBLIC) {
212
			$pollId = $this->share->getPollId();
213
			$this->share = new Share();
214
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
215
				16,
216
				ISecureRandom::CHAR_DIGITS .
217
				ISecureRandom::CHAR_LOWER .
218
				ISecureRandom::CHAR_UPPER
219
			));
220
			$this->share->setType(Share::TYPE_EXTERNAL);
221
			$this->share->setPollId($pollId);
222
			$this->share->setUserId($userName);
223
			$this->share->setUserEmail($emailAddress);
224
			$this->share->setInvitationSent(time());
225
			$this->shareMapper->insert($this->share);
226
227
			if ($emailAddress) {
228
				$this->mailService->sendInvitationMail($this->share->getToken());
229
			}
230
231
			return $this->share;
232
		} elseif ($this->share->getType() === Share::TYPE_EMAIL) {
233
			$this->share->setType(Share::TYPE_EXTERNAL);
234
			$this->share->setUserId($userName);
235
			$this->share->setUserEmail($emailAddress);
236
			return $this->shareMapper->update($this->share);
237
		} else {
238
			throw new NotAuthorizedException;
239
		}
240
	}
241
242
	/**
243
	 * Delete share
244
	 * remove share
245
	 * @NoAdminRequired
246
	 * @param string $token
247
	 * @return Share
248
	 * @throws NotAuthorizedException
249
	 */
250
251
	public function delete($token) {
252
		$this->share = $this->shareMapper->findByToken($token);
253
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
254
			throw new NotAuthorizedException;
255
		}
256
257
		$this->shareMapper->delete($this->share);
258
259
		return $this->share;
260
	}
261
}
262