Completed
Push — master ( adf3ba...9958e3 )
by René
02:10 queued 02:06
created

SystemController   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 138
c 2
b 0
f 0
dl 0
loc 255
ccs 0
cts 177
cp 0
rs 9.68
wmc 34

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
C validatePublicUsername() 0 59 12
C getSiteUsersAndGroups() 0 110 17
A isValidEmail() 0 2 2
A getDisplayName() 0 7 2
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
	 * Validate string as email address
87
	 * @NoAdminRequired
88
	 * @param string $query
89
	 * @return Boolval
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Controller\Boolval 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
	 private function isValidEmail($email) {
92
		 return (!preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $email)) ? FALSE : TRUE;
93
	 }
94
95
	/**
96
	 * Get a list of NC users, groups and contacts
97
	 * @NoCSRFRequired
98
	 * @NoAdminRequired
99
	 * @param string $query
100
	 * @param bool $getGroups - search in groups
101
	 * @param bool $getUsers - search in site users
102
	 * @param bool $getContacts - search in contacs
103
	 * @param array $skipGroups - group names to skip in return array
104
	 * @param array $skipUsers - user names to skip in return array
105
	 * @return DataResponse
106
	 */
107
	public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $getContacts = true, $getMail = false, $skipGroups = array(), $skipUsers = array()) {
0 ignored issues
show
Unused Code introduced by
The parameter $getMail 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

107
	public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $getContacts = true, /** @scrutinizer ignore-unused */ $getMail = false, $skipGroups = array(), $skipUsers = array()) {

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