Completed
Push — master ( 0256f6...5411d6 )
by Morris
22:12 queued 06:46
created

Group_Proxy::deleteGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
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
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\User_LDAP;
28
29
class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP {
30
	private $backends = array();
31
	private $refBackend = null;
32
33
	/**
34
	 * Constructor
35
	 * @param string[] $serverConfigPrefixes array containing the config Prefixes
36
	 */
37
	public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) {
38
		parent::__construct($ldap);
39 View Code Duplication
		foreach($serverConfigPrefixes as $configPrefix) {
40
			$this->backends[$configPrefix] =
41
				new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix), $groupPluginManager);
42
			if(is_null($this->refBackend)) {
43
				$this->refBackend = &$this->backends[$configPrefix];
44
			}
45
		}
46
	}
47
48
	/**
49
	 * Tries the backends one after the other until a positive result is returned from the specified method
50
	 * @param string $gid the gid connected to the request
51
	 * @param string $method the method of the group backend that shall be called
52
	 * @param array $parameters an array of parameters to be passed
53
	 * @return mixed, the result of the method or false
0 ignored issues
show
Documentation introduced by
The doc-type mixed, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
54
	 */
55
	protected function walkBackends($gid, $method, $parameters) {
56
		$cacheKey = $this->getGroupCacheKey($gid);
57
		foreach($this->backends as $configPrefix => $backend) {
58 View Code Duplication
			if($result = call_user_func_array(array($backend, $method), $parameters)) {
59
				$this->writeToCache($cacheKey, $configPrefix);
60
				return $result;
61
			}
62
		}
63
		return false;
64
	}
65
66
	/**
67
	 * Asks the backend connected to the server that supposely takes care of the gid from the request.
68
	 * @param string $gid the gid connected to the request
69
	 * @param string $method the method of the group backend that shall be called
70
	 * @param array $parameters an array of parameters to be passed
71
	 * @param mixed $passOnWhen the result matches this variable
72
	 * @return mixed, the result of the method or false
0 ignored issues
show
Documentation introduced by
The doc-type mixed, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
73
	 */
74
	protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) {
75
		$cacheKey = $this->getGroupCacheKey($gid);;
76
		$prefix = $this->getFromCache($cacheKey);
77
		//in case the uid has been found in the past, try this stored connection first
78
		if(!is_null($prefix)) {
79
			if(isset($this->backends[$prefix])) {
80
				$result = call_user_func_array(array($this->backends[$prefix], $method), $parameters);
81 View Code Duplication
				if($result === $passOnWhen) {
82
					//not found here, reset cache to null if group vanished
83
					//because sometimes methods return false with a reason
84
					$groupExists = call_user_func_array(
85
						array($this->backends[$prefix], 'groupExists'),
86
						array($gid)
87
					);
88
					if(!$groupExists) {
89
						$this->writeToCache($cacheKey, null);
90
					}
91
				}
92
				return $result;
93
			}
94
		}
95
		return false;
96
	}
97
98
	/**
99
	 * is user in group?
100
	 * @param string $uid uid of the user
101
	 * @param string $gid gid of the group
102
	 * @return bool
103
	 *
104
	 * Checks whether the user is member of a group or not.
105
	 */
106
	public function inGroup($uid, $gid) {
107
		return $this->handleRequest($gid, 'inGroup', array($uid, $gid));
108
	}
109
110
	/**
111
	 * Get all groups a user belongs to
112
	 * @param string $uid Name of the user
113
	 * @return string[] with group names
114
	 *
115
	 * This function fetches all groups a user belongs to. It does not check
116
	 * if the user exists at all.
117
	 */
118
	public function getUserGroups($uid) {
119
		$groups = array();
120
121
		foreach($this->backends as $backend) {
122
			$backendGroups = $backend->getUserGroups($uid);
123
			if (is_array($backendGroups)) {
124
				$groups = array_merge($groups, $backendGroups);
125
			}
126
		}
127
128
		return $groups;
129
	}
130
131
	/**
132
	 * get a list of all users in a group
133
	 * @return string[] with user ids
134
	 */
135 View Code Duplication
	public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
136
		$users = array();
137
138
		foreach($this->backends as $backend) {
139
			$backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
140
			if (is_array($backendUsers)) {
141
				$users = array_merge($users, $backendUsers);
142
			}
143
		}
144
145
		return $users;
146
	}
147
148
	/**
149
	 * @param string $gid
150
	 * @return bool
151
	 */
152
	public function createGroup($gid) {
153
		return $this->handleRequest(
154
			$gid, 'createGroup', array($gid));
155
	}
156
157
	/**
158
	 * delete a group
159
	 * @param string $gid gid of the group to delete
160
	 * @return bool
161
	 */
162
	public function deleteGroup($gid) {
163
		return $this->handleRequest(
164
			$gid, 'deleteGroup', array($gid));
165
	}
166
167
	/**
168
	 * Add a user to a group
169
	 * @param string $uid Name of the user to add to group
170
	 * @param string $gid Name of the group in which add the user
171
	 * @return bool
172
	 *
173
	 * Adds a user to a group.
174
	 */
175
	public function addToGroup($uid, $gid) {
176
		return $this->handleRequest(
177
			$gid, 'addToGroup', array($uid, $gid));
178
	}
179
180
	/**
181
	 * Removes a user from a group
182
	 * @param string $uid Name of the user to remove from group
183
	 * @param string $gid Name of the group from which remove the user
184
	 * @return bool
185
	 *
186
	 * removes the user from a group.
187
	 */
188
	public function removeFromGroup($uid, $gid) {
189
		return $this->handleRequest(
190
			$gid, 'removeFromGroup', array($uid, $gid));
191
	}
192
193
	/**
194
	 * returns the number of users in a group, who match the search term
195
	 * @param string $gid the internal group name
196
	 * @param string $search optional, a search string
197
	 * @return int|bool
198
	 */
199
	public function countUsersInGroup($gid, $search = '') {
200
		return $this->handleRequest(
201
			$gid, 'countUsersInGroup', array($gid, $search));
202
	}
203
204
	/**
205
	 * get an array with group details
206
	 * @param string $gid
207
	 * @return array|false
208
	 */
209
	public function getGroupDetails($gid) {
210
		return $this->handleRequest(
211
			$gid, 'getGroupDetails', array($gid));
212
	}
213
214
	/**
215
	 * get a list of all groups
216
	 * @return string[] with group names
217
	 *
218
	 * Returns a list with all groups
219
	 */
220 View Code Duplication
	public function getGroups($search = '', $limit = -1, $offset = 0) {
221
		$groups = array();
222
223
		foreach($this->backends as $backend) {
224
			$backendGroups = $backend->getGroups($search, $limit, $offset);
225
			if (is_array($backendGroups)) {
226
				$groups = array_merge($groups, $backendGroups);
227
			}
228
		}
229
230
		return $groups;
231
	}
232
233
	/**
234
	 * check if a group exists
235
	 * @param string $gid
236
	 * @return bool
237
	 */
238
	public function groupExists($gid) {
239
		return $this->handleRequest($gid, 'groupExists', array($gid));
240
	}
241
242
	/**
243
	 * Check if backend implements actions
244
	 * @param int $actions bitwise-or'ed actions
245
	 * @return boolean
246
	 *
247
	 * Returns the supported actions as int to be
248
	 * compared with \OCP\GroupInterface::CREATE_GROUP etc.
249
	 */
250
	public function implementsActions($actions) {
251
		//it's the same across all our user backends obviously
252
		return $this->refBackend->implementsActions($actions);
253
	}
254
255
	/**
256
	 * Return access for LDAP interaction.
257
	 * @param string $gid
258
	 * @return Access instance of Access for LDAP interaction
259
	 */
260
	public function getLDAPAccess($gid) {
261
		return $this->handleRequest($gid, 'getLDAPAccess', [$gid]);
262
	}
263
264
	/**
265
	 * Return a new LDAP connection for the specified group.
266
	 * The connection needs to be closed manually.
267
	 * @param string $gid
268
	 * @return resource of the LDAP connection
269
	 */
270
	public function getNewLDAPConnection($gid) {
271
		return $this->handleRequest($gid, 'getNewLDAPConnection', array($gid));
272
	}
273
274
}
275