Passed
Pull Request — master (#708)
by René
03:48
created

ShareController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 9
dl 0
loc 19
ccs 0
cts 19
cp 0
crap 2
rs 10
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Controller;
25
26
use Exception;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
29
30
use OCP\IRequest;
31
use OCP\ILogger;
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
36
use OCP\Security\ISecureRandom;
37
38
use OCA\Polls\Db\Poll;
39
40
use OCA\Polls\Model\Acl;
41
use OCA\Polls\Db\PollMapper;
42
use OCA\Polls\Db\Share;
43
use OCA\Polls\Db\ShareMapper;
44
use OCA\Polls\Service\MailService;
45
// TODO: Change to Service
46
use OCA\Polls\Controller\SystemController;
47
48
class ShareController extends Controller {
49
50
	private $logger;
51
	private $acl;
52
	private $mapper;
53
	private $userId;
54
55
	private $pollMapper;
56
	private $systemController;
57
	private $mailService;
58
59
	/**
60
	 * ShareController constructor.
61
	 * @param string $appName
62
	 * @param string $userId
63
	 * @param IRequest $request
64
	 * @param ILogger $logger
65
	 * @param ShareMapper $mapper
66
	 * @param PollMapper $pollMapper
67
	 * @param SystemController $systemController
68
	 * @param MailService $mailService
69
	 * @param Acl $acl
70
	 */
71
	public function __construct(
72
		string $appName,
73
		$userId,
74
		IRequest $request,
75
		ILogger $logger,
76
		ShareMapper $mapper,
77
		PollMapper $pollMapper,
78
		SystemController $systemController,
79
		MailService $mailService,
80
		Acl $acl
81
	) {
82
		parent::__construct($appName, $request);
83
		$this->logger = $logger;
84
		$this->userId = $userId;
85
		$this->mapper = $mapper;
86
		$this->pollMapper = $pollMapper;
87
		$this->systemController = $systemController;
88
		$this->mailService = $mailService;
89
		$this->acl = $acl;
90
	}
91
92
	/**
93
	 * getByToken
94
	 * Get pollId by token
95
	 * @NoAdminRequired
96
	 * @NoCSRFRequired
97
	 * @PublicPage
98
	 * @param string $token
99
	 * @return DataResponse
100
	 */
101
	public function get($token) {
102
		try {
103
			$share = $this->mapper->findByToken($token);
104
			return new DataResponse($share, Http::STATUS_OK);
105
106
		} catch (DoesNotExistException $e) {
107
			return new DataResponse(null, Http::STATUS_NOT_FOUND);
108
		}
109
	}
110
111
	/**
112
	 * get
113
	 * Read all shares of a poll based on the poll id and return list as array
114
	 * @NoAdminRequired
115
	 * @param integer $pollId
116
	 * @return DataResponse
117
	 */
118
	public function getShares($pollId) {
119
		$this->logger->alert('enter');
120
		if ($this->acl->setPollId($pollId)->getAllowEdit()) {
121
			try {
122
				$this->logger->alert('try loading shares');
123
				$shares = $this->mapper->findByPoll($pollId);
124
				return new DataResponse((array) $shares, Http::STATUS_OK);
125
126
			} catch (DoesNotExistException $e) {
127
				return new DataResponse($e, Http::STATUS_NOT_FOUND);
128
			}
129
130
		} else {
131
			$this->logger->alert('no access');
132
133
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
134
		}
135
136
	}
137
138
	/**
139
	 * write
140
	 * Write a new share to the db and returns the new share as array
141
	 * @NoAdminRequired
142
	 * @NoCSRFRequired
143
	 * @PublicPage
144
	 * @param int $pollId
145
	 * @param string $message
146
	 * @return DataResponse
147
	 */
148
	public function write($pollId, $share) {
149
		$this->acl->setPollId($pollId);
150
		if (!$this->acl->getAllowEdit()) {
151
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
152
		}
153
154
		$newShare = new Share();
155
		$newShare->setType($share['type']);
156
		$newShare->setPollId($share['pollId']);
157
		$newShare->setUserId($share['userId']);
158
		$newShare->setUserEmail($share['userEmail']);
159
		$newShare->setToken(\OC::$server->getSecureRandom()->generate(
160
			16,
161
			ISecureRandom::CHAR_DIGITS .
162
			ISecureRandom::CHAR_LOWER .
163
			ISecureRandom::CHAR_UPPER
164
		));
165
166
		try {
167
			$newShare = $this->mapper->insert($newShare);
168
			$this->mailService->sendInvitationMail($newShare->getToken());
169
			return new DataResponse($newShare, Http::STATUS_OK);
170
171
		} catch (\Exception $e) {
172
			return new DataResponse($e, Http::STATUS_CONFLICT);
173
		}
174
175
	}
176
177
	/**
178
	 * createPersonalShare
179
	 * Write a new share to the db and returns the new share as array
180
	 * @NoAdminRequired
181
	 * @NoCSRFRequired
182
	 * @PublicPage
183
	 * @param int $pollId
184
	 * @param string $message
185
	 * @return DataResponse
186
	 */
187
	public function createPersonalShare($token, $userName) {
188
189
		try {
190
			$userShare = $this->mapper->findByToken($token);
191
			if (!$this->systemController->validatePublicUsername($userShare->getPollId(), $userName)) {
192
				return new DataResponse(['message' => 'invalid userName'], Http::STATUS_CONFLICT);
193
			}
194
195
			if ($userShare->getType() === 'mail') {
196
197
				$userShare->setType('external');
198
				$userShare->setUserId($userName);
199
200
			} elseif ($userShare->getType() === 'public') {
201
202
				$userShare->setType('external');
203
				$userShare->setPollId(intval($userShare->getPollId()));
204
				$userShare->setUserId($userName);
205
				$userShare->setToken(\OC::$server->getSecureRandom()->generate(
206
					16,
207
					ISecureRandom::CHAR_DIGITS .
208
					ISecureRandom::CHAR_LOWER .
209
					ISecureRandom::CHAR_UPPER
210
				));
211
212
			} else {
213
				return new DataResponse(['message'=> 'Wrong share type: ' . $userShare->getType()], Http::STATUS_FORBIDDEN);
214
			}
215
216
			try {
217
				if ($token === $userShare->getToken()) {
218
					$userShare = $this->mapper->update($userShare);
219
				} else {
220
					$userShare = $this->mapper->insert($userShare);
221
				}
222
223
			} catch (\Exception $e) {
224
				return new DataResponse($e, Http::STATUS_CONFLICT);
225
			}
226
227
			return new DataResponse($userShare, Http::STATUS_OK);
228
229
		} catch (DoesNotExistException $e) {
230
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
231
		}
232
233
234
	}
235
236
237
	public function remove($share) {
238
		try {
239
			if ($this->acl->setPollId($share['pollId'])->getAllowEdit()) {
240
				$this->mapper->remove($share['id']);
241
242
				return new DataResponse(array(
243
					'action' => 'deleted',
244
					'shareId' => $share['id']
245
				), Http::STATUS_OK);
246
			} else {
247
				return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
248
			}
249
250
		} catch (Exception $e) {
251
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
252
		}
253
	}
254
}
255