Completed
Push — master ( 9466d5...cf3e21 )
by René
18s queued 15s
created

ShareService::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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