Completed
Pull Request — master (#1038)
by René
04:22
created

ShareService::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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