Passed
Push — master ( 84a353...212138 )
by Blizzz
12:21 queued 10s
created

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