Completed
Push — master ( 798959...da39f8 )
by René
16s queued 10s
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 7
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
35
class ShareService {
36
37
	/** @var SystemService */
38
	private $systemService;
39
40
	/** @var ShareMapper */
41
	private $shareMapper;
42
43
	/** @var Share */
44
	private $share;
45
46
	/** @var MailService */
47
	private $mailService;
48
49
	/** @var Acl */
50
	private $acl;
51
52
	/**
53
	 * ShareController constructor.
54
	 * @param SystemService $systemService
55
	 * @param ShareMapper $shareMapper
56
	 * @param Share $share
57
	 * @param MailService $mailService
58
	 * @param Acl $acl
59
	 */
60
	public function __construct(
61
		SystemService $systemService,
62
		ShareMapper $shareMapper,
63
		Share $share,
64
		MailService $mailService,
65
		Acl $acl
66
	) {
67
		$this->systemService = $systemService;
68
		$this->shareMapper = $shareMapper;
69
		$this->share = $share;
70
		$this->mailService = $mailService;
71
		$this->acl = $acl;
72
	}
73
74
	/**
75
	 * Read all shares of a poll based on the poll id and return list as array
76
	 * @NoAdminRequired
77
	 * @param int $pollId
78
	 * @return array array of Share
79
	 * @throws NotAuthorizedException
80
	 */
81
	public function list($pollId, $token) {
82
		if ($token) {
83
			return [$this->get($token)];
84
		}
85
86
		if (!$this->acl->set($pollId)->getAllowEdit()) {
87
			throw new NotAuthorizedException;
88
		}
89
90
		return $this->shareMapper->findByPoll($pollId);
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
		return $this->share;
103
	}
104
105
	/**
106
	 * Add share
107
	 * @NoAdminRequired
108
	 * @param int $pollId
109
	 * @param string $type
110
	 * @param string $userId
111
	 * @param string $userEmail
112
	 * @return Share
113
	 * @throws NotAuthorizedException
114
	 */
115
	public function add($pollId, $type, $userId, $userEmail = '') {
116
		if (!$this->acl->set($pollId)->getAllowEdit()) {
117
			throw new NotAuthorizedException;
118
		}
119
120
		$this->share = new Share();
121
		$this->share->setType($type);
122
		$this->share->setPollId($pollId);
123
		$this->share->setUserId($userId);
124
		$this->share->setUserEmail($userEmail);
125
		$this->share->setInvitationSent(0);
126
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
127
			16,
128
			ISecureRandom::CHAR_DIGITS .
129
			ISecureRandom::CHAR_LOWER .
130
			ISecureRandom::CHAR_UPPER
131
		));
132
133
		return $this->shareMapper->insert($this->share);
134
	}
135
136
	/**
137
	 * Set emailAddress to personal share
138
	 * or update an email share with the username
139
	 * @NoAdminRequired
140
	 * @param string $token
141
	 * @param string $emailAddress
142
	 * @return Share
143
	 * @throws InvalidShareType
144
	 */
145
	public function setEmailAddress($token, $emailAddress) {
146
		$this->share = $this->shareMapper->findByToken($token);
147
		if ($this->share->getType() === 'external') {
148
			$this->systemService->validateEmailAddress($emailAddress);
149
			$this->share->setUserEmail($emailAddress);
150
			// TODO: Send confirmation
151
			return $this->shareMapper->update($this->share);
152
		} else {
153
			throw new InvalidShareType('Email address can only be set in external shares.');
154
		}
155
	}
156
157
	/**
158
	 * Create a personal share from a public share
159
	 * or update an email share with the username
160
	 * @NoAdminRequired
161
	 * @param string $token
162
	 * @param string $userName
163
	 * @return Share
164
	 * @throws NotAuthorizedException
165
	 */
166
	public function personal($token, $userName, $emailAddress = '') {
167
		$this->share = $this->shareMapper->findByToken($token);
168
169
		$this->systemService->validatePublicUsername($this->share->getPollId(), $userName, $token);
170
171
		if ($emailAddress) {
172
			$this->systemService->validateEmailAddress($emailAddress);
173
		}
174
175
		if ($this->share->getType() === 'public') {
176
			$pollId = $this->share->getPollId();
177
			$this->share = new Share();
178
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
179
				16,
180
				ISecureRandom::CHAR_DIGITS .
181
				ISecureRandom::CHAR_LOWER .
182
				ISecureRandom::CHAR_UPPER
183
			));
184
			$this->share->setType('external');
185
			$this->share->setPollId($pollId);
186
			$this->share->setUserId($userName);
187
			$this->share->setUserEmail($emailAddress);
188
			$this->share->setInvitationSent(time());
189
			$this->shareMapper->insert($this->share);
190
191
			if ($emailAddress) {
192
				$this->mailService->sendInvitationMail($this->share->getToken());
193
			}
194
195
			return $this->share;
196
		} elseif ($this->share->getType() === 'email') {
197
			$this->share->setType('external');
198
			$this->share->setUserId($userName);
199
			$this->share->setUserEmail($emailAddress);
200
			return $this->shareMapper->update($this->share);
201
		} else {
202
			throw new NotAuthorizedException;
203
		}
204
	}
205
206
	/**
207
	 * Delete share
208
	 * remove share
209
	 * @NoAdminRequired
210
	 * @param string $token
211
	 * @return Share
212
	 * @throws NotAuthorizedException
213
	 */
214
215
	public function delete($token) {
216
		$this->share = $this->shareMapper->findByToken($token);
217
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
218
			throw new NotAuthorizedException;
219
		}
220
221
		$this->shareMapper->delete($this->share);
222
223
		return $this->share;
224
	}
225
}
226