Completed
Pull Request — master (#1128)
by René
04:25
created

SystemService::getSiteUsersAndGroups()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 35
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 14
dl 0
loc 35
ccs 0
cts 27
cp 0
rs 8.4444
c 2
b 1
f 1
cc 8
nc 33
nop 8
crap 72

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\Service;
25
26
use OCA\Polls\Exceptions\NotAuthorizedException;
27
use OCA\Polls\Exceptions\TooShortException;
28
use OCA\Polls\Exceptions\InvalidUsernameException;
29
use OCA\Polls\Exceptions\InvalidEmailAddress;
30
31
use OCA\Polls\Db\Share;
32
use OCA\Polls\Db\ShareMapper;
33
use OCA\Polls\Db\VoteMapper;
34
use OCA\Polls\Model\Circle;
35
use OCA\Polls\Model\Contact;
36
use OCA\Polls\Model\ContactGroup;
37
use OCA\Polls\Model\Email;
38
use OCA\Polls\Model\Group;
39
use OCA\Polls\Model\User;
40
41
class SystemService {
42
43
	/** @var VoteMapper */
44
	private $voteMapper;
45
46
	/** @var ShareMapper */
47
	private $shareMapper;
48
49
	/**
50
	 * SystemService constructor.
51
	 * @param VoteMapper $voteMapper
52
	 * @param ShareMapper $shareMapper
53
	 */
54
	public function __construct(
55
		VoteMapper $voteMapper,
56
		ShareMapper $shareMapper
57
	) {
58
		$this->voteMapper = $voteMapper;
59
		$this->shareMapper = $shareMapper;
60
	}
61
62
	/**
63
	 * Validate string as email address
64
	 * @NoAdminRequired
65
	 * @param string $emailAddress
66
	 * @return bool
67
	 */
68
	private static function isValidEmail($emailAddress) {
69
		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,}))$/', $emailAddress)) ? false : true;
70
	}
71
72
	/**
73
	 * Validate email address and throw an exception
74
	 * return true, if email address is a valid
75
	 * @NoAdminRequired
76
	 * @return Boolean
77
	 * @throws InvalidEmailAddress
78
	 */
79
	public static function validateEmailAddress($emailAddress) {
80
		if (!self::isValidEmail($emailAddress)) {
81
			throw new InvalidEmailAddress;
82
		}
83
		return true;
84
	}
85
86
	/**
87
	 * Get a list of users
88
	 * @NoAdminRequired
89
	 * @param string $query
90
	 * @param array $skip - usernames to skip in return array
91
	 * @return User[]
92
	 */
93
	public static function getSiteUsers($query = '', $skip = []) {
94
		$users = [];
95
		foreach (\OC::$server->getUserManager()->searchDisplayName($query) as $user) {
96
			if (!in_array($user->getUID(), $skip) && $user->isEnabled()) {
97
				$users[] = new User($user->getUID());
98
			}
99
		}
100
		return $users;
101
	}
102
103
	/**
104
	 * Get a combined list of users, groups, circles, contact groups and contacts
105
	 * @NoAdminRequired
106
	 * @param string $query
107
	 * @param bool $getGroups - search in groups
108
	 * @param bool $getUsers - search in site users
109
	 * @param bool $getContacts - search in contacs
110
	 * @param bool $getContactGroups - search in contacs
111
	 * @param array $skipGroups - group names to skip in return array
112
	 * @param array $skipUsers - user names to skip in return array
113
	 * @return User[]
114
	 */
115
	public function getSiteUsersAndGroups(
116
		$query = '',
117
		$getGroups = true,
118
		$getUsers = true,
119
		$getContacts = true,
120
		$getContactGroups = true,
0 ignored issues
show
Unused Code introduced by
The parameter $getContactGroups 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

120
		/** @scrutinizer ignore-unused */ $getContactGroups = true,

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...
121
		$getMail = false,
122
		$skipGroups = [],
123
		$skipUsers = []
124
	) {
125
		$list = [];
126
		if ($query !== '') {
127
			if ($getMail && self::isValidEmail($query)) {
128
				$list[] = new Email($query);
129
			}
130
131
			if ($getGroups) {
132
				$list = array_merge($list, Group::search($query, $skipGroups));
133
			}
134
135
			if ($getUsers) {
136
				$list = array_merge($list, User::search($query, $skipUsers));
137
			}
138
139
			if ($getContacts) {
140
				$list = array_merge($list, Contact::search($query));
141
			}
142
143
			if ($getContacts) {
144
				$list = array_merge($list, ContactGroup::search($query));
145
			}
146
			$list = array_merge($list, Circle::search($query));
147
		}
148
149
		return $list;
150
	}
151
152
	/**
153
	 * Validate it the user name is reservrd
154
	 * return false, if this username already exists as a user or as
155
	 * a participant of the poll
156
	 * @NoAdminRequired
157
	 * @return Boolean
158
	 * @throws NotAuthorizedException
159
	 * @throws TooShortException
160
	 * @throws InvalidUsernameException
161
	 */
162
	public function validatePublicUsername($pollId, $userName, $token) {
163
		$userName = strtolower(trim($userName));
164
165
		// return forbidden, if $pollId does not match the share's pollId, force int compare
166
		if (intval($this->shareMapper->findByToken($token)->getPollId()) !== intVal($pollId)) {
167
			throw new NotAuthorizedException;
168
		}
169
170
		// return forbidden, if the length of the userame is lower than 3 characters
171
		if (strlen($userName) < 3) {
172
			return new TooShortException('Username must have at least 3 characters');
173
		}
174
175
		// get all groups
176
		foreach (Group::search() as $group) {
177
			if ($userName === strtolower(trim($group->getId()))
178
				|| $userName === strtolower(trim($group->getDisplayName()))) {
179
				throw new InvalidUsernameException;
180
			}
181
		}
182
183
		// get all users
184
		foreach (User::search() as $user) {
185
			if ($userName === strtolower(trim($user->getId()))
186
				|| $userName === strtolower(trim($user->getDisplayName()))) {
187
				throw new InvalidUsernameException;
188
			}
189
		}
190
191
		// get all participants
192
		foreach ($this->voteMapper->findParticipantsByPoll($pollId) as $vote) {
193
			if ($vote->getUserId()) {
194
				if ($userName === strtolower(trim($vote->getUserId()))) {
195
					throw new InvalidUsernameException;
196
				}
197
			}
198
		}
199
200
		// get all shares for this poll
201
		foreach ($this->shareMapper->findByPoll($pollId) as $share) {
202
			if ($share->getUserId() && $share->getType() !== Circle::TYPE) {
203
				if ($userName === strtolower(trim($share->getUserId()))
204
					|| $userName === strtolower(trim($share->getDisplayName()))) {
205
					throw new InvalidUsernameException;
206
				}
207
			}
208
		}
209
		// return true, if username is allowed
210
		return true;
211
	}
212
}
213