Completed
Push — master ( c20409...a4266f )
by Lukas
07:37
created

Group_Proxy::countUsersInGroup()   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 2
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 {
30
	private $backends = array();
31
	private $refBackend = null;
32
33
	/**
34
	 * Constructor
35
	 * @param string[] $serverConfigPrefixes array containing the config Prefixes
36
	 */
37 View Code Duplication
	public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
		parent::__construct($ldap);
39
		foreach($serverConfigPrefixes as $configPrefix) {
40
			$this->backends[$configPrefix] =
41
				new \OCA\User_LDAP\Group_LDAP($this->getAccess($configPrefix));
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	 * returns the number of users in a group, who match the search term
150
	 * @param string $gid the internal group name
151
	 * @param string $search optional, a search string
152
	 * @return int|bool
153
	 */
154
	public function countUsersInGroup($gid, $search = '') {
155
		return $this->handleRequest(
156
			$gid, 'countUsersInGroup', array($gid, $search));
157
	}
158
159
	/**
160
	 * get a list of all groups
161
	 * @return string[] with group names
162
	 *
163
	 * Returns a list with all groups
164
	 */
165 View Code Duplication
	public function getGroups($search = '', $limit = -1, $offset = 0) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
		$groups = array();
167
168
		foreach($this->backends as $backend) {
169
			$backendGroups = $backend->getGroups($search, $limit, $offset);
170
			if (is_array($backendGroups)) {
171
				$groups = array_merge($groups, $backendGroups);
172
			}
173
		}
174
175
		return $groups;
176
	}
177
178
	/**
179
	 * check if a group exists
180
	 * @param string $gid
181
	 * @return bool
182
	 */
183
	public function groupExists($gid) {
184
		return $this->handleRequest($gid, 'groupExists', array($gid));
185
	}
186
187
	/**
188
	 * Check if backend implements actions
189
	 * @param int $actions bitwise-or'ed actions
190
	 * @return boolean
191
	 *
192
	 * Returns the supported actions as int to be
193
	 * compared with OC_USER_BACKEND_CREATE_USER etc.
194
	 */
195
	public function implementsActions($actions) {
196
		//it's the same across all our user backends obviously
197
		return $this->refBackend->implementsActions($actions);
198
	}
199
200
	/**
201
	 * Return access for LDAP interaction.
202
	 * @param string $gid
203
	 * @return Access instance of Access for LDAP interaction
204
	 */
205
	public function getLDAPAccess($gid) {
206
		return $this->handleRequest($gid, 'getLDAPAccess', []);
207
	}
208
}
209