Passed
Pull Request — master (#747)
by René
04:11
created

SystemController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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 DateTime;
27
use DateInterval;
28
use OCP\ILogger;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\DataResponse;
32
33
use OCP\IGroupManager;
34
use OCP\IUser;
35
use OCP\IUserManager;
36
use OCP\IConfig;
37
use OCP\IRequest;
38
use OCA\Polls\Db\Option;
39
use OCA\Polls\Db\Share;
40
use OCA\Polls\Db\ShareMapper;
41
use OCA\Polls\Db\Vote;
42
use OCA\Polls\Db\VoteMapper;
43
use OCA\Polls\Service\CalendarService;
44
45
class SystemController extends Controller {
46
47
	private $userId;
48
	private $logger;
49
	private $systemConfig;
50
	private $groupManager;
51
	private $userManager;
52
	private $voteMapper;
53
	private $shareMapper;
54
	private $calendarService;
55
56
57
	/**
58
	 * PageController constructor.
59
	 * @param string $appName
60
	 * @param $userId
61
	 * @param IRequest $request
62
	 * @param ILogger $logger
63
	 * @param IConfig $systemConfig
64
	 * @param IGroupManager $groupManager
65
	 * @param IUserManager $userManager
66
	 * @param VoteMapper $voteMapper
67
	 * @param ShareMapper $shareMapper
68
	 * @param CalendarService $calendarService,
69
	 */
70
	public function __construct(
71
		string $appName,
72
		$UserId,
73
		IRequest $request,
74
		ILogger $logger,
75
		IConfig $systemConfig,
76
		IGroupManager $groupManager,
77
		IUserManager $userManager,
78
		VoteMapper $voteMapper,
79
		ShareMapper $shareMapper,
80
		CalendarService $calendarService
81
	) {
82
		parent::__construct($appName, $request);
83
		$this->voteMapper = $voteMapper;
84
		$this->shareMapper = $shareMapper;
85
		$this->logger = $logger;
86
		$this->userId = $UserId;
87
		$this->systemConfig = $systemConfig;
88
		$this->groupManager = $groupManager;
89
		$this->userManager = $userManager;
90
		$this->calendarService = $calendarService;
91
	}
92
93
94
	/**
95
	 * findCalendarEvents
96
	 * @NoAdminRequired
97
	 * @param integer $from
98
	 * @param integer $to
99
	 * @return DataResponse
100
	 */
101
	public function findCalendarEvents($from, $to = null) {
102
103
		if (is_int($from)) {
0 ignored issues
show
introduced by
The condition is_int($from) is always true.
Loading history...
104
			$searchFrom = new DateTime();
105
			$searchFrom = $searchFrom->setTimestamp($from);
106
		} else {
107
			$searchFrom = new DateTime($from);
108
		}
109
110
111
		if (!$to) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
112
			$searchTo = clone $searchFrom;
113
			$searchTo = $searchTo->add(new DateInterval('PT1H'));
114
115
		} else if (is_int($to)) {
0 ignored issues
show
introduced by
The condition is_int($to) is always true.
Loading history...
116
				$searchTo = new DateTime();
117
				$searchTo = $searchTo->setTimestamp($to);
118
		} else {
119
			$searchTo = new DateTime($to);
120
		}
121
122
		$events = array_values($this->calendarService->getEvents($searchFrom, $searchTo));
123
		return new DataResponse($events, Http::STATUS_OK);
124
125
	}
126
127
	/**
128
	 * Get a list of NC users, groups and contacts
129
	 * @NoCSRFRequired
130
	 * @NoAdminRequired
131
	 * @param string $query
132
	 * @param bool $getGroups - search in groups
133
	 * @param bool $getUsers - search in site users
134
	 * @param bool $getContacts - search in contacs
135
	 * @param array $skipGroups - group names to skip in return array
136
	 * @param array $skipUsers - user names to skip in return array
137
	 * @return DataResponse
138
	 */
139
	public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $getContacts = true, $skipGroups = array(), $skipUsers = array()) {
140
		$list = array();
141
		if ($getGroups) {
142
			$groups = $this->groupManager->search($query);
143
			foreach ($groups as $group) {
144
				if (!in_array($group->getGID(), $skipGroups)) {
145
					$list[] = [
146
						'id' => $group->getGID(),
147
						'user' => $group->getGID(),
148
						'organisation' => '',
149
						'displayName' => $group->getGID(),
150
						'emailAddress' => '',
151
						'desc' => 'Group',
152
						'type' => 'group',
153
						'icon' => 'icon-group',
154
						'avatarURL' => '',
155
						'avatar' => '',
156
						'lastLogin' => '',
157
						'cloudId' => ''
158
159
					];
160
				}
161
			}
162
		}
163
164
		if ($getUsers) {
165
			$users = $this->userManager->searchDisplayName($query);
166
			foreach ($users as $user) {
167
				if (!in_array($user->getUID(), $skipUsers)) {
168
					$list[] = [
169
						'id' => $user->getUID(),
170
						'user' => $user->getUID(),
171
						'displayName' => $user->getDisplayName(),
172
						'organisation' => '',
173
						'emailAddress' => $user->getEMailAddress(),
174
						'desc' => 'User',
175
						'type' => 'user',
176
						'icon' => 'icon-user',
177
						'avatarURL' => '',
178
						'avatar' => '',
179
						'lastLogin' => $user->getLastLogin(),
180
						'cloudId' => $user->getCloudId()
181
					];
182
				}
183
			}
184
		}
185
186
		$contactsManager = \OC::$server->getContactsManager();
187
188
189
		if ($getContacts && $contactsManager->isEnabled()) {
190
			$contacts = $contactsManager->search($query, array('FN', 'EMAIL', 'ORG', 'CATEGORIES'));
191
192
			foreach ($contacts as $contact) {
193
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
194
195
					$emailAdresses = $contact['EMAIL'];
196
197
					if (!is_array($emailAdresses)) {
198
						$emailAdresses = array($emailAdresses);
199
					} else {
200
						// take the first eMail address for now
201
						$emailAdresses = array($emailAdresses[0]);
202
					}
203
204
					foreach ($emailAdresses as $emailAddress) {
205
						$list[] = [
206
							'id' => $contact['UID'],
207
							'user' => $contact['FN'],
208
							'displayName' => $contact['FN'],
209
							'organisation' => isset($contact['ORG']) ? $contact['ORG'] : '',
210
							'emailAddress' => $emailAddress,
211
							'desc' => 'Contact',
212
							'type' => 'contact',
213
							'icon' => 'icon-mail',
214
							'avatarURL' => '',
215
							'avatar' => isset($contact['PHOTO']) ? $contact['PHOTO'] : '',
216
							'lastLogin' => '',
217
							'cloudId' => ''
218
						];
219
					}
220
221
				}
222
			}
223
224
		}
225
226
		return new DataResponse([
227
			'siteusers' => $list
228
		], Http::STATUS_OK);
229
	}
230
231
	/**
232
	 * Validate it the user name is reservrd
233
	 * return false, if this username already exists as a user or as
234
	 * a participant of the poll
235
	 * @NoCSRFRequired
236
	 * @NoAdminRequired
237
	 * @PublicPage
238
	 * @return DataResponse
239
	 */
240
	public function validatePublicUsername($pollId, $userName) {
241
		$list = array();
242
243
		$groups = $this->groupManager->search('');
244
		foreach ($groups as $group) {
245
			$list[] = [
246
				'id' => $group->getGID(),
247
				'user' => $group->getGID(),
248
				'type' => 'group',
249
				'displayName' => $group->getGID(),
250
			];
251
		}
252
253
		$users = $this->userManager->searchDisplayName('');
254
		foreach ($users as $user) {
255
			$list[] = [
256
				'id' => $user->getUID(),
257
				'user' => $user->getUID(),
258
				'type' => 'user',
259
				'displayName' => $user->getDisplayName(),
260
			];
261
		}
262
263
		$votes = $this->voteMapper->findParticipantsByPoll($pollId);
264
		foreach ($votes as $vote) {
265
			if ($vote->getUserId() !== '' && $vote->getUserId() !== null) {
266
				$list[] = [
267
					'id' => $vote->getUserId(),
268
					'user' => $vote->getUserId(),
269
					'type' => 'participant',
270
					'displayName' => $vote->getUserId(),
271
				];
272
			}
273
		}
274
275
		$shares = $this->shareMapper->findByPoll($pollId);
276
		foreach ($shares as $share) {
277
			if ($share->getUserId() !== '' && $share->getUserId() !== null) {
278
				$list[] = [
279
					'id' => $share->getUserId(),
280
					'user' => $share->getUserId(),
281
					'type' => 'share',
282
					'displayName' => $share->getUserId(),
283
				];
284
			}
285
		}
286
287
		foreach ($list as $element) {
288
			if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) {
289
				return new DataResponse([
290
					'result' => false
291
				], Http::STATUS_FORBIDDEN);
292
			}
293
		}
294
295
		return new DataResponse([
296
			'result' => true,
297
			'list' => $list
298
		], Http::STATUS_OK);
299
	}
300
301
	public function getDisplayName() {
302
		$this->userManager = \OC::$server->getUserManager();
303
304
		if (\OC::$server->getUserManager()->get($this->userId) instanceof IUser) {
305
			return \OC::$server->getUserManager()->get($this->userId)->getDisplayName();
306
		} else {
307
			return $this->userId;
308
		}
309
	}
310
311
312
313
	//
314
	//
315
	// /**
316
	//  * Get some system informations
317
	//  * @NoAdminRequired
318
	//  * @return DataResponse
319
	//  */
320
	// public function getSystem() {
321
	// 	$data = array();
322
	//
323
	// 	$data['system'] = [
324
	// 		'versionArray' => \OCP\Util::getVersion(),
325
	// 		'version' => implode('.', \OCP\Util::getVersion()),
326
	// 		'vendor' => $this->getVendor(),
327
	// 		'language' => $this->systemConfig->getUserValue($this->userId, 'core', 'lang')
328
	// 	];
329
	//
330
	// 	return new DataResponse($data, Http::STATUS_OK);
331
	// }
332
}
333