Completed
Push — master ( 1f48f6...828106 )
by Lukas
10:12
created

User_LDAP::userExistsOnLDAP()   D

Complexity

Conditions 9
Paths 22

Size

Total Lines 39
Code Lines 24

Duplication

Lines 3
Ratio 7.69 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 24
c 1
b 0
f 0
nc 22
nop 1
dl 3
loc 39
rs 4.909
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Dominik Schmidt <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Renaud Fortier <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Robin McCorkell <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Tom Needham <[email protected]>
15
 *
16
 * @copyright Copyright (c) 2016, ownCloud, Inc.
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
namespace OCA\User_LDAP;
34
35
use OC\User\NoUserException;
36
use OCA\User_LDAP\User\OfflineUser;
37
use OCA\User_LDAP\User\User;
38
use OCP\IConfig;
39
40
class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserInterface {
41
	/** @var string[] $homesToKill */
42
	protected $homesToKill = array();
43
44
	/** @var \OCP\IConfig */
45
	protected $ocConfig;
46
47
	/**
48
	 * @param Access $access
49
	 * @param \OCP\IConfig $ocConfig
50
	 */
51
	public function __construct(Access $access, IConfig $ocConfig) {
52
		parent::__construct($access);
53
		$this->ocConfig = $ocConfig;
54
	}
55
56
	/**
57
	 * checks whether the user is allowed to change his avatar in ownCloud
58
	 * @param string $uid the ownCloud user name
59
	 * @return boolean either the user can or cannot
60
	 */
61
	public function canChangeAvatar($uid) {
62
		$user = $this->access->userManager->get($uid);
63
		if(!$user instanceof User) {
64
			return false;
65
		}
66
		if($user->getAvatarImage() === false) {
67
			return true;
68
		}
69
70
		return false;
71
	}
72
73
	/**
74
	 * returns the username for the given login name, if available
75
	 *
76
	 * @param string $loginName
77
	 * @return string|false
78
	 */
79
	public function loginName2UserName($loginName) {
80
		try {
81
			$ldapRecord = $this->getLDAPUserByLoginName($loginName);
82
			$user = $this->access->userManager->get($ldapRecord['dn'][0]);
83
			if($user instanceof OfflineUser) {
84
				return false;
85
			}
86
			return $user->getUsername();
87
		} catch (\Exception $e) {
88
			return false;
89
		}
90
	}
91
92
	/**
93
	 * returns an LDAP record based on a given login name
94
	 *
95
	 * @param string $loginName
96
	 * @return array
97
	 * @throws \Exception
98
	 */
99
	public function getLDAPUserByLoginName($loginName) {
100
		//find out dn of the user name
101
		$attrs = $this->access->userManager->getAttributes();
102
		$users = $this->access->fetchUsersByLoginName($loginName, $attrs);
103
		if(count($users) < 1) {
104
			throw new \Exception('No user available for the given login name on ' .
105
				$this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort);
106
		}
107
		return $users[0];
108
	}
109
110
	/**
111
	 * Check if the password is correct
112
	 * @param string $uid The username
113
	 * @param string $password The password
114
	 * @return false|string
115
	 *
116
	 * Check if the password is correct without logging in the user
117
	 */
118
	public function checkPassword($uid, $password) {
119
		try {
120
			$ldapRecord = $this->getLDAPUserByLoginName($uid);
121
		} catch(\Exception $e) {
122
			\OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']);
123
			return false;
124
		}
125
		$dn = $ldapRecord['dn'][0];
126
		$user = $this->access->userManager->get($dn);
127
128
		if(!$user instanceof User) {
129
			\OCP\Util::writeLog('user_ldap',
130
				'LDAP Login: Could not get user object for DN ' . $dn .
131
				'. Maybe the LDAP entry has no set display name attribute?',
132
				\OCP\Util::WARN);
133
			return false;
134
		}
135
		if($user->getUsername() !== false) {
136
			//are the credentials OK?
137
			if(!$this->access->areCredentialsValid($dn, $password)) {
138
				return false;
139
			}
140
141
			$this->access->cacheUserExists($user->getUsername());
142
			$user->processAttributes($ldapRecord);
143
			$user->markLogin();
144
145
			return $user->getUsername();
146
		}
147
148
		return false;
149
	}
150
151
	/**
152
	 * Get a list of all users
153
	 *
154
	 * @param string $search
155
	 * @param integer $limit
156
	 * @param integer $offset
157
	 * @return string[] an array of all uids
158
	 */
159
	public function getUsers($search = '', $limit = 10, $offset = 0) {
160
		$search = $this->access->escapeFilterPart($search, true);
161
		$cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
162
163
		//check if users are cached, if so return
164
		$ldap_users = $this->access->connection->getFromCache($cachekey);
165
		if(!is_null($ldap_users)) {
166
			return $ldap_users;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $ldap_users; (object|integer|double|string|array|boolean) is incompatible with the return type declared by the interface OCP\UserInterface::getUsers of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
167
		}
168
169
		// if we'd pass -1 to LDAP search, we'd end up in a Protocol
170
		// error. With a limit of 0, we get 0 results. So we pass null.
171
		if($limit <= 0) {
172
			$limit = null;
173
		}
174
		$filter = $this->access->combineFilterWithAnd(array(
175
			$this->access->connection->ldapUserFilter,
176
			$this->access->connection->ldapUserDisplayName . '=*',
177
			$this->access->getFilterPartForUserSearch($search)
178
		));
179
		$attrs = array($this->access->connection->ldapUserDisplayName, 'dn');
180
		$additionalAttribute = $this->access->connection->ldapUserDisplayName2;
181
		if(!empty($additionalAttribute)) {
182
			$attrs[] = $additionalAttribute;
183
		}
184
185
		\OCP\Util::writeLog('user_ldap',
186
			'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
187
			\OCP\Util::DEBUG);
188
		//do the search and translate results to owncloud names
189
		$ldap_users = $this->access->fetchListOfUsers(
190
			$filter,
191
			$this->access->userManager->getAttributes(true),
192
			$limit, $offset);
193
		$ldap_users = $this->access->ownCloudUserNames($ldap_users);
194
		\OCP\Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', \OCP\Util::DEBUG);
195
196
		$this->access->connection->writeToCache($cachekey, $ldap_users);
197
		return $ldap_users;
198
	}
199
200
	/**
201
	 * checks whether a user is still available on LDAP
202
	 *
203
	 * @param string|\OCA\User_LDAP\User\User $user either the ownCloud user
204
	 * name or an instance of that user
205
	 * @return bool
206
	 * @throws \Exception
207
	 * @throws \OC\ServerNotAvailableException
208
	 */
209
	public function userExistsOnLDAP($user) {
210
		if(is_string($user)) {
211
			$user = $this->access->userManager->get($user);
212
		}
213
		if(is_null($user)) {
214
			return false;
215
		}
216
217
		$dn = $user->getDN();
218
		//check if user really still exists by reading its entry
219
		if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) {
220
			$lcr = $this->access->connection->getConnectionResource();
221
			if(is_null($lcr)) {
222
				throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost);
223
			}
224
225
			try {
226
				$uuid = $this->access->getUserMapper()->getUUIDByDN($dn);
227
				if(!$uuid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uuid of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
228
					return false;
229
				}
230
				$newDn = $this->access->getUserDnByUuid($uuid);
231
				//check if renamed user is still valid by reapplying the ldap filter
232 View Code Duplication
				if(!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) {
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...
233
					return false;
234
				}
235
				$this->access->getUserMapper()->setDNbyUUID($newDn, $uuid);
236
				return true;
237
			} catch (\Exception $e) {
238
				return false;
239
			}
240
		}
241
242
		if($user instanceof OfflineUser) {
243
			$user->unmark();
244
		}
245
246
		return true;
247
	}
248
249
	/**
250
	 * check if a user exists
251
	 * @param string $uid the username
252
	 * @return boolean
253
	 * @throws \Exception when connection could not be established
254
	 */
255
	public function userExists($uid) {
256
		$userExists = $this->access->connection->getFromCache('userExists'.$uid);
257
		if(!is_null($userExists)) {
258
			return (bool)$userExists;
259
		}
260
		//getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
261
		$user = $this->access->userManager->get($uid);
262
263
		if(is_null($user)) {
264
			\OCP\Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.
265
				$this->access->connection->ldapHost, \OCP\Util::DEBUG);
266
			$this->access->connection->writeToCache('userExists'.$uid, false);
267
			return false;
268
		} else if($user instanceof OfflineUser) {
269
			//express check for users marked as deleted. Returning true is
270
			//necessary for cleanup
271
			return true;
272
		}
273
274
		$result = $this->userExistsOnLDAP($user);
275
		$this->access->connection->writeToCache('userExists'.$uid, $result);
276
		if($result === true) {
277
			$user->update();
278
		}
279
		return $result;
280
	}
281
282
	/**
283
	* returns whether a user was deleted in LDAP
284
	*
285
	* @param string $uid The username of the user to delete
286
	* @return bool
287
	*/
288
	public function deleteUser($uid) {
289
		$marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0);
290 View Code Duplication
		if(intval($marked) === 0) {
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...
291
			\OC::$server->getLogger()->notice(
292
				'User '.$uid . ' is not marked as deleted, not cleaning up.',
293
				array('app' => 'user_ldap'));
294
			return false;
295
		}
296
		\OC::$server->getLogger()->info('Cleaning up after user ' . $uid,
297
			array('app' => 'user_ldap'));
298
299
		//Get Home Directory out of user preferences so we can return it later,
300
		//necessary for removing directories as done by OC_User.
301
		$home = $this->ocConfig->getUserValue($uid, 'user_ldap', 'homePath', '');
302
		$this->homesToKill[$uid] = $home;
303
		$this->access->getUserMapper()->unmap($uid);
304
305
		return true;
306
	}
307
308
	/**
309
	 * get the user's home directory
310
	 *
311
	 * @param string $uid the username
312
	 * @return bool|string
313
	 * @throws NoUserException
314
	 * @throws \Exception
315
	 */
316
	public function getHome($uid) {
317
		if(isset($this->homesToKill[$uid]) && !empty($this->homesToKill[$uid])) {
318
			//a deleted user who needs some clean up
319
			return $this->homesToKill[$uid];
320
		}
321
322
		// user Exists check required as it is not done in user proxy!
323
		if(!$this->userExists($uid)) {
324
			return false;
325
		}
326
327
		$cacheKey = 'getHome'.$uid;
328
		$path = $this->access->connection->getFromCache($cacheKey);
329
		if(!is_null($path)) {
330
			return $path;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $path; (object|integer|double|string|array|boolean) is incompatible with the return type documented by OCA\User_LDAP\User_LDAP::getHome of type boolean|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
331
		}
332
333
		$user = $this->access->userManager->get($uid);
334
		if(is_null($user) || ($user instanceof OfflineUser && !$this->userExistsOnLDAP($user->getOCName()))) {
335
			throw new NoUserException($uid . ' is not a valid user anymore');
336
		}
337
		if($user instanceof OfflineUser) {
338
			// apparently this user survived the userExistsOnLDAP check,
339
			// we request the user instance again in order to retrieve a User
340
			// instance instead
341
			$user = $this->access->userManager->get($uid);
342
		}
343
		$path = $user->getHomePath();
344
		$this->access->cacheUserHome($uid, $path);
345
346
		return $path;
347
	}
348
349
	/**
350
	 * get display name of the user
351
	 * @param string $uid user ID of the user
352
	 * @return string|false display name
353
	 */
354
	public function getDisplayName($uid) {
355
		if(!$this->userExists($uid)) {
356
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface OCP\UserInterface::getDisplayName of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
357
		}
358
359
		$cacheKey = 'getDisplayName'.$uid;
360
		if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
361
			return $displayName;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $displayName; (object|integer|double|string|array|boolean) is incompatible with the return type declared by the interface OCP\UserInterface::getDisplayName of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
362
		}
363
364
		//Check whether the display name is configured to have a 2nd feature
365
		$additionalAttribute = $this->access->connection->ldapUserDisplayName2;
366
		$displayName2 = '';
367
		if(!empty($additionalAttribute)) {
368
			$displayName2 = $this->access->readAttribute(
369
				$this->access->username2dn($uid),
0 ignored issues
show
Security Bug introduced by
It seems like $this->access->username2dn($uid) targeting OCA\User_LDAP\Access::username2dn() can also be of type false; however, OCA\User_LDAP\Access::readAttribute() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
370
				$additionalAttribute);
371
		}
372
373
		$displayName = $this->access->readAttribute(
374
			$this->access->username2dn($uid),
0 ignored issues
show
Security Bug introduced by
It seems like $this->access->username2dn($uid) targeting OCA\User_LDAP\Access::username2dn() can also be of type false; however, OCA\User_LDAP\Access::readAttribute() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
375
			$this->access->connection->ldapUserDisplayName);
376
377
		if($displayName && (count($displayName) > 0)) {
378
			$displayName = $displayName[0];
379
380
			if(is_array($displayName2) && (count($displayName2) > 0)) {
381
				$displayName2 = $displayName2[0];
382
			}
383
384
			$user = $this->access->userManager->get($uid);
385
			$displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
0 ignored issues
show
Bug introduced by
The method composeAndStoreDisplayName does only exist in OCA\User_LDAP\User\User, but not in OCA\User_LDAP\User\OfflineUser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
386
			$this->access->connection->writeToCache($cacheKey, $displayName);
387
			return $displayName;
388
		}
389
390
		return null;
391
	}
392
393
	/**
394
	 * Get a list of all display names
395
	 *
396
	 * @param string $search
397
	 * @param string|null $limit
398
	 * @param string|null $offset
399
	 * @return array an array of all displayNames (value) and the corresponding uids (key)
400
	 */
401
	public function getDisplayNames($search = '', $limit = null, $offset = null) {
402
		$cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
403
		if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
404
			return $displayNames;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $displayNames; (object|integer|double|string|array|boolean) is incompatible with the return type declared by the interface OCP\UserInterface::getDisplayNames of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
405
		}
406
407
		$displayNames = array();
408
		$users = $this->getUsers($search, $limit, $offset);
409
		foreach ($users as $user) {
0 ignored issues
show
Bug introduced by
The expression $users of type object|integer|double|string|array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
410
			$displayNames[$user] = $this->getDisplayName($user);
411
		}
412
		$this->access->connection->writeToCache($cacheKey, $displayNames);
413
		return $displayNames;
414
	}
415
416
	/**
417
	* Check if backend implements actions
418
	* @param int $actions bitwise-or'ed actions
419
	* @return boolean
420
	*
421
	* Returns the supported actions as int to be
422
	* compared with OC_USER_BACKEND_CREATE_USER etc.
423
	*/
424
	public function implementsActions($actions) {
425
		return (bool)((\OC\User\Backend::CHECK_PASSWORD
426
			| \OC\User\Backend::GET_HOME
427
			| \OC\User\Backend::GET_DISPLAYNAME
428
			| \OC\User\Backend::PROVIDE_AVATAR
429
			| \OC\User\Backend::COUNT_USERS)
430
			& $actions);
431
	}
432
433
	/**
434
	 * @return bool
435
	 */
436
	public function hasUserListings() {
437
		return true;
438
	}
439
440
	/**
441
	 * counts the users in LDAP
442
	 *
443
	 * @return int|bool
444
	 */
445
	public function countUsers() {
446
		$filter = $this->access->getFilterForUserCount();
447
		$cacheKey = 'countUsers-'.$filter;
448
		if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) {
449
			return $entries;
450
		}
451
		$entries = $this->access->countUsers($filter);
452
		$this->access->connection->writeToCache($cacheKey, $entries);
453
		return $entries;
454
	}
455
456
	/**
457
	 * Backend name to be shown in user management
458
	 * @return string the name of the backend to be shown
459
	 */
460
	public function getBackendName(){
461
		return 'LDAP';
462
	}
463
464
}
465