Passed
Pull Request — master (#966)
by René
04:17
created

ShareService::remove()   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 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 6
rs 10
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 DataResponse
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 Array
91
	 */
92
	public function get($token) {
93
		$this->share = $this->shareMapper->findByToken($token);
94
		return $this->share;
95
	}
96
97
	/**
98
	 * Write a new share to the db and returns the new share as array
99
	 * @NoAdminRequired
100
	 * @depricated
101
	 * @param int $pollId
102
	 * @param string $share
103
	 * @return Array
104
	 */
105
	 // TODO: Replace with $this->add and separate sending invitations
106
	public function write($pollId, $type, $userId, $userEmail = '') {
107
108
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
109
			throw new NotAuthorizedException;
110
		}
111
112
		$this->share = new Share();
113
		$this->share->setType($type);
114
		$this->share->setPollId($pollId);
115
		$this->share->setUserId($userId);
116
		$this->share->setUserEmail($userEmail);
117
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
118
			16,
119
			ISecureRandom::CHAR_DIGITS .
120
			ISecureRandom::CHAR_LOWER .
121
			ISecureRandom::CHAR_UPPER
122
		));
123
124
		$this->share = $this->shareMapper->insert($this->share);
125
		$sendResult = $this->mailService->sendInvitationMail($this->share->getToken());
126
127
		return [
128
			'share' => $this->share,
129
			'sendResult' => $sendResult
130
		];
131
	}
132
133
	/**
134
	 * Write a new share to the db and returns the new share as array
135
	 * @NoAdminRequired
136
	 * @param int $pollId
137
	 * @param string $share
138
	 * @return Array
139
	 */
140
	public function add($pollId, $type, $userId, $userEmail = '') {
141
142
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
143
			throw new NotAuthorizedException;
144
		}
145
146
		$this->share = new Share();
147
		$this->share->setType($type);
148
		$this->share->setPollId($pollId);
149
		$this->share->setUserId($userId);
150
		$this->share->setUserEmail($userEmail);
151
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
152
			16,
153
			ISecureRandom::CHAR_DIGITS .
154
			ISecureRandom::CHAR_LOWER .
155
			ISecureRandom::CHAR_UPPER
156
		));
157
158
		return $this->shareMapper->insert($this->share);
159
160
	}
161
162
	/**
163
	 * createPersonalShare
164
	 * Write a new share to the db and returns the new share as array
165
	 * @NoAdminRequired
166
	 * @param string $token
167
	 * @param string $userName
168
	 * @return Share
169
	 */
170
	public function createPersonalShare($token, $userName) {
171
		$publicShare = $this->shareMapper->findByToken($token);
172
173
		// Return of validatePublicUsername is a DataResponse
174
		$checkUsername = $this->systemController->validatePublicUsername($publicShare->getPollId(), $userName, $token);
175
176
		// if status is not 200, return DataResponse from validatePublicUsername
177
		if ($checkUsername->getStatus() !== 200) {
178
			throw new InvalidUsername;
179
		}
180
181
		if ($publicShare->getType() === 'public') {
182
183
184
			$this->share = new Share();
185
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
186
				16,
187
				ISecureRandom::CHAR_DIGITS .
188
				ISecureRandom::CHAR_LOWER .
189
				ISecureRandom::CHAR_UPPER
190
			));
191
			$this->share->setType('external');
192
			$this->share->setPollId($publicShare->getPollId());
193
			$this->share->setUserId($userName);
194
			$this->share->setUserEmail('');
195
			$this->share = $this->shareMapper->insert($this->share);
196
			return $this->share;
197
198
		} elseif ($publicShare->getType() === 'email') {
199
200
			$publicShare->setType('external');
201
			$publicShare->setUserId($userName);
202
			$this->shareMapper->update($publicShare);
203
			return new DataResponse($publicShare, Http::STATUS_OK);
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
204
205
		} else {
206
			throw new NotAuthorizedException;
207
		}
208
	}
209
210
	/**
211
	 * remove
212
	 * remove share
213
	 * @NoAdminRequired
214
	 * @param string $token
215
	 * @return Share
216
	 */
217
218
	public function remove($token) {
219
		$this->share = $this->shareMapper->findByToken($token);
220
		if (!$this->acl->setPollId($this->share->getPollId())->getAllowEdit()) {
221
			throw new NotAuthorizedException;
222
		}
223
224
		$this->shareMapper->delete($this->share);
225
226
		return $this->share;
227
228
	}
229
}
230