Completed
Pull Request — master (#1038)
by René
06:20
created

ShareService::list()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
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\InvalidUsername;
28
use OCA\Polls\Exceptions\InvalidShareType;
29
30
use OCP\Security\ISecureRandom;
31
32
use OCA\Polls\Controller\SystemController;
33
use OCA\Polls\Db\ShareMapper;
34
use OCA\Polls\Db\Share;
35
use OCA\Polls\Model\Acl;
36
37
class ShareService {
38
39
	/** @var SystemController */
40
	private $systemController;
41
42
	/** @var ShareMapper */
43
	private $shareMapper;
44
45
	/** @var Share */
46
	private $share;
47
48
	/** @var MailService */
49
	private $mailService;
50
51
	/** @var Acl */
52
	private $acl;
53
54
	/**
55
	 * ShareController constructor.
56
	 * @param SystemController $systemController
57
	 * @param ShareMapper $shareMapper
58
	 * @param Share $share
59
	 * @param MailService $mailService
60
	 * @param Acl $acl
61
	 */
62
	public function __construct(
63
		SystemController $systemController,
64
		ShareMapper $shareMapper,
65
		Share $share,
66
		MailService $mailService,
67
		Acl $acl
68
	) {
69
		$this->systemController = $systemController;
70
		$this->shareMapper = $shareMapper;
71
		$this->share = $share;
72
		$this->mailService = $mailService;
73
		$this->acl = $acl;
74
	}
75
76
	/**
77
	 * Read all shares of a poll based on the poll id and return list as array
78
	 * @NoAdminRequired
79
	 * @param int $pollId
80
	 * @return array array of Share
81
	 * @throws NotAuthorizedException
82
	 */
83
	public function list($pollId, $token) {
84
		if ($token) {
85
			return [$this->get($token)];
86
		}
87
88
		if (!$this->acl->set($pollId)->getAllowEdit()) {
89
			throw new NotAuthorizedException;
90
		}
91
92
		return $this->shareMapper->findByPoll($pollId);
93
	}
94
95
	/**
96
	 * Get share by token
97
	 * @NoAdminRequired
98
	 * @param string $token
99
	 * @return Share
100
	 */
101
	public function get($token) {
102
		$this->share = $this->shareMapper->findByToken($token);
103
104
		return $this->share;
105
	}
106
107
	/**
108
	 * Add share
109
	 * @NoAdminRequired
110
	 * @param int $pollId
111
	 * @param string $type
112
	 * @param string $userId
113
	 * @param string $userEmail
114
	 * @return Share
115
	 * @throws NotAuthorizedException
116
	 */
117
	public function add($pollId, $type, $userId, $userEmail = '') {
118
		if (!$this->acl->set($pollId)->getAllowEdit()) {
119
			throw new NotAuthorizedException;
120
		}
121
122
		$this->share = new Share();
123
		$this->share->setType($type);
124
		$this->share->setPollId($pollId);
125
		$this->share->setUserId($userId);
126
		$this->share->setUserEmail($userEmail);
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
		return $this->shareMapper->insert($this->share);
136
	}
137
138
	/**
139
	 * Set emailAddress to personal share
140
	 * or update an email share with the username
141
	 * @NoAdminRequired
142
	 * @param string $token
143
	 * @param string $emailAddress
144
	 * @return Share
145
	 * @throws NotAuthorizedException
146
	 */
147
	public function setEmailAddress($token, $emailAddress) {
148
		$this->share = $this->shareMapper->findByToken($token);
149
		if ($this->share->getType() === 'external') {
150
			// TODO: Simple validate email address
151
			$this->share->setUserEmail($emailAddress);
152
			// TODO: Send confirmation
153
			return $this->shareMapper->update($this->share);
154
		} else {
155
			throw new InvalidShareType('Email address can only be set in external shares.');
156
		}
157
	}
158
159
	/**
160
	 * Create a personal share from a public share
161
	 * or update an email share with the username
162
	 * @NoAdminRequired
163
	 * @param string $token
164
	 * @param string $userName
165
	 * @return Share
166
	 * @throws NotAuthorizedException
167
	 * @throws InvalidUsername
168
	 */
169
	public function personal($token, $userName, $emailAddress) {
170
		$this->share = $this->shareMapper->findByToken($token);
171
172
		// Return of validatePublicUsername is a DataResponse
173
		$checkUsername = $this->systemController->validatePublicUsername($this->share->getPollId(), $userName, $token);
174
175
		// if status is not 200, return DataResponse from validatePublicUsername
176
		if ($checkUsername->getStatus() !== 200) {
177
			throw new InvalidUsername;
178
		}
179
180
		if ($this->share->getType() === 'public') {
181
			$pollId = $this->share->getPollId();
182
			$this->share = new Share();
183
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
184
				16,
185
				ISecureRandom::CHAR_DIGITS .
186
				ISecureRandom::CHAR_LOWER .
187
				ISecureRandom::CHAR_UPPER
188
			));
189
			$this->share->setType('external');
190
			$this->share->setPollId($pollId);
191
			$this->share->setUserId($userName);
192
			$this->share->setUserEmail($emailAddress);
193
			$this->share->setInvitationSent(time());
194
			$this->shareMapper->insert($this->share);
195
			$this->mailService->sendInvitationMail($this->share->getToken());
196
			return $this->share;
197
		} elseif ($this->share->getType() === 'email') {
198
			$this->share->setType('external');
199
			$this->share->setUserId($userName);
200
			$this->share->setUserEmail($emailAddress);
201
			return $this->shareMapper->update($this->share);
202
		} else {
203
			throw new NotAuthorizedException;
204
		}
205
	}
206
207
	/**
208
	 * Delete share
209
	 * remove share
210
	 * @NoAdminRequired
211
	 * @param string $token
212
	 * @return Share
213
	 * @throws NotAuthorizedException
214
	 */
215
216
	public function delete($token) {
217
		$this->share = $this->shareMapper->findByToken($token);
218
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
219
			throw new NotAuthorizedException;
220
		}
221
222
		$this->shareMapper->delete($this->share);
223
224
		return $this->share;
225
	}
226
}
227