Completed
Push — master ( 0da6ae...09fb36 )
by René
06:04 queued 01:33
created

SystemController::__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 2
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 2
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 OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http;
28
use OCP\AppFramework\Http\DataResponse;
29
30
use OCP\IGroupManager;
31
use OCP\IUser;
32
use OCP\IUserManager;
33
use OCP\IConfig;
34
use OCP\IRequest;
35
use OCA\Polls\Db\Share;
36
use OCA\Polls\Db\ShareMapper;
37
use OCA\Polls\Db\Vote;
38
use OCA\Polls\Db\VoteMapper;
39
use OCP\ILogger;
40
41
42
class SystemController extends Controller {
43
44
	private $userId;
45
	private $logger;
46
	private $systemConfig;
47
	private $groupManager;
48
	private $userManager;
49
	private $voteMapper;
50
	private $shareMapper;
51
52
	/**
53
	 * PageController constructor.
54
	 * @param string $appName
55
	 * @param $userId
56
	 * @param IRequest $request
57
	 * @param ILogger $logger
58
	 * @param IConfig $systemConfig
59
	 * @param IGroupManager $groupManager
60
	 * @param IUserManager $userManager
61
	 * @param VoteMapper $voteMapper
62
	 * @param ShareMapper $shareMapper
63
	 */
64
	public function __construct(
65
		string $appName,
66
		$UserId,
67
		IRequest $request,
68
		ILogger $logger,
69
		IConfig $systemConfig,
70
		IGroupManager $groupManager,
71
		IUserManager $userManager,
72
		VoteMapper $voteMapper,
73
		ShareMapper $shareMapper
74
	) {
75
		parent::__construct($appName, $request);
76
		$this->voteMapper = $voteMapper;
77
		$this->shareMapper = $shareMapper;
78
		$this->logger = $logger;
79
		$this->userId = $UserId;
80
		$this->systemConfig = $systemConfig;
81
		$this->groupManager = $groupManager;
82
		$this->userManager = $userManager;
83
	}
84
85
	/**
86
	 * Get a list of NC users, groups and contacts
87
	 * @NoCSRFRequired
88
	 * @NoAdminRequired
89
	 * @param string $query
90
	 * @param bool $getGroups - search in groups
91
	 * @param bool $getUsers - search in site users
92
	 * @param bool $getContacts - search in contacs
93
	 * @param array $skipGroups - group names to skip in return array
94
	 * @param array $skipUsers - user names to skip in return array
95
	 * @return DataResponse
96
	 */
97
	public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $getContacts = true, $skipGroups = array(), $skipUsers = array()) {
98
		$list = array();
99
		if ($getGroups) {
100
			$groups = $this->groupManager->search($query);
101
			foreach ($groups as $group) {
102
				if (!in_array($group->getGID(), $skipGroups)) {
103
					$list[] = [
104
						'id' => $group->getGID(),
105
						'user' => $group->getGID(),
106
						'organisation' => '',
107
						'displayName' => $group->getGID(),
108
						'emailAddress' => '',
109
						'desc' => 'Group',
110
						'type' => 'group',
111
						'icon' => 'icon-group',
112
						'avatarURL' => '',
113
						'avatar' => '',
114
						'lastLogin' => '',
115
						'cloudId' => ''
116
117
					];
118
				}
119
			}
120
		}
121
122
		if ($getUsers) {
123
			$users = $this->userManager->searchDisplayName($query);
124
			foreach ($users as $user) {
125
				if (!in_array($user->getUID(), $skipUsers)) {
126
					$list[] = [
127
						'id' => $user->getUID(),
128
						'user' => $user->getUID(),
129
						'displayName' => $user->getDisplayName(),
130
						'organisation' => '',
131
						'emailAddress' => $user->getEMailAddress(),
132
						'desc' => 'User',
133
						'type' => 'user',
134
						'icon' => 'icon-user',
135
						'avatarURL' => '',
136
						'avatar' => '',
137
						'lastLogin' => $user->getLastLogin(),
138
						'cloudId' => $user->getCloudId()
139
					];
140
				}
141
			}
142
		}
143
144
		$contactsManager = \OC::$server->getContactsManager();
145
146
147
		if ($getContacts && $contactsManager->isEnabled()) {
148
			$contacts = $contactsManager->search($query, array('FN', 'EMAIL', 'ORG', 'CATEGORIES'));
149
150
			// $this->logger->error(json_encode($contacts));
151
			foreach ($contacts as $contact) {
152
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
153
154
					$emailAdresses = $contact['EMAIL'];
155
156
					if (!is_array($emailAdresses)) {
157
						$emailAdresses = array($emailAdresses);
158
					} else {
159
						// take the first eMail address for now
160
						$emailAdresses = array($emailAdresses[0]);
161
					}
162
163
					foreach ($emailAdresses as $emailAddress) {
164
						$list[] = [
165
							'id' => $contact['UID'],
166
							'user' => $contact['FN'],
167
							'displayName' => $contact['FN'],
168
							'organisation' => $contact['ORG'],
169
							'emailAddress' => $emailAddress,
170
							'desc' => 'Contact',
171
							'type' => 'contact',
172
							'icon' => 'icon-mail',
173
							'avatarURL' => '',
174
							'avatar' => $contact['PHOTO'],
175
							'lastLogin' => '',
176
							'cloudId' => ''
177
						];
178
					}
179
180
				}
181
			}
182
183
		}
184
185
		return new DataResponse([
186
			'siteusers' => $list
187
		], Http::STATUS_OK);
188
	}
189
190
	/**
191
	 * Validate it the user name is reservrd
192
	 * return false, if this username already exists as a user or as
193
	 * a participant of the poll
194
	 * @NoCSRFRequired
195
	 * @NoAdminRequired
196
	 * @PublicPage
197
	 * @return DataResponse
198
	 */
199
	public function validatePublicUsername($pollId, $userName) {
200
		$list = array();
201
202
		$groups = $this->groupManager->search('');
203
		foreach ($groups as $group) {
204
			$list[] = [
205
				'id' => $group->getGID(),
206
				'user' => $group->getGID(),
207
				'type' => 'group',
208
				'displayName' => $group->getGID(),
209
			];
210
		}
211
212
		$users = $this->userManager->searchDisplayName('');
213
		foreach ($users as $user) {
214
			$list[] = [
215
				'id' => $user->getUID(),
216
				'user' => $user->getUID(),
217
				'type' => 'user',
218
				'displayName' => $user->getDisplayName(),
219
			];
220
		}
221
222
		$votes = $this->voteMapper->findParticipantsByPoll($pollId);
223
		foreach ($votes as $vote) {
224
			if ($vote->getUserId() !== '' && $vote->getUserId() !== null ) {
225
				$list[] = [
226
					'id' => $vote->getUserId(),
227
					'user' => $vote->getUserId(),
228
					'type' => 'participant',
229
					'displayName' => $vote->getUserId(),
230
				];
231
			}
232
		}
233
234
		$shares = $this->shareMapper->findByPoll($pollId);
235
		foreach ($shares as $share) {
236
			if ($share->getUserId() !== '' && $share->getUserId() !== null ) {
237
				$list[] = [
238
					'id' => $share->getUserId(),
239
					'user' => $share->getUserId(),
240
					'type' => 'share',
241
					'displayName' => $share->getUserId(),
242
				];
243
			}
244
		}
245
246
		foreach ($list as $element) {
247
			if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) {
248
				return new DataResponse([
249
					'result' => false
250
				], Http::STATUS_FORBIDDEN);
251
			}
252
		}
253
254
		return new DataResponse([
255
			'result' => true,
256
			'list' => $list
257
		], Http::STATUS_OK);
258
	}
259
	//
260
	//
261
	// /**
262
	//  * Get some system informations
263
	//  * @NoAdminRequired
264
	//  * @return DataResponse
265
	//  */
266
	// public function getSystem() {
267
	// 	$data = array();
268
	//
269
	// 	$data['system'] = [
270
	// 		'versionArray' => \OCP\Util::getVersion(),
271
	// 		'version' => implode('.', \OCP\Util::getVersion()),
272
	// 		'vendor' => $this->getVendor(),
273
	// 		'language' => $this->systemConfig->getUserValue($this->userId, 'core', 'lang')
274
	// 	];
275
	//
276
	// 	return new DataResponse($data, Http::STATUS_OK);
277
	// }
278
}
279