Passed
Push — master ( e6ef09...54cffe )
by Roeland
49:25 queued 37:20
created

Group_Proxy::createGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
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\INamedBackend;
32
use OCP\Group\Backend\IGetDisplayNameBackend;
33
34
class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend {
35
	private $backends = [];
36
	private $refBackend = null;
37
38
	public function __construct(Helper $helper, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) {
39
		parent::__construct($ldap);
40
		$serverConfigPrefixes = $helper->getServerConfigurationPrefixes(true);
41
		foreach ($serverConfigPrefixes as $configPrefix) {
42
			$this->backends[$configPrefix] =
43
				new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix), $groupPluginManager);
44
			if (is_null($this->refBackend)) {
45
				$this->refBackend = &$this->backends[$configPrefix];
46
			}
47
		}
48
	}
49
50
	/**
51
	 * Tries the backends one after the other until a positive result is returned from the specified method
52
	 *
53
	 * @param string $id 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($id, $method, $parameters) {
59
		$gid = $id;
60
		$cacheKey = $this->getGroupCacheKey($gid);
61
		foreach ($this->backends as $configPrefix => $backend) {
62
			if ($result = call_user_func_array([$backend, $method], $parameters)) {
63
				if (!$this->isSingleBackend()) {
64
					$this->writeToCache($cacheKey, $configPrefix);
65
				}
66
				return $result;
67
			}
68
		}
69
		return false;
70
	}
71
72
	/**
73
	 * Asks the backend connected to the server that supposely takes care of the gid from the request.
74
	 *
75
	 * @param string $id the gid connected to the request
76
	 * @param string $method the method of the group backend that shall be called
77
	 * @param array $parameters an array of parameters to be passed
78
	 * @param mixed $passOnWhen the result matches this variable
79
	 * @return mixed the result of the method or false
80
	 */
81
	protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) {
82
		$gid = $id;
83
		$cacheKey = $this->getGroupCacheKey($gid);
84
		$prefix = $this->getFromCache($cacheKey);
85
		//in case the uid has been found in the past, try this stored connection first
86
		if (!is_null($prefix)) {
87
			if (isset($this->backends[$prefix])) {
88
				$result = call_user_func_array([$this->backends[$prefix], $method], $parameters);
89
				if ($result === $passOnWhen) {
90
					//not found here, reset cache to null if group vanished
91
					//because sometimes methods return false with a reason
92
					$groupExists = call_user_func_array(
93
						[$this->backends[$prefix], 'groupExists'],
94
						[$gid]
95
					);
96
					if (!$groupExists) {
97
						$this->writeToCache($cacheKey, null);
98
					}
99
				}
100
				return $result;
101
			}
102
		}
103
		return false;
104
	}
105
106
	protected function activeBackends(): int {
107
		return count($this->backends);
108
	}
109
110
	/**
111
	 * is user in group?
112
	 *
113
	 * @param string $uid uid of the user
114
	 * @param string $gid gid of the group
115
	 * @return bool
116
	 *
117
	 * Checks whether the user is member of a group or not.
118
	 */
119
	public function inGroup($uid, $gid) {
120
		return $this->handleRequest($gid, 'inGroup', [$uid, $gid]);
121
	}
122
123
	/**
124
	 * Get all groups a user belongs to
125
	 *
126
	 * @param string $uid Name of the user
127
	 * @return string[] with group names
128
	 *
129
	 * This function fetches all groups a user belongs to. It does not check
130
	 * if the user exists at all.
131
	 */
132
	public function getUserGroups($uid) {
133
		$groups = [];
134
135
		foreach ($this->backends as $backend) {
136
			$backendGroups = $backend->getUserGroups($uid);
137
			if (is_array($backendGroups)) {
138
				$groups = array_merge($groups, $backendGroups);
139
			}
140
		}
141
142
		return $groups;
143
	}
144
145
	/**
146
	 * get a list of all users in a group
147
	 *
148
	 * @return string[] with user ids
149
	 */
150
	public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
151
		$users = [];
152
153
		foreach ($this->backends as $backend) {
154
			$backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
155
			if (is_array($backendUsers)) {
156
				$users = array_merge($users, $backendUsers);
157
			}
158
		}
159
160
		return $users;
161
	}
162
163
	/**
164
	 * @param string $gid
165
	 * @return bool
166
	 */
167
	public function createGroup($gid) {
168
		return $this->handleRequest(
169
			$gid, 'createGroup', [$gid]);
170
	}
171
172
	/**
173
	 * delete a group
174
	 *
175
	 * @param string $gid gid of the group to delete
176
	 * @return bool
177
	 */
178
	public function deleteGroup($gid) {
179
		return $this->handleRequest(
180
			$gid, 'deleteGroup', [$gid]);
181
	}
182
183
	/**
184
	 * Add a user to a group
185
	 *
186
	 * @param string $uid Name of the user to add to group
187
	 * @param string $gid Name of the group in which add the user
188
	 * @return bool
189
	 *
190
	 * Adds a user to a group.
191
	 */
192
	public function addToGroup($uid, $gid) {
193
		return $this->handleRequest(
194
			$gid, 'addToGroup', [$uid, $gid]);
195
	}
196
197
	/**
198
	 * Removes a user from a group
199
	 *
200
	 * @param string $uid Name of the user to remove from group
201
	 * @param string $gid Name of the group from which remove the user
202
	 * @return bool
203
	 *
204
	 * removes the user from a group.
205
	 */
206
	public function removeFromGroup($uid, $gid) {
207
		return $this->handleRequest(
208
			$gid, 'removeFromGroup', [$uid, $gid]);
209
	}
210
211
	/**
212
	 * returns the number of users in a group, who match the search term
213
	 *
214
	 * @param string $gid the internal group name
215
	 * @param string $search optional, a search string
216
	 * @return int|bool
217
	 */
218
	public function countUsersInGroup($gid, $search = '') {
219
		return $this->handleRequest(
220
			$gid, 'countUsersInGroup', [$gid, $search]);
221
	}
222
223
	/**
224
	 * get an array with group details
225
	 *
226
	 * @param string $gid
227
	 * @return array|false
228
	 */
229
	public function getGroupDetails($gid) {
230
		return $this->handleRequest(
231
			$gid, 'getGroupDetails', [$gid]);
232
	}
233
234
	/**
235
	 * get a list of all groups
236
	 *
237
	 * @return string[] with group names
238
	 *
239
	 * Returns a list with all groups
240
	 */
241
	public function getGroups($search = '', $limit = -1, $offset = 0) {
242
		$groups = [];
243
244
		foreach ($this->backends as $backend) {
245
			$backendGroups = $backend->getGroups($search, $limit, $offset);
246
			if (is_array($backendGroups)) {
247
				$groups = array_merge($groups, $backendGroups);
248
			}
249
		}
250
251
		return $groups;
252
	}
253
254
	/**
255
	 * check if a group exists
256
	 *
257
	 * @param string $gid
258
	 * @return bool
259
	 */
260
	public function groupExists($gid) {
261
		return $this->handleRequest($gid, 'groupExists', [$gid]);
262
	}
263
264
	/**
265
	 * Check if backend implements actions
266
	 *
267
	 * @param int $actions bitwise-or'ed actions
268
	 * @return boolean
269
	 *
270
	 * Returns the supported actions as int to be
271
	 * compared with \OCP\GroupInterface::CREATE_GROUP etc.
272
	 */
273
	public function implementsActions($actions) {
274
		//it's the same across all our user backends obviously
275
		return $this->refBackend->implementsActions($actions);
276
	}
277
278
	/**
279
	 * Return access for LDAP interaction.
280
	 *
281
	 * @param string $gid
282
	 * @return Access instance of Access for LDAP interaction
283
	 */
284
	public function getLDAPAccess($gid) {
285
		return $this->handleRequest($gid, 'getLDAPAccess', [$gid]);
286
	}
287
288
	/**
289
	 * Return a new LDAP connection for the specified group.
290
	 * The connection needs to be closed manually.
291
	 *
292
	 * @param string $gid
293
	 * @return resource of the LDAP connection
294
	 */
295
	public function getNewLDAPConnection($gid) {
296
		return $this->handleRequest($gid, 'getNewLDAPConnection', [$gid]);
297
	}
298
299
	public function getDisplayName(string $gid): string {
300
		return $this->handleRequest($gid, 'getDisplayName', [$gid]);
301
	}
302
303
	/**
304
	 * Backend name to be shown in group management
305
	 * @return string the name of the backend to be shown
306
	 * @since 22.0.0
307
	 */
308
	public function getBackendName(): string {
309
		return 'LDAP';
310
	}
311
}
312