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

ShareService::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 18
c 1
b 0
f 1
nc 2
nop 4
dl 0
loc 24
ccs 0
cts 21
cp 0
crap 6
rs 9.6666
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\ILogger;
29
use OCP\Security\ISecureRandom;
30
31
use OCA\Polls\Exceptions\NotAuthorizedException;
32
use OCA\Polls\Exceptions\InvalidUsername;
33
34
use OCA\Polls\Db\Share;
35
use OCA\Polls\Db\ShareMapper;
36
use OCA\Polls\Service\MailService;
37
use OCA\Polls\Model\Acl;
38
// TODO: Change to Service
39
use OCA\Polls\Controller\SystemController;
40
41
class ShareService {
42
43
	private $logger;
44
	private $acl;
45
	private $shareMapper;
46
	private $share;
47
	private $userId;
48
49
	private $pollMapper;
0 ignored issues
show
introduced by
The private property $pollMapper is not used, and could be removed.
Loading history...
50
	private $systemController;
51
	private $mailService;
52
53
	/**
54
	 * ShareController constructor.
55
	 * @param string $appName
56
	 * @param string $userId
57
	 * @param IRequest $request
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\IRequest 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...
58
	 * @param ILogger $logger
59
	 * @param ShareMapper $shareMapper
60
	 * @param Share $share
61
	 * @param SystemController $systemController
62
	 * @param MailService $mailService
63
	 * @param Acl $acl
64
	 */
65
	public function __construct(
66
		string $appName,
0 ignored issues
show
Unused Code introduced by
The parameter $appName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

66
		/** @scrutinizer ignore-unused */ string $appName,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
		$userId,
68
		ILogger $logger,
69
		ShareMapper $shareMapper,
70
		Share $share,
71
		SystemController $systemController,
72
		MailService $mailService,
73
		Acl $acl
74
	) {
75
		$this->logger = $logger;
76
		$this->userId = $userId;
77
		$this->shareMapper = $shareMapper;
78
		$this->share = $share;
79
		$this->systemController = $systemController;
80
		$this->mailService = $mailService;
81
		$this->acl = $acl;
82
	}
83
84
	/**
85
	 * get
86
	 * Read all shares of a poll based on the poll id and return list as array
87
	 * @NoAdminRequired
88
	 * @param integer $pollId
89
	 * @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...
90
	 */
91
	public function list($pollId) {
92
		if ($this->acl->setPollId($pollId)->getAllowEdit()) {
93
			return $this->shareMapper->findByPoll($pollId);
94
		} else {
95
			throw new NotAuthorizedException;
96
		}
97
	}
98
99
	/**
100
	 * getByToken
101
	 * Get pollId by token
102
	 * @NoAdminRequired
103
	 * @param string $token
104
	 * @return Array
105
	 */
106
	public function get($token) {
107
		$this->share = $this->shareMapper->findByToken($token);
108
		return $this->share;
109
	}
110
111
	/**
112
	 * Write a new share to the db and returns the new share as array
113
	 * @NoAdminRequired
114
	 * @depricated
115
	 * @param int $pollId
116
	 * @param string $share
117
	 * @return Array
118
	 */
119
	 // TODO: Replace with $this->add and separate sending invitations
120
	public function write($pollId, $type, $userId, $userEmail = '') {
121
		$this->acl->setPollId($pollId);
122
		if (!$this->acl->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->setToken(\OC::$server->getSecureRandom()->generate(
132
			16,
133
			ISecureRandom::CHAR_DIGITS .
134
			ISecureRandom::CHAR_LOWER .
135
			ISecureRandom::CHAR_UPPER
136
		));
137
138
		$this->share = $this->shareMapper->insert($this->share);
139
		$sendResult = $this->mailService->sendInvitationMail($this->share->getToken());
140
141
		return [
142
			'share' => $this->share,
143
			'sendResult' => $sendResult
144
		];
145
	}
146
147
	/**
148
	 * Write a new share to the db and returns the new share as array
149
	 * @NoAdminRequired
150
	 * @param int $pollId
151
	 * @param string $share
152
	 * @return Array
153
	 */
154
	public function add($pollId, $type, $userId, $userEmail = '') {
155
		$this->acl->setPollId($pollId);
156
		if (!$this->acl->getAllowEdit()) {
157
			throw new NotAuthorizedException;
158
		}
159
160
		$this->share = new Share();
161
		$this->share->setType($type);
162
		$this->share->setPollId($pollId);
163
		$this->share->setUserId($userId);
164
		$this->share->setUserEmail($userEmail);
165
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
166
			16,
167
			ISecureRandom::CHAR_DIGITS .
168
			ISecureRandom::CHAR_LOWER .
169
			ISecureRandom::CHAR_UPPER
170
		));
171
172
		return $this->shareMapper->insert($this->share);
173
174
	}
175
176
	/**
177
	 * createPersonalShare
178
	 * Write a new share to the db and returns the new share as array
179
	 * @NoAdminRequired
180
	 * @param string $token
181
	 * @param string $userName
182
	 * @return Share
183
	 */
184
	public function createPersonalShare($token, $userName) {
185
186
		$publicShare = $this->shareMapper->findByToken($token);
187
188
		// Return of validatePublicUsername is a DataResponse
189
		$checkUsername = $this->systemController->validatePublicUsername($publicShare->getPollId(), $userName, $token);
190
191
		// if status is not 200, return DataResponse from validatePublicUsername
192
		if ($checkUsername->getStatus() !== 200) {
193
			throw new InvalidUsername;
194
		}
195
196
		if ($publicShare->getType() === 'public') {
197
198
			$this->share = new Share();
199
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
200
				16,
201
				ISecureRandom::CHAR_DIGITS .
202
				ISecureRandom::CHAR_LOWER .
203
				ISecureRandom::CHAR_UPPER
204
			));
205
			$this->share->setType('external');
206
			$this->share->setPollId($publicShare->getPollId());
207
			$this->share->setUserId($userName);
208
			$this->share->setUserEmail('');
209
			$this->share = $this->shareMapper->insert($this->share);
210
			return $this->share;
211
212
		} elseif ($publicShare->getType() === 'email') {
213
214
			$publicShare->setType('external');
215
			$publicShare->setUserId($userName);
216
			$this->shareMapper->update($publicShare);
217
			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...
218
219
		} else {
220
			throw new NotAuthorizedException;
221
		}
222
	}
223
224
	/**
225
	 * remove
226
	 * remove share
227
	 * @NoAdminRequired
228
	 * @param string $token
229
	 * @return Share
230
	 */
231
232
	public function remove($token) {
233
		$this->share = $this->shareMapper->findByToken($token);
234
		if ($this->acl->setPollId($this->share->getPollId())->getAllowEdit()) {
235
			$this->shareMapper->delete($this->share);
236
			return $this->share;
237
		} else {
238
			throw new NotAuthorizedException;
239
		}
240
	}
241
}
242