Passed
Pull Request — master (#1016)
by René
04:03
created

SystemController::getDisplayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
40
class SystemController extends Controller {
41
42
	/** @var string */
43
	private $userId;
44
45
	/** @var IConfig */
46
	private $systemConfig;
47
48
	/** @var IGroupManager */
49
	private $groupManager;
50
51
	/** @var IUserManager */
52
	private $userManager;
53
54
	/** @var VoteMapper */
55
	private $voteMapper;
56
57
	/** @var ShareMapper */
58
	private $shareMapper;
59
60
	/**
61
	 * SystemController constructor.
62
	 * @param string $appName
63
	 * @param $userId
64
	 * @param IRequest $request
65
	 * @param IConfig $systemConfig
66
	 * @param IGroupManager $groupManager
67
	 * @param IUserManager $userManager
68
	 * @param VoteMapper $voteMapper
69
	 * @param ShareMapper $shareMapper
70
	 */
71
	public function __construct(
72
		string $appName,
73
		$userId,
74
		IRequest $request,
75
		IConfig $systemConfig,
76
		IGroupManager $groupManager,
77
		IUserManager $userManager,
78
		VoteMapper $voteMapper,
79
		ShareMapper $shareMapper
80
	) {
81
		parent::__construct($appName, $request);
82
		$this->voteMapper = $voteMapper;
83
		$this->shareMapper = $shareMapper;
84
		$this->userId = $userId;
85
		$this->systemConfig = $systemConfig;
86
		$this->groupManager = $groupManager;
87
		$this->userManager = $userManager;
88
	}
89
90
	/**
91
	 * Validate string as email address
92
	 * @NoAdminRequired
93
	 * @param string $query
94
	 * @return bool
95
	 */
96
	 private function isValidEmail($email) {
97
		 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;
98
	 }
99
100
	/**
101
	 * Get a list of NC users, groups and contacts
102
	 * @NoAdminRequired
103
	 * @NoCSRFRequired
104
	 * @param string $query
105
	 * @param bool $getGroups - search in groups
106
	 * @param bool $getUsers - search in site users
107
	 * @param bool $getContacts - search in contacs
108
	 * @param array $skipGroups - group names to skip in return array
109
	 * @param array $skipUsers - user names to skip in return array
110
	 * @return DataResponse
111
	 */
112
	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

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