Passed
Push — master ( 4abceb...6361bf )
by René
04:58 queued 11s
created

ShareService::create()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 5
cp 0
rs 9.8333
cc 2
nc 1
nop 3
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 OCP\AppFramework\Db\DoesNotExistException;
27
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
28
use OCA\Polls\Exceptions\NotAuthorizedException;
29
use OCA\Polls\Exceptions\InvalidShareType;
30
use OCA\Polls\Exceptions\ShareAlreadyExists;
31
32
use OCP\Security\ISecureRandom;
33
34
use OCA\Polls\Db\ShareMapper;
35
use OCA\Polls\Db\Share;
36
use OCA\Polls\Model\Acl;
37
use OCA\Polls\Model\UserGroupClass;
38
39
class ShareService {
40
41
	/** @var SystemService */
42
	private $systemService;
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 SystemService $systemService
59
	 * @param ShareMapper $shareMapper
60
	 * @param Share $share
61
	 * @param MailService $mailService
62
	 * @param Acl $acl
63
	 */
64
	public function __construct(
65
		SystemService $systemService,
66
		ShareMapper $shareMapper,
67
		Share $share,
68
		MailService $mailService,
69
		Acl $acl
70
	) {
71
		$this->systemService = $systemService;
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) {
86
		if (!$this->acl->set($pollId)->getAllowEdit()) {
87
			throw new NotAuthorizedException;
88
		}
89
		$shares = $this->shareMapper->findByPoll($pollId);
90
		return $shares;
91
	}
92
93
	/**
94
	 * Get share by token
95
	 * @NoAdminRequired
96
	 * @param string $token
97
	 * @return Share
98
	 */
99
	public function get($token) {
100
		$this->share = $this->shareMapper->findByToken($token);
101
102
		// Allow users entering the poll with a public share access
103
		if ($this->share->getType() === Share::TYPE_PUBLIC && \OC::$server->getUserSession()->getUser()->getUID()) {
104
105
			// Check if the user has already access
106
			if (!$this->acl->set($this->share->getPollId())->getAllowView()) {
107
108
				// Create a new share for this user, so he is allowed to access the poll later
109
				// via normal shared access and return the created share
110
				return $this->create(
111
					$this->share->getPollId(),
112
					UserGroupClass::getUserGroupChild(Share::TYPE_USER, \OC::$server->getUserSession()->getUser()->getUID()),
113
					true
114
				);
115
			}
116
		}
117
		return $this->share;
118
	}
119
120
121
	/**
122
	 * crate share
123
	 * @NoAdminRequired
124
	 * @param int $pollId
125
	 * @param UserGroupClass $userGroup
126
	 * @param bool $skipInvitation
127
	 * @return Share
128
	 */
129
	private function create($pollId, $userGroup, $skipInvitation = fale) {
0 ignored issues
show
Bug introduced by
The constant OCA\Polls\Service\fale was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
130
		$this->share = new Share();
131
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
132
			16,
133
			ISecureRandom::CHAR_DIGITS .
134
			ISecureRandom::CHAR_LOWER .
135
			ISecureRandom::CHAR_UPPER
136
		));
137
		$this->share->setPollId($pollId);
138
		$this->share->setInvitationSent($skipInvitation ? time() : 0);
139
		$this->share->setType($userGroup->getType());
140
		$this->share->setUserId($userGroup->getId());
141
		$this->share->setDisplayName($userGroup->getDisplayName());
142
		$this->share->setUserEmail($userGroup->getEmailAddress());
143
144
		return $this->shareMapper->insert($this->share);
145
	}
146
147
	/**
148
	 * Add share
149
	 * @NoAdminRequired
150
	 * @param int $pollId
151
	 * @param array $user
152
	 * @return Share
153
	 * @throws NotAuthorizedException
154
	 * @throws InvalidShareType
155
	 */
156
	public function add($pollId, $type, $userId = '', $emailAddress = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $emailAddress is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
	public function add($pollId, $type, $userId = '', /** @scrutinizer ignore-unused */ $emailAddress = '') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
		if (!$this->acl->set($pollId)->getAllowEdit()) {
158
			throw new NotAuthorizedException;
159
		}
160
161
		if ($type !== UserGroupClass::TYPE_PUBLIC) {
162
			try {
163
				$this->shareMapper->findByPollAndUser($pollId, $userId);
164
				throw new ShareAlreadyExists;
165
			} catch (MultipleObjectsReturnedException $e) {
166
				throw new ShareAlreadyExists;
167
			} catch (DoesNotExistException $e) {
168
				// continue
169
			}
170
		}
171
172
		$userGroup = UserGroupClass::getUserGroupChild($type, $userId);
173
		return $this->create($pollId, $userGroup) ;
174
175
		// $this->share = new Share();
176
		// $this->share->setPollId($pollId);
177
		// $this->share->setInvitationSent(0);
178
		// $this->share->setToken(\OC::$server->getSecureRandom()->generate(
179
		// 	16,
180
		// 	ISecureRandom::CHAR_DIGITS .
181
		// 	ISecureRandom::CHAR_LOWER .
182
		// 	ISecureRandom::CHAR_UPPER
183
		// ));
184
		//
185
		//
186
		// $userGroup = UserGroupClass::getUserGroupChild($type, $userId);
187
		// $this->share->setType($userGroup->getType());
188
		// $this->share->setUserId($userGroup->getId());
189
		// $this->share->setDisplayName($userGroup->getDisplayName());
190
		// $this->share->setUserEmail($userGroup->getEmailAddress());
191
		//
192
		// return $this->shareMapper->insert($this->share);
193
	}
194
195
	/**
196
	 * Set emailAddress to personal share
197
	 * or update an email share with the username
198
	 * @NoAdminRequired
199
	 * @param string $token
200
	 * @param string $emailAddress
201
	 * @return Share
202
	 * @throws InvalidShareType
203
	 */
204
	public function setEmailAddress($token, $emailAddress) {
205
		$this->share = $this->shareMapper->findByToken($token);
206
		if ($this->share->getType() === Share::TYPE_EXTERNAL) {
207
			$this->systemService->validateEmailAddress($emailAddress);
208
			$this->share->setUserEmail($emailAddress);
209
			// TODO: Send confirmation
210
			return $this->shareMapper->update($this->share);
211
		} else {
212
			throw new InvalidShareType('Email address can only be set in external shares.');
213
		}
214
	}
215
216
	/**
217
	 * Create a personal share from a public share
218
	 * or update an email share with the username
219
	 * @NoAdminRequired
220
	 * @param string $token
221
	 * @param string $userName
222
	 * @return Share
223
	 * @throws NotAuthorizedException
224
	 */
225
	public function personal($token, $userName, $emailAddress = '') {
226
		$this->share = $this->shareMapper->findByToken($token);
227
228
		$this->systemService->validatePublicUsername($this->share->getPollId(), $userName, $token);
229
230
		if ($emailAddress) {
231
			$this->systemService->validateEmailAddress($emailAddress);
232
		}
233
234
		if ($this->share->getType() === Share::TYPE_PUBLIC) {
235
			$pollId = $this->share->getPollId();
236
			$this->share = new Share();
237
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
238
				16,
239
				ISecureRandom::CHAR_DIGITS .
240
				ISecureRandom::CHAR_LOWER .
241
				ISecureRandom::CHAR_UPPER
242
			));
243
			$this->share->setType(Share::TYPE_EXTERNAL);
244
			$this->share->setPollId($pollId);
245
			$this->share->setUserId($userName);
246
			$this->share->setDisplayName($userName);
247
			$this->share->setUserEmail($emailAddress);
248
			$this->share->setInvitationSent(time());
249
			$this->shareMapper->insert($this->share);
250
251
			if ($emailAddress) {
252
				$this->mailService->sendInvitationMail($this->share->getToken());
253
			}
254
255
			return $this->share;
256
		} elseif ($this->share->getType() === Share::TYPE_EMAIL) {
257
			$this->share->setType(Share::TYPE_EXTERNAL);
258
			$this->share->setUserId($userName);
259
			$this->share->setUserEmail($emailAddress);
260
			return $this->shareMapper->update($this->share);
261
		} else {
262
			throw new NotAuthorizedException;
263
		}
264
	}
265
266
	/**
267
	 * Delete share
268
	 * remove share
269
	 * @NoAdminRequired
270
	 * @param string $token
271
	 * @return Share
272
	 * @throws NotAuthorizedException
273
	 */
274
275
	public function delete($token) {
276
		$this->share = $this->shareMapper->findByToken($token);
277
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
278
			throw new NotAuthorizedException;
279
		}
280
281
		$this->shareMapper->delete($this->share);
282
283
		return $this->share;
284
	}
285
}
286