Passed
Push — master ( bffb34...3e5174 )
by Blizzz
12:26 queued 12s
created

Group_Proxy::inGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christopher Schäpers <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin McCorkell <[email protected]>
10
 * @author Vinicius Cubas Brand <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OCA\User_LDAP;
29
30
use OCP\Group\Backend\IGetDisplayNameBackend;
31
32
class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend {
33
	private $backends = array();
34
	private $refBackend = null;
35
36
	/**
37
	 * Constructor
38
	 * @param string[] $serverConfigPrefixes array containing the config Prefixes
39
	 */
40
	public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) {
41
		parent::__construct($ldap);
42
		foreach($serverConfigPrefixes as $configPrefix) {
43
			$this->backends[$configPrefix] =
44
				new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix), $groupPluginManager);
45
			if(is_null($this->refBackend)) {
46
				$this->refBackend = &$this->backends[$configPrefix];
47
			}
48
		}
49
	}
50
51
	/**
52
	 * Tries the backends one after the other until a positive result is returned from the specified method
53
	 * @param string $gid the gid connected to the request
54
	 * @param string $method the method of the group backend that shall be called
55
	 * @param array $parameters an array of parameters to be passed
56
	 * @return mixed, the result of the method or false
57
	 */
58
	protected function walkBackends($gid, $method, $parameters) {
59
		$cacheKey = $this->getGroupCacheKey($gid);
60
		foreach($this->backends as $configPrefix => $backend) {
61
			if($result = call_user_func_array(array($backend, $method), $parameters)) {
62
				$this->writeToCache($cacheKey, $configPrefix);
63
				return $result;
64
			}
65
		}
66
		return false;
67
	}
68
69
	/**
70
	 * Asks the backend connected to the server that supposely takes care of the gid from the request.
71
	 * @param string $gid the gid connected to the request
72
	 * @param string $method the method of the group backend that shall be called
73
	 * @param array $parameters an array of parameters to be passed
74
	 * @param mixed $passOnWhen the result matches this variable
75
	 * @return mixed, the result of the method or false
76
	 */
77
	protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) {
78
		$cacheKey = $this->getGroupCacheKey($gid);
79
		$prefix = $this->getFromCache($cacheKey);
80
		//in case the uid has been found in the past, try this stored connection first
81
		if(!is_null($prefix)) {
82
			if(isset($this->backends[$prefix])) {
83
				$result = call_user_func_array(array($this->backends[$prefix], $method), $parameters);
84
				if($result === $passOnWhen) {
85
					//not found here, reset cache to null if group vanished
86
					//because sometimes methods return false with a reason
87
					$groupExists = call_user_func_array(
88
						array($this->backends[$prefix], 'groupExists'),
89
						array($gid)
90
					);
91
					if(!$groupExists) {
92
						$this->writeToCache($cacheKey, null);
93
					}
94
				}
95
				return $result;
96
			}
97
		}
98
		return false;
99
	}
100
101
	/**
102
	 * is user in group?
103
	 * @param string $uid uid of the user
104
	 * @param string $gid gid of the group
105
	 * @return bool
106
	 *
107
	 * Checks whether the user is member of a group or not.
108
	 */
109
	public function inGroup($uid, $gid) {
110
		return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
111
	}
112
113
	/**
114
	 * Get all groups a user belongs to
115
	 * @param string $uid Name of the user
116
	 * @return string[] with group names
117
	 *
118
	 * This function fetches all groups a user belongs to. It does not check
119
	 * if the user exists at all.
120
	 */
121
	public function getUserGroups($uid) {
122
		$groups = array();
123
124
		foreach($this->backends as $backend) {
125
			$backendGroups = $backend->getUserGroups($uid);
126
			if (is_array($backendGroups)) {
127
				$groups = array_merge($groups, $backendGroups);
128
			}
129
		}
130
131
		return $groups;
132
	}
133
134
	/**
135
	 * get a list of all users in a group
136
	 * @return string[] with user ids
137
	 */
138
	public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
139
		$users = array();
140
141
		foreach($this->backends as $backend) {
142
			$backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
143
			if (is_array($backendUsers)) {
144
				$users = array_merge($users, $backendUsers);
145
			}
146
		}
147
148
		return $users;
149
	}
150
151
	/**
152
	 * @param string $gid
153
	 * @return bool
154
	 */
155
	public function createGroup($gid) {
156
		return $this->handleRequest(
157
			$gid, 'createGroup', array($gid));
158
	}
159
160
	/**
161
	 * delete a group
162
	 * @param string $gid gid of the group to delete
163
	 * @return bool
164
	 */
165
	public function deleteGroup($gid) {
166
		return $this->handleRequest(
167
			$gid, 'deleteGroup', array($gid));
168
	}
169
170
	/**
171
	 * Add a user to a group
172
	 * @param string $uid Name of the user to add to group
173
	 * @param string $gid Name of the group in which add the user
174
	 * @return bool
175
	 *
176
	 * Adds a user to a group.
177
	 */
178
	public function addToGroup($uid, $gid) {
179
		return $this->handleRequest(
180
			$gid, 'addToGroup', array($uid, $gid));
181
	}
182
183
	/**
184
	 * Removes a user from a group
185
	 * @param string $uid Name of the user to remove from group
186
	 * @param string $gid Name of the group from which remove the user
187
	 * @return bool
188
	 *
189
	 * removes the user from a group.
190
	 */
191
	public function removeFromGroup($uid, $gid) {
192
		return $this->handleRequest(
193
			$gid, 'removeFromGroup', array($uid, $gid));
194
	}
195
196
	/**
197
	 * returns the number of users in a group, who match the search term
198
	 * @param string $gid the internal group name
199
	 * @param string $search optional, a search string
200
	 * @return int|bool
201
	 */
202
	public function countUsersInGroup($gid, $search = '') {
203
		return $this->handleRequest(
204
			$gid, 'countUsersInGroup', array($gid, $search));
205
	}
206
207
	/**
208
	 * get an array with group details
209
	 * @param string $gid
210
	 * @return array|false
211
	 */
212
	public function getGroupDetails($gid) {
213
		return $this->handleRequest(
214
			$gid, 'getGroupDetails', array($gid));
215
	}
216
217
	/**
218
	 * get a list of all groups
219
	 * @return string[] with group names
220
	 *
221
	 * Returns a list with all groups
222
	 */
223
	public function getGroups($search = '', $limit = -1, $offset = 0) {
224
		$groups = array();
225
226
		foreach($this->backends as $backend) {
227
			$backendGroups = $backend->getGroups($search, $limit, $offset);
228
			if (is_array($backendGroups)) {
229
				$groups = array_merge($groups, $backendGroups);
230
			}
231
		}
232
233
		return $groups;
234
	}
235
236
	/**
237
	 * check if a group exists
238
	 * @param string $gid
239
	 * @return bool
240
	 */
241
	public function groupExists($gid) {
242
		return $this->handleRequest($gid, 'groupExists', array($gid));
243
	}
244
245
	/**
246
	 * Check if backend implements actions
247
	 * @param int $actions bitwise-or'ed actions
248
	 * @return boolean
249
	 *
250
	 * Returns the supported actions as int to be
251
	 * compared with \OCP\GroupInterface::CREATE_GROUP etc.
252
	 */
253
	public function implementsActions($actions) {
254
		//it's the same across all our user backends obviously
255
		return $this->refBackend->implementsActions($actions);
256
	}
257
258
	/**
259
	 * Return access for LDAP interaction.
260
	 * @param string $gid
261
	 * @return Access instance of Access for LDAP interaction
262
	 */
263
	public function getLDAPAccess($gid) {
264
		return $this->handleRequest($gid, 'getLDAPAccess', [$gid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->handleRequ...APAccess', array($gid)) could also return false which is incompatible with the documented return type OCA\User_LDAP\Access. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
265
	}
266
267
	/**
268
	 * Return a new LDAP connection for the specified group.
269
	 * The connection needs to be closed manually.
270
	 * @param string $gid
271
	 * @return resource of the LDAP connection
272
	 */
273
	public function getNewLDAPConnection($gid) {
274
		return $this->handleRequest($gid, 'getNewLDAPConnection', array($gid));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->handleRequ...nnection', array($gid)) could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
275
	}
276
277
	public function getDisplayName(string $gid): string {
278
		return $this->handleRequest($gid, 'getDisplayName', [$gid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->handleRequ...playName', array($gid)) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
279
	}
280
}
281