Completed
Pull Request — master (#794)
by René
04:12
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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 19
ccs 0
cts 19
cp 0
crap 2
rs 10

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
class SystemController extends Controller {
42
43
	private $userId;
44
	private $logger;
45
	private $systemConfig;
46
	private $groupManager;
47
	private $userManager;
48
	private $voteMapper;
49
	private $shareMapper;
50
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
			foreach ($contacts as $contact) {
151
				if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) {
152
153
					$emailAdresses = $contact['EMAIL'];
154
155
					if (!is_array($emailAdresses)) {
156
						$emailAdresses = array($emailAdresses);
157
					} else {
158
						// take the first eMail address for now
159
						$emailAdresses = array($emailAdresses[0]);
160
					}
161
162
					foreach ($emailAdresses as $emailAddress) {
163
						$list[] = [
164
							'id' => $contact['UID'],
165
							'user' => $contact['FN'],
166
							'displayName' => $contact['FN'],
167
							'organisation' => isset($contact['ORG']) ? $contact['ORG'] : '' ,
168
							'emailAddress' => $emailAddress,
169
							'desc' => 'Contact',
170
							'type' => 'contact',
171
							'icon' => 'icon-mail',
172
							'avatarURL' => '',
173
							'avatar' => isset($contact['PHOTO']) ? $contact['PHOTO'] : '',
174
							'lastLogin' => '',
175
							'cloudId' => ''
176
						];
177
					}
178
179
				}
180
			}
181
182
		}
183
184
		return new DataResponse([
185
			'siteusers' => $list
186
		], Http::STATUS_OK);
187
	}
188
189
	/**
190
	 * Validate it the user name is reservrd
191
	 * return false, if this username already exists as a user or as
192
	 * a participant of the poll
193
	 * @NoCSRFRequired
194
	 * @NoAdminRequired
195
	 * @PublicPage
196
	 * @return DataResponse
197
	 */
198
	public function validatePublicUsername($pollId, $userName) {
199
		$list = array();
200
201
		$groups = $this->groupManager->search('');
202
		foreach ($groups as $group) {
203
			$list[] = [
204
				'id' => $group->getGID(),
205
				'user' => $group->getGID(),
206
				'type' => 'group',
207
				'displayName' => $group->getGID(),
208
			];
209
		}
210
211
		$users = $this->userManager->searchDisplayName('');
212
		foreach ($users as $user) {
213
			$list[] = [
214
				'id' => $user->getUID(),
215
				'user' => $user->getUID(),
216
				'type' => 'user',
217
				'displayName' => $user->getDisplayName(),
218
			];
219
		}
220
221
		$votes = $this->voteMapper->findParticipantsByPoll($pollId);
222
		foreach ($votes as $vote) {
223
			if ($vote->getUserId() !== '' && $vote->getUserId() !== null) {
224
				$list[] = [
225
					'id' => $vote->getUserId(),
226
					'user' => $vote->getUserId(),
227
					'type' => 'participant',
228
					'displayName' => $vote->getUserId(),
229
				];
230
			}
231
		}
232
233
		$shares = $this->shareMapper->findByPoll($pollId);
234
		foreach ($shares as $share) {
235
			if ($share->getUserId() !== '' && $share->getUserId() !== null) {
236
				$list[] = [
237
					'id' => $share->getUserId(),
238
					'user' => $share->getUserId(),
239
					'type' => 'share',
240
					'displayName' => $share->getUserId(),
241
				];
242
			}
243
		}
244
245
		foreach ($list as $element) {
246
			if (strtolower(trim($userName)) === strtolower(trim($element['id'])) || strtolower(trim($userName)) === strtolower(trim($element['displayName']))) {
247
				return new DataResponse([
248
					'result' => false
249
				], Http::STATUS_FORBIDDEN);
250
			}
251
		}
252
253
		return new DataResponse([
254
			'result' => true,
255
			'list' => $list
256
		], Http::STATUS_OK);
257
	}
258
259
	public function getDisplayName() {
260
		$this->userManager = \OC::$server->getUserManager();
261
262
		if (\OC::$server->getUserManager()->get($this->userId) instanceof IUser) {
263
			return \OC::$server->getUserManager()->get($this->userId)->getDisplayName();
264
		} else {
265
			return $this->userId;
266
		}
267
	}
268
269
270
271
	//
272
	//
273
	// /**
274
	//  * Get some system informations
275
	//  * @NoAdminRequired
276
	//  * @return DataResponse
277
	//  */
278
	// public function getSystem() {
279
	// 	$data = array();
280
	//
281
	// 	$data['system'] = [
282
	// 		'versionArray' => \OCP\Util::getVersion(),
283
	// 		'version' => implode('.', \OCP\Util::getVersion()),
284
	// 		'vendor' => $this->getVendor(),
285
	// 		'language' => $this->systemConfig->getUserValue($this->userId, 'core', 'lang')
286
	// 	];
287
	//
288
	// 	return new DataResponse($data, Http::STATUS_OK);
289
	// }
290
}
291