]> source.dussan.org Git - nextcloud-server.git/commitdiff
Don't loop over all groups to check for subadmins 20677/head
authorJoas Schilling <coding@schilljs.com>
Mon, 27 Apr 2020 06:37:53 +0000 (08:37 +0200)
committerJoas Schilling <coding@schilljs.com>
Mon, 27 Apr 2020 06:37:53 +0000 (08:37 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/SubAdmin.php

index 9a758ac4423c4a02b81576fc06cabff2d1bdd85c..890bcf67b3bcb90554dce8e6a3beb2e657b86e06 100644 (file)
@@ -110,6 +110,25 @@ class SubAdmin extends PublicEmitter implements ISubAdmin {
         * @return IGroup[]
         */
        public function getSubAdminsGroups(IUser $user): array {
+               $groupIds = $this->getSubAdminsGroupIds($user);
+
+               $groups = [];
+               foreach ($groupIds as $groupId) {
+                       $group = $this->groupManager->get($groupId);
+                       if ($group !== null) {
+                               $groups[$group->getGID()] = $group;
+                       }
+               }
+
+               return $groups;
+       }
+
+       /**
+        * Get group ids of a SubAdmin
+        * @param IUser $user the SubAdmin
+        * @return string[]
+        */
+       public function getSubAdminsGroupIds(IUser $user): array {
                $qb = $this->dbConn->getQueryBuilder();
 
                $result = $qb->select('gid')
@@ -119,10 +138,7 @@ class SubAdmin extends PublicEmitter implements ISubAdmin {
 
                $groups = [];
                while ($row = $result->fetch()) {
-                       $group = $this->groupManager->get($row['gid']);
-                       if (!is_null($group)) {
-                               $groups[$group->getGID()] = $group;
-                       }
+                       $groups[] = $row['gid'];
                }
                $result->closeCursor();
 
@@ -256,13 +272,10 @@ class SubAdmin extends PublicEmitter implements ISubAdmin {
                        return false;
                }
 
-               $accessibleGroups = $this->getSubAdminsGroups($subadmin);
-               foreach ($accessibleGroups as $accessibleGroup) {
-                       if ($accessibleGroup->inGroup($user)) {
-                               return true;
-                       }
-               }
-               return false;
+               $accessibleGroups = $this->getSubAdminsGroupIds($subadmin);
+               $userGroups = $this->groupManager->getUserGroupIds($user);
+
+               return !empty(array_intersect($accessibleGroups, $userGroups));
        }
 
        /**