Passed
Pull Request — master (#1169)
by René
06:00
created

ShareService::list()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 6
rs 10
ccs 0
cts 4
cp 0
cc 2
nc 2
nop 1
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
	/**
149
	 * Add share
150
	 * @NoAdminRequired
151
	 * @param int $pollId
152
	 * @param array $user
153
	 * @return Share
154
	 * @throws NotAuthorizedException
155
	 * @throws InvalidShareType
156
	 */
157
	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

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