Completed
Pull Request — master (#1128)
by René
04:27
created

ShareService::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 10
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 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
		$this->share->setPollId($pollId);
127
		$this->share->setInvitationSent(0);
128
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
129
			16,
130
			ISecureRandom::CHAR_DIGITS .
131
			ISecureRandom::CHAR_LOWER .
132
			ISecureRandom::CHAR_UPPER
133
		));
134
135
136
		if ($type === UserGroupClass::TYPE_PUBLIC) {
137
			$this->share->setType(UserGroupClass::TYPE_PUBLIC);
138
		} else {
139
			switch ($type) {
140
				case Group::TYPE:
141
					$share = new Group($userId);
142
					break;
143
				case Circle::TYPE:
144
					$share = new Circle($userId);
145
					break;
146
				case Contact::TYPE:
147
					$share = new Contact($userId);
148
					break;
149
				case ContactGroup::TYPE:
150
					$share = new ContactGroup($userId);
151
					break;
152
				case User::TYPE:
153
					$share = new User($userId);
154
					break;
155
				case Email::TYPE:
156
					$share = new Email($userId, $emailAddress);
157
					break;
158
				default:
159
					throw new InvalidShareType('Invalid share type (' . $type . ')');
160
			}
161
162
			$this->share->setType($share->getType());
163
			$this->share->setUserId($share->getId());
164
			$this->share->setDisplayName($share->getDisplayName());
165
			$this->share->setUserEmail($share->getEmailAddress());
166
		}
167
168
		return $this->shareMapper->insert($this->share);
169
	}
170
171
	/**
172
	 * Set emailAddress to personal share
173
	 * or update an email share with the username
174
	 * @NoAdminRequired
175
	 * @param string $token
176
	 * @param string $emailAddress
177
	 * @return Share
178
	 * @throws InvalidShareType
179
	 */
180
	public function setEmailAddress($token, $emailAddress) {
181
		$this->share = $this->shareMapper->findByToken($token);
182
		if ($this->share->getType() === Share::TYPE_EXTERNAL) {
183
			$this->systemService->validateEmailAddress($emailAddress);
184
			$this->share->setUserEmail($emailAddress);
185
			// TODO: Send confirmation
186
			return $this->shareMapper->update($this->share);
187
		} else {
188
			throw new InvalidShareType('Email address can only be set in external shares.');
189
		}
190
	}
191
192
	/**
193
	 * Create a personal share from a public share
194
	 * or update an email share with the username
195
	 * @NoAdminRequired
196
	 * @param string $token
197
	 * @param string $userName
198
	 * @return Share
199
	 * @throws NotAuthorizedException
200
	 */
201
	public function personal($token, $userName, $emailAddress = '') {
202
		$this->share = $this->shareMapper->findByToken($token);
203
204
		$this->systemService->validatePublicUsername($this->share->getPollId(), $userName, $token);
205
206
		if ($emailAddress) {
207
			$this->systemService->validateEmailAddress($emailAddress);
208
		}
209
210
		if ($this->share->getType() === Share::TYPE_PUBLIC) {
211
			$pollId = $this->share->getPollId();
212
			$this->share = new Share();
213
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
214
				16,
215
				ISecureRandom::CHAR_DIGITS .
216
				ISecureRandom::CHAR_LOWER .
217
				ISecureRandom::CHAR_UPPER
218
			));
219
			$this->share->setType(Share::TYPE_EXTERNAL);
220
			$this->share->setPollId($pollId);
221
			$this->share->setUserId($userName);
222
			$this->share->setUserEmail($emailAddress);
223
			$this->share->setInvitationSent(time());
224
			$this->shareMapper->insert($this->share);
225
226
			if ($emailAddress) {
227
				$this->mailService->sendInvitationMail($this->share->getToken());
228
			}
229
230
			return $this->share;
231
		} elseif ($this->share->getType() === Share::TYPE_EMAIL) {
232
			$this->share->setType(Share::TYPE_EXTERNAL);
233
			$this->share->setUserId($userName);
234
			$this->share->setUserEmail($emailAddress);
235
			return $this->shareMapper->update($this->share);
236
		} else {
237
			throw new NotAuthorizedException;
238
		}
239
	}
240
241
	/**
242
	 * Delete share
243
	 * remove share
244
	 * @NoAdminRequired
245
	 * @param string $token
246
	 * @return Share
247
	 * @throws NotAuthorizedException
248
	 */
249
250
	public function delete($token) {
251
		$this->share = $this->shareMapper->findByToken($token);
252
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
253
			throw new NotAuthorizedException;
254
		}
255
256
		$this->shareMapper->delete($this->share);
257
258
		return $this->share;
259
	}
260
}
261