]> source.dussan.org Git - nextcloud-server.git/commitdiff
tidy up Group_LDAP 21171/head
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Sun, 31 May 2020 22:01:49 +0000 (00:01 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Mon, 8 Jun 2020 11:40:24 +0000 (13:40 +0200)
* remove unused method
* resolve code duplication
* remove usage of deprectad Util::writeLog
* phpDoc updates
* signature updates

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
apps/user_ldap/lib/Access.php
apps/user_ldap/lib/Connection.php
apps/user_ldap/lib/Group_LDAP.php
apps/user_ldap/tests/Group_LDAPTest.php

index d58106707efa91967056e5185eeb8f8040beaaf5..ab064962e0139849746360fcb9b3d26330374815 100644 (file)
@@ -1454,7 +1454,7 @@ class Access extends LDAPUtility {
         * @param bool $allowAsterisk whether in * at the beginning should be preserved
         * @return string the escaped string
         */
-       public function escapeFilterPart($input, $allowAsterisk = false) {
+       public function escapeFilterPart($input, $allowAsterisk = false): string {
                $asterisk = '';
                if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') {
                        $asterisk = '*';
@@ -1471,7 +1471,7 @@ class Access extends LDAPUtility {
         * @param string[] $filters the filters to connect
         * @return string the combined filter
         */
-       public function combineFilterWithAnd($filters) {
+       public function combineFilterWithAnd($filters): string {
                return $this->combineFilter($filters, '&');
        }
 
index 0cc93a08e6f77df62b98bcaa425c2ebe1ebdb9b3..079429027c54fd2473d8e723ed3934cd25840288 100644 (file)
@@ -67,6 +67,11 @@ use OCP\ILogger;
  * @property string[] ldapBaseGroups
  * @property string ldapGroupFilter
  * @property string ldapGroupDisplayName
+ * @property string ldapLoginFilter
+ * @property string ldapDynamicGroupMemberURL
+ * @property string ldapGidNumber
+ * @property int hasMemberOfFilterSupport
+ * @property int useMemberOfToDetectMembership
  */
 class Connection extends LDAPUtility {
        private $ldapConnectionRes = null;
index a6cd9669c93f7d58018aa917e8932ffa4a85d79e..323aa61b1b07014137e20371a309c877ab75b1e7 100644 (file)
 
 namespace OCA\User_LDAP;
 
+use Closure;
+use Exception;
+use OC;
 use OC\Cache\CappedMemoryCache;
+use OC\ServerNotAvailableException;
 use OCP\Group\Backend\IGetDisplayNameBackend;
 use OCP\GroupInterface;
 use OCP\ILogger;
 
-class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend {
+class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend {
        protected $enabled = false;
 
-       /**
-        * @var string[] $cachedGroupMembers array of users with gid as key
-        */
+       /** @var string[] $cachedGroupMembers array of users with gid as key */
        protected $cachedGroupMembers;
-
-       /**
-        * @var string[] $cachedGroupsByMember array of groups with uid as key
-        */
+       /** @var string[] $cachedGroupsByMember array of groups with uid as key */
        protected $cachedGroupsByMember;
-
-       /**
-        * @var string[] $cachedNestedGroups array of groups with gid (DN) as key
-        */
+       /** @var string[] $cachedNestedGroups array of groups with gid (DN) as key */
        protected $cachedNestedGroups;
-
        /** @var GroupPluginManager */
        protected $groupPluginManager;
+       /** @var ILogger */
+       protected $logger;
 
        public function __construct(Access $access, GroupPluginManager $groupPluginManager) {
                parent::__construct($access);
                $filter = $this->access->connection->ldapGroupFilter;
-               $gassoc = $this->access->connection->ldapGroupMemberAssocAttr;
-               if (!empty($filter) && !empty($gassoc)) {
+               $gAssoc = $this->access->connection->ldapGroupMemberAssocAttr;
+               if (!empty($filter) && !empty($gAssoc)) {
                        $this->enabled = true;
                }
 
@@ -81,6 +78,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                $this->cachedGroupsByMember = new CappedMemoryCache();
                $this->cachedNestedGroups = new CappedMemoryCache();
                $this->groupPluginManager = $groupPluginManager;
+               $this->logger = OC::$server->getLogger();
        }
 
        /**
@@ -89,8 +87,8 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         * @param string $uid uid of the user
         * @param string $gid gid of the group
         * @return bool
-        *
-        * Checks whether the user is member of a group or not.
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
        public function inGroup($uid, $gid) {
                if (!$this->enabled) {
@@ -105,8 +103,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                $userDN = $this->access->username2dn($uid);
 
                if (isset($this->cachedGroupMembers[$gid])) {
-                       $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]);
-                       return $isInGroup;
+                       return in_array($userDN, $this->cachedGroupMembers[$gid]);
                }
 
                $cacheKeyMembers = 'inGroup-members:' . $gid;
@@ -174,13 +171,13 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * @param string $dnGroup
-        * @return array
+        * For a group that has user membership defined by an LDAP search url
+        * attribute returns the users that match the search url otherwise returns
+        * an empty array.
         *
-        * For a group that has user membership defined by an LDAP search url attribute returns the users
-        * that match the search url otherwise returns an empty array.
+        * @throws ServerNotAvailableException
         */
-       public function getDynamicGroupMembers($dnGroup) {
+       public function getDynamicGroupMembers(string $dnGroup): array {
                $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL);
 
                if (empty($dynamicGroupMemberURL)) {
@@ -206,26 +203,26 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                                        $dynamicMembers[$value['dn'][0]] = 1;
                                }
                        } else {
-                               \OCP\Util::writeLog('user_ldap', 'No search filter found on member url ' .
-                                       'of group ' . $dnGroup, ILogger::DEBUG);
+                               $this->logger->debug('No search filter found on member url of group {dn}',
+                                       [
+                                               'app' => 'user_ldap',
+                                               'dn' => $dnGroup,
+                                       ]
+                               );
                        }
                }
                return $dynamicMembers;
        }
 
        /**
-        * @param string $dnGroup
-        * @param array|null &$seen
-        * @return array|mixed|null
-        * @throws \OC\ServerNotAvailableException
+        * @throws ServerNotAvailableException
         */
-       private function _groupMembers($dnGroup, &$seen = null) {
+       private function _groupMembers(string $dnGroup, ?array &$seen = null): array {
                if ($seen === null) {
                        $seen = [];
                }
                $allMembers = [];
                if (array_key_exists($dnGroup, $seen)) {
-                       // avoid loops
                        return [];
                }
                // used extensively in cron job, caching makes sense for nested groups
@@ -250,13 +247,10 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * @param string $DN
-        * @param array|null &$seen
-        * @return array
-        * @throws \OC\ServerNotAvailableException
+        * @throws ServerNotAvailableException
         */
-       private function _getGroupDNsFromMemberOf($DN) {
-               $groups = $this->access->readAttribute($DN, 'memberOf');
+       private function _getGroupDNsFromMemberOf(string $dn): array {
+               $groups = $this->access->readAttribute($dn, 'memberOf');
                if (!is_array($groups)) {
                        return [];
                }
@@ -274,17 +268,11 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        return $nestedGroups;
                };
 
-               $groups = $this->walkNestedGroups($DN, $fetcher, $groups);
+               $groups = $this->walkNestedGroups($dn, $fetcher, $groups);
                return $this->filterValidGroups($groups);
        }
 
-       /**
-        * @param string $dn
-        * @param \Closure $fetcher args: string $dn, array $seen, returns: string[] of dns
-        * @param array $list
-        * @return array
-        */
-       private function walkNestedGroups(string $dn, \Closure $fetcher, array $list): array {
+       private function walkNestedGroups(string $dn, Closure $fetcher, array $list): array {
                $nesting = (int)$this->access->connection->ldapNestedGroups;
                // depending on the input, we either have a list of DNs or a list of LDAP records
                // also, the output expects either DNs or records. Testing the first element should suffice.
@@ -321,11 +309,11 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        /**
         * translates a gidNumber into an ownCloud internal name
         *
-        * @param string $gid as given by gidNumber on POSIX LDAP
-        * @param string $dn a DN that belongs to the same domain as the group
         * @return string|bool
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
-       public function gidNumber2Name($gid, $dn) {
+       public function gidNumber2Name(string $gid, string $dn) {
                $cacheKey = 'gidNumberToName' . $gid;
                $groupName = $this->access->connection->getFromCache($cacheKey);
                if (!is_null($groupName) && isset($groupName)) {
@@ -338,14 +326,22 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        'objectClass=posixGroup',
                        $this->access->connection->ldapGidNumber . '=' . $gid
                ]);
+               return $this->getNameOfGroup($filter, $cacheKey) ?? false;
+       }
+
+       /**
+        * @throws ServerNotAvailableException
+        * @throws Exception
+        */
+       private function getNameOfGroup(string $filter, string $cacheKey) {
                $result = $this->access->searchGroups($filter, ['dn'], 1);
                if (empty($result)) {
-                       return false;
+                       return null;
                }
                $dn = $result[0]['dn'][0];
 
                //and now the group name
-               //NOTE once we have separate ownCloud group IDs and group names we can
+               //NOTE once we have separate Nextcloud group IDs and group names we can
                //directly read the display name attribute instead of the DN
                $name = $this->access->dn2groupname($dn);
 
@@ -357,11 +353,10 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        /**
         * returns the entry's gidNumber
         *
-        * @param string $dn
-        * @param string $attribute
         * @return string|bool
+        * @throws ServerNotAvailableException
         */
-       private function getEntryGidNumber($dn, $attribute) {
+       private function getEntryGidNumber(string $dn, string $attribute) {
                $value = $this->access->readAttribute($dn, $attribute);
                if (is_array($value) && !empty($value)) {
                        return $value[0];
@@ -370,22 +365,20 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * returns the group's primary ID
-        *
-        * @param string $dn
         * @return string|bool
+        * @throws ServerNotAvailableException
         */
-       public function getGroupGidNumber($dn) {
+       public function getGroupGidNumber(string $dn) {
                return $this->getEntryGidNumber($dn, 'gidNumber');
        }
 
        /**
         * returns the user's gidNumber
         *
-        * @param string $dn
         * @return string|bool
+        * @throws ServerNotAvailableException
         */
-       public function getUserGidNumber($dn) {
+       public function getUserGidNumber(string $dn) {
                $gidNumber = false;
                if ($this->access->connection->hasGidNumber) {
                        $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber);
@@ -397,17 +390,13 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * returns a filter for a "users has specific gid" search or count operation
-        *
-        * @param string $groupDN
-        * @param string $search
-        * @return string
-        * @throws \Exception
+        * @throws ServerNotAvailableException
+        * @throws Exception
         */
-       private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') {
+       private function prepareFilterForUsersHasGidNumber(string $groupDN, string $search = ''): string {
                $groupID = $this->getGroupGidNumber($groupDN);
                if ($groupID === false) {
-                       throw new \Exception('Not a valid group');
+                       throw new Exception('Not a valid group');
                }
 
                $filterParts = [];
@@ -423,13 +412,14 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        /**
         * returns a list of users that have the given group as gid number
         *
-        * @param string $groupDN
-        * @param string $search
-        * @param int $limit
-        * @param int $offset
-        * @return string[]
+        * @throws ServerNotAvailableException
         */
-       public function getUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) {
+       public function getUsersInGidNumber(
+               string $groupDN,
+               string $search = '',
+               ?int $limit = -1,
+               ?int $offset = 0
+       ): array {
                try {
                        $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search);
                        $users = $this->access->fetchListOfUsers(
@@ -439,37 +429,18 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                                $offset
                        );
                        return $this->access->nextcloudUserNames($users);
-               } catch (\Exception $e) {
+               } catch (ServerNotAvailableException $e) {
+                       throw $e;
+               } catch (Exception $e) {
                        return [];
                }
        }
 
        /**
-        * returns the number of users that have the given group as gid number
-        *
-        * @param string $groupDN
-        * @param string $search
-        * @param int $limit
-        * @param int $offset
-        * @return int
-        */
-       public function countUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) {
-               try {
-                       $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search);
-                       $users = $this->access->countUsers($filter, ['dn'], $limit, $offset);
-                       return (int)$users;
-               } catch (\Exception $e) {
-                       return 0;
-               }
-       }
-
-       /**
-        * gets the gidNumber of a user
-        *
-        * @param string $dn
-        * @return string
+        * @throws ServerNotAvailableException
+        * @return bool
         */
-       public function getUserGroupByGid($dn) {
+       public function getUserGroupByGid(string $dn) {
                $groupID = $this->getUserGidNumber($dn);
                if ($groupID !== false) {
                        $groupName = $this->gidNumber2Name($groupID, $dn);
@@ -484,11 +455,11 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        /**
         * translates a primary group ID into an Nextcloud internal name
         *
-        * @param string $gid as given by primaryGroupID on AD
-        * @param string $dn a DN that belongs to the same domain as the group
         * @return string|bool
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
-       public function primaryGroupID2Name($gid, $dn) {
+       public function primaryGroupID2Name(string $gid, string $dn) {
                $cacheKey = 'primaryGroupIDtoName';
                $groupNames = $this->access->connection->getFromCache($cacheKey);
                if (!is_null($groupNames) && isset($groupNames[$gid])) {
@@ -505,30 +476,16 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        $this->access->connection->ldapGroupFilter,
                        'objectsid=' . $domainObjectSid . '-' . $gid
                ]);
-               $result = $this->access->searchGroups($filter, ['dn'], 1);
-               if (empty($result)) {
-                       return false;
-               }
-               $dn = $result[0]['dn'][0];
-
-               //and now the group name
-               //NOTE once we have separate Nextcloud group IDs and group names we can
-               //directly read the display name attribute instead of the DN
-               $name = $this->access->dn2groupname($dn);
-
-               $this->access->connection->writeToCache($cacheKey, $name);
-
-               return $name;
+               return $this->getNameOfGroup($filter, $cacheKey) ?? false;
        }
 
        /**
         * returns the entry's primary group ID
         *
-        * @param string $dn
-        * @param string $attribute
         * @return string|bool
+        * @throws ServerNotAvailableException
         */
-       private function getEntryGroupID($dn, $attribute) {
+       private function getEntryGroupID(string $dn, string $attribute) {
                $value = $this->access->readAttribute($dn, $attribute);
                if (is_array($value) && !empty($value)) {
                        return $value[0];
@@ -537,22 +494,18 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * returns the group's primary ID
-        *
-        * @param string $dn
         * @return string|bool
+        * @throws ServerNotAvailableException
         */
-       public function getGroupPrimaryGroupID($dn) {
+       public function getGroupPrimaryGroupID(string $dn) {
                return $this->getEntryGroupID($dn, 'primaryGroupToken');
        }
 
        /**
-        * returns the user's primary group ID
-        *
-        * @param string $dn
         * @return string|bool
+        * @throws ServerNotAvailableException
         */
-       public function getUserPrimaryGroupIDs($dn) {
+       public function getUserPrimaryGroupIDs(string $dn) {
                $primaryGroupID = false;
                if ($this->access->connection->hasPrimaryGroups) {
                        $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID');
@@ -564,17 +517,13 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * returns a filter for a "users in primary group" search or count operation
-        *
-        * @param string $groupDN
-        * @param string $search
-        * @return string
-        * @throws \Exception
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
-       private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') {
+       private function prepareFilterForUsersInPrimaryGroup(string $groupDN, string $search = ''): string {
                $groupID = $this->getGroupPrimaryGroupID($groupDN);
                if ($groupID === false) {
-                       throw new \Exception('Not a valid group');
+                       throw new Exception('Not a valid group');
                }
 
                $filterParts = [];
@@ -588,15 +537,14 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * returns a list of users that have the given group as primary group
-        *
-        * @param string $groupDN
-        * @param string $search
-        * @param int $limit
-        * @param int $offset
-        * @return string[]
+        * @throws ServerNotAvailableException
         */
-       public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) {
+       public function getUsersInPrimaryGroup(
+               string $groupDN,
+               string $search = '',
+               ?int $limit = -1,
+               ?int $offset = 0
+       ): array {
                try {
                        $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search);
                        $users = $this->access->fetchListOfUsers(
@@ -606,37 +554,38 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                                $offset
                        );
                        return $this->access->nextcloudUserNames($users);
-               } catch (\Exception $e) {
+               } catch (ServerNotAvailableException $e) {
+                       throw $e;
+               } catch (Exception $e) {
                        return [];
                }
        }
 
        /**
-        * returns the number of users that have the given group as primary group
-        *
-        * @param string $groupDN
-        * @param string $search
-        * @param int $limit
-        * @param int $offset
-        * @return int
+        * @throws ServerNotAvailableException
         */
-       public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) {
+       public function countUsersInPrimaryGroup(
+               string $groupDN,
+               string $search = '',
+               int $limit = -1,
+               int $offset = 0
+       ): int {
                try {
                        $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search);
                        $users = $this->access->countUsers($filter, ['dn'], $limit, $offset);
                        return (int)$users;
-               } catch (\Exception $e) {
+               } catch (ServerNotAvailableException $e) {
+                       throw $e;
+               } catch (Exception $e) {
                        return 0;
                }
        }
 
        /**
-        * gets the primary group of a user
-        *
-        * @param string $dn
-        * @return string
+        * @return string|bool
+        * @throws ServerNotAvailableException
         */
-       public function getUserPrimaryGroup($dn) {
+       public function getUserPrimaryGroup(string $dn) {
                $groupID = $this->getUserPrimaryGroupIDs($dn);
                if ($groupID !== false) {
                        $groupName = $this->primaryGroupID2Name($groupID, $dn);
@@ -649,15 +598,15 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * Get all groups a user belongs to
-        *
-        * @param string $uid Name of the user
-        * @return array with group names
-        *
         * This function fetches all groups a user belongs to. It does not check
         * if the user exists at all.
         *
         * This function includes groups based on dynamic group membership.
+        *
+        * @param string $uid Name of the user
+        * @return array with group names
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
        public function getUserGroups($uid) {
                if (!$this->enabled) {
@@ -708,8 +657,12 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                                                }
                                        }
                                } else {
-                                       \OCP\Util::writeLog('user_ldap', 'No search filter found on member url ' .
-                                               'of group ' . print_r($dynamicGroup, true), ILogger::DEBUG);
+                                       $this->logger->debug('No search filter found on member url of group {dn}',
+                                               [
+                                                       'app' => 'user_ldap',
+                                                       'dn' => $dynamicGroup,
+                                               ]
+                                       );
                                }
                        }
                }
@@ -751,8 +704,13 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                } elseif (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
                        $result = $this->access->readAttribute($userDN, 'uid');
                        if ($result === false) {
-                               \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on ' .
-                                       $this->access->connection->ldapHost, ILogger::DEBUG);
+                               $this->logger->debug('No uid attribute found for DN {dn} on {host}',
+                                       [
+                                               'app' => 'user_ldap',
+                                               'dn' => $userDN,
+                                               'host' => $this->access->connection->ldapHost,
+                                       ]
+                               );
                                $uid = false;
                        } else {
                                $uid = $result[0];
@@ -787,11 +745,9 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * @param string $dn
-        * @param array|null &$seen
-        * @return array
+        * @throws ServerNotAvailableException
         */
-       private function getGroupsByMember($dn, &$seen = null) {
+       private function getGroupsByMember(string $dn, array &$seen = null): array {
                if ($seen === null) {
                        $seen = [];
                }
@@ -825,7 +781,8 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         * @param int $limit
         * @param int $offset
         * @return array with user ids
-        * @throws \Exception
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
        public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
                if (!$this->enabled) {
@@ -933,6 +890,8 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         * @param string $gid the internal group name
         * @param string $search optional, a search string
         * @return int|bool
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
        public function countUsersInGroup($gid, $search = '') {
                if ($this->groupPluginManager->implementsActions(GroupInterface::COUNT_USERS)) {
@@ -1002,8 +961,8 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                                        continue;
                                }
                                // dn2username will also check if the users belong to the allowed base
-                               if ($ocname = $this->access->dn2username($member)) {
-                                       $groupUsers[] = $ocname;
+                               if ($ncGroupId = $this->access->dn2username($member)) {
+                                       $groupUsers[] = $ncGroupId;
                                }
                        }
                }
@@ -1026,6 +985,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         * Uses a paged search if available to override a
         * server side search limit.
         * (active directory has a limit of 1000 by default)
+        * @throws Exception
         */
        public function getGroups($search = '', $limit = -1, $offset = 0) {
                if (!$this->enabled) {
@@ -1034,7 +994,6 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                $cacheKey = 'getGroups-' . $search . '-' . $limit . '-' . $offset;
 
                //Check cache before driving unnecessary searches
-               \OCP\Util::writeLog('user_ldap', 'getGroups ' . $cacheKey, ILogger::DEBUG);
                $ldap_groups = $this->access->connection->getFromCache($cacheKey);
                if (!is_null($ldap_groups)) {
                        return $ldap_groups;
@@ -1049,7 +1008,6 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        $this->access->connection->ldapGroupFilter,
                        $this->access->getFilterPartForGroupSearch($search)
                ]);
-               \OCP\Util::writeLog('user_ldap', 'getGroups Filter ' . $filter, ILogger::DEBUG);
                $ldap_groups = $this->access->fetchListOfGroups($filter,
                        [$this->access->connection->ldapGroupDisplayName, 'dn'],
                        $limit,
@@ -1060,19 +1018,12 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                return $ldap_groups;
        }
 
-       /**
-        * @param string $group
-        * @return bool
-        */
-       public function groupMatchesFilter($group) {
-               return (strripos($group, $this->groupSearch) !== false);
-       }
-
        /**
         * check if a group exists
         *
         * @param string $gid
         * @return bool
+        * @throws ServerNotAvailableException
         */
        public function groupExists($gid) {
                $groupExists = $this->access->connection->getFromCache('groupExists' . $gid);
@@ -1093,7 +1044,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        return false;
                }
 
-               //if group really still exists, we will be able to read its objectclass
+               //if group really still exists, we will be able to read its objectClass
                if (!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapGroupFilter))) {
                        $this->access->connection->writeToCache('groupExists' . $gid, false);
                        return false;
@@ -1103,6 +1054,10 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                return true;
        }
 
+       /**
+        * @throws ServerNotAvailableException
+        * @throws Exception
+        */
        protected function filterValidGroups(array $listOfGroups): array {
                $validGroupDNs = [];
                foreach ($listOfGroups as $key => $item) {
@@ -1146,7 +1101,8 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         *
         * @param string $gid
         * @return bool
-        * @throws \Exception
+        * @throws Exception
+        * @throws ServerNotAvailableException
         */
        public function createGroup($gid) {
                if ($this->groupPluginManager->implementsActions(GroupInterface::CREATE_GROUP)) {
@@ -1166,7 +1122,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        }
                        return $dn != null;
                }
-               throw new \Exception('Could not create group in LDAP backend.');
+               throw new Exception('Could not create group in LDAP backend.');
        }
 
        /**
@@ -1174,7 +1130,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         *
         * @param string $gid gid of the group to delete
         * @return bool
-        * @throws \Exception
+        * @throws Exception
         */
        public function deleteGroup($gid) {
                if ($this->groupPluginManager->implementsActions(GroupInterface::DELETE_GROUP)) {
@@ -1185,7 +1141,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        }
                        return $ret;
                }
-               throw new \Exception('Could not delete group in LDAP backend.');
+               throw new Exception('Could not delete group in LDAP backend.');
        }
 
        /**
@@ -1194,7 +1150,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         * @param string $uid Name of the user to add to group
         * @param string $gid Name of the group in which add the user
         * @return bool
-        * @throws \Exception
+        * @throws Exception
         */
        public function addToGroup($uid, $gid) {
                if ($this->groupPluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)) {
@@ -1204,7 +1160,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        }
                        return $ret;
                }
-               throw new \Exception('Could not add user to group in LDAP backend.');
+               throw new Exception('Could not add user to group in LDAP backend.');
        }
 
        /**
@@ -1213,7 +1169,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         * @param string $uid Name of the user to remove from group
         * @param string $gid Name of the group from which remove the user
         * @return bool
-        * @throws \Exception
+        * @throws Exception
         */
        public function removeFromGroup($uid, $gid) {
                if ($this->groupPluginManager->implementsActions(GroupInterface::REMOVE_FROM_GROUP)) {
@@ -1223,21 +1179,21 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
                        }
                        return $ret;
                }
-               throw new \Exception('Could not remove user from group in LDAP backend.');
+               throw new Exception('Could not remove user from group in LDAP backend.');
        }
 
        /**
         * Gets group details
         *
         * @param string $gid Name of the group
-        * @return array | false
-        * @throws \Exception
+        * @return array|false
+        * @throws Exception
         */
        public function getGroupDetails($gid) {
                if ($this->groupPluginManager->implementsActions(GroupInterface::GROUP_DETAILS)) {
                        return $this->groupPluginManager->getGroupDetails($gid);
                }
-               throw new \Exception('Could not get group details in LDAP backend.');
+               throw new Exception('Could not get group details in LDAP backend.');
        }
 
        /**
@@ -1247,6 +1203,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
         *
         * @param string $gid
         * @return resource of the LDAP connection
+        * @throws ServerNotAvailableException
         */
        public function getNewLDAPConnection($gid) {
                $connection = clone $this->access->getConnection();
@@ -1254,7 +1211,7 @@ class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLD
        }
 
        /**
-        * @throws \OC\ServerNotAvailableException
+        * @throws ServerNotAvailableException
         */
        public function getDisplayName(string $gid): string {
                if ($this->groupPluginManager instanceof IGetDisplayNameBackend) {
index 6a64a37f8c20e9c71a93258992e861c72dd8c2d3..c8d8ecaf5bf76dda8d2af22b0c87e5196a1f9e5f 100644 (file)
@@ -36,8 +36,10 @@ use OCA\User_LDAP\Connection;
 use OCA\User_LDAP\Group_LDAP as GroupLDAP;
 use OCA\User_LDAP\GroupPluginManager;
 use OCA\User_LDAP\ILDAPWrapper;
+use OCA\User_LDAP\Mapping\GroupMapping;
 use OCA\User_LDAP\User\Manager;
 use OCP\GroupInterface;
+use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
 /**
@@ -49,18 +51,18 @@ use Test\TestCase;
  */
 class Group_LDAPTest extends TestCase {
        /**
-        * @return \PHPUnit_Framework_MockObject_MockObject|Access
+        * @return MockObject|Access
         */
        private function getAccessMock() {
                static $conMethods;
                static $accMethods;
 
                if (is_null($conMethods) || is_null($accMethods)) {
-                       $conMethods = get_class_methods('\OCA\User_LDAP\Connection');
-                       $accMethods = get_class_methods('\OCA\User_LDAP\Access');
+                       $conMethods = get_class_methods(Connection::class);
+                       $accMethods = get_class_methods(Access::class);
                }
                $lw = $this->createMock(ILDAPWrapper::class);
-               $connector = $this->getMockBuilder('\OCA\User_LDAP\Connection')
+               $connector = $this->getMockBuilder(Connection::class)
                        ->setMethods($conMethods)
                        ->setConstructorArgs([$lw, null, null])
                        ->getMock();
@@ -74,14 +76,14 @@ class Group_LDAPTest extends TestCase {
                return $access;
        }
 
+       /**
+        * @return MockObject|GroupPluginManager
+        */
        private function getPluginManagerMock() {
-               return $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')->getMock();
+               return $this->createMock(GroupPluginManager::class);
        }
 
-       /**
-        * @param Access|\PHPUnit_Framework_MockObject_MockObject $access
-        */
-       private function enableGroups($access) {
+       private function enableGroups(Access $access) {
                $access->connection = $this->createMock(Connection::class);
 
                $access->connection->expects($this->any())
@@ -104,7 +106,6 @@ class Group_LDAPTest extends TestCase {
                $access->expects($this->any())
                        ->method('groupname2dn')
                        ->willReturn($groupDN);
-
                $access->expects($this->any())
                        ->method('readAttribute')
                        ->willReturnCallback(function ($dn) use ($groupDN) {
@@ -121,7 +122,6 @@ class Group_LDAPTest extends TestCase {
                $access->expects($this->any())
                        ->method('isDNPartOfBase')
                        ->willReturn(true);
-
                // for primary groups
                $access->expects($this->once())
                        ->method('countUsers')
@@ -166,6 +166,9 @@ class Group_LDAPTest extends TestCase {
                $access->expects($this->any())
                        ->method('isDNPartOfBase')
                        ->willReturn(true);
+               $access->expects($this->any())
+                       ->method('escapeFilterPart')
+                       ->willReturnArgument(0);
 
                $groupBackend = new GroupLDAP($access, $pluginManager);
                $users = $groupBackend->countUsersInGroup('group', '3');
@@ -174,8 +177,8 @@ class Group_LDAPTest extends TestCase {
        }
 
        public function testCountUsersWithPlugin() {
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'countUsersInGroup'])
                        ->getMock();
 
@@ -636,11 +639,9 @@ class Group_LDAPTest extends TestCase {
                $access->expects($this->any())
                        ->method('username2dn')
                        ->willReturn($dn);
-
                $access->expects($this->exactly(5))
                        ->method('readAttribute')
                        ->will($this->onConsecutiveCalls(['cn=groupA,dc=foobar', 'cn=groupB,dc=foobar'], [], [], [], []));
-
                $access->expects($this->any())
                        ->method('dn2groupname')
                        ->willReturnArgument(0);
@@ -770,8 +771,8 @@ class Group_LDAPTest extends TestCase {
        }
 
        public function testCreateGroupWithPlugin() {
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'createGroup'])
                        ->getMock();
 
@@ -797,8 +798,8 @@ class Group_LDAPTest extends TestCase {
        public function testCreateGroupFailing() {
                $this->expectException(\Exception::class);
 
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'createGroup'])
                        ->getMock();
 
@@ -816,8 +817,8 @@ class Group_LDAPTest extends TestCase {
        }
 
        public function testDeleteGroupWithPlugin() {
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'deleteGroup'])
                        ->getMock();
 
@@ -831,7 +832,7 @@ class Group_LDAPTest extends TestCase {
                        ->with('gid')
                        ->willReturn('result');
 
-               $mapper = $this->getMockBuilder('\OCA\User_LDAP\Mapping\GroupMapping')
+               $mapper = $this->getMockBuilder(GroupMapping::class)
                        ->setMethods(['unmap'])
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -852,8 +853,8 @@ class Group_LDAPTest extends TestCase {
        public function testDeleteGroupFailing() {
                $this->expectException(\Exception::class);
 
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'deleteGroup'])
                        ->getMock();
 
@@ -871,8 +872,8 @@ class Group_LDAPTest extends TestCase {
        }
 
        public function testAddToGroupWithPlugin() {
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'addToGroup'])
                        ->getMock();
 
@@ -898,8 +899,8 @@ class Group_LDAPTest extends TestCase {
        public function testAddToGroupFailing() {
                $this->expectException(\Exception::class);
 
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'addToGroup'])
                        ->getMock();
 
@@ -917,8 +918,8 @@ class Group_LDAPTest extends TestCase {
        }
 
        public function testRemoveFromGroupWithPlugin() {
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'removeFromGroup'])
                        ->getMock();
 
@@ -944,8 +945,8 @@ class Group_LDAPTest extends TestCase {
        public function testRemoveFromGroupFailing() {
                $this->expectException(\Exception::class);
 
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'removeFromGroup'])
                        ->getMock();
 
@@ -963,8 +964,8 @@ class Group_LDAPTest extends TestCase {
        }
 
        public function testGetGroupDetailsWithPlugin() {
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'getGroupDetails'])
                        ->getMock();
 
@@ -990,8 +991,8 @@ class Group_LDAPTest extends TestCase {
        public function testGetGroupDetailsFailing() {
                $this->expectException(\Exception::class);
 
-               /** @var GroupPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
-               $pluginManager = $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')
+               /** @var GroupPluginManager|MockObject $pluginManager */
+               $pluginManager = $this->getMockBuilder(GroupPluginManager::class)
                        ->setMethods(['implementsActions', 'getGroupDetails'])
                        ->getMock();