diff --git a/apps/user_ldap/lib/Group_Proxy.php b/apps/user_ldap/lib/Group_Proxy.php index 43997a09892d3..114902ff9bad7 100644 --- a/apps/user_ldap/lib/Group_Proxy.php +++ b/apps/user_ldap/lib/Group_Proxy.php @@ -29,13 +29,14 @@ namespace OCA\User_LDAP; use OC\ServerNotAvailableException; +use OCP\Group\Backend\IBatchMethodsBackend; use OCP\Group\Backend\IDeleteGroupBackend; use OCP\Group\Backend\IGetDisplayNameBackend; use OCP\Group\Backend\IGroupDetailsBackend; use OCP\Group\Backend\INamedBackend; use OCP\GroupInterface; -class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend { +class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend { private $backends = []; private ?Group_LDAP $refBackend = null; private Helper $helper; diff --git a/lib/private/Group/Database.php b/lib/private/Group/Database.php index 4e3703c6d1fa6..fee5d927c124b 100644 --- a/lib/private/Group/Database.php +++ b/lib/private/Group/Database.php @@ -33,6 +33,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Group\Backend\ABackend; use OCP\Group\Backend\IAddToGroupBackend; +use OCP\Group\Backend\IBatchMethodsBackend; use OCP\Group\Backend\ICountDisabledInGroup; use OCP\Group\Backend\ICountUsersBackend; use OCP\Group\Backend\ICreateGroupBackend; @@ -61,6 +62,7 @@ class Database extends ABackend implements IRemoveFromGroupBackend, ISetDisplayNameBackend, ISearchableGroupBackend, + IBatchMethodsBackend, INamedBackend { /** @var array */ private $groupCache = []; diff --git a/lib/private/Group/Manager.php b/lib/private/Group/Manager.php index 924b9a3b6a6dc..47475121ea0cf 100644 --- a/lib/private/Group/Manager.php +++ b/lib/private/Group/Manager.php @@ -42,6 +42,7 @@ use OC\Hooks\PublicEmitter; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Group\Backend\IBatchMethodsBackend; use OCP\Group\Backend\IGroupDetailsBackend; use OCP\Group\Events\BeforeGroupCreatedEvent; use OCP\Group\Events\GroupCreatedEvent; @@ -212,7 +213,7 @@ protected function getGroupObject($gid, $displayName = null) { * @param array $displayNames Array containing already know display name for a groupId * @return array */ - protected function getGroupsObject(array $gids, array $displayNames = []): array { + protected function getGroupsObjects(array $gids, array $displayNames = []): array { $backends = []; $groups = []; foreach ($gids as $gid) { @@ -224,7 +225,14 @@ protected function getGroupsObject(array $gids, array $displayNames = []): array foreach ($this->backends as $backend) { if ($backend instanceof IGroupDetailsBackend || $backend->implementsActions(GroupInterface::GROUP_DETAILS)) { /** @var IGroupDetailsBackend $backend */ - $groupDatas = $backend->getGroupsDetails($gids); + if ($backend instanceof IBatchMethodsBackend) { + $groupDatas = $backend->getGroupsDetails($gids); + } else { + $groupDatas = []; + foreach ($gids as $gid) { + $groupDatas[$gid] = $backend->getGroupDetails($gid); + } + } foreach ($groupDatas as $gid => $groupData) { if (!empty($groupData)) { // take the display name from the last backend that has a non-null one @@ -235,7 +243,11 @@ protected function getGroupsObject(array $gids, array $displayNames = []): array } } } else { - $existingGroups = $backend->groupsExists($gids); + if ($backend instanceof IBatchMethodsBackend) { + $existingGroups = $backend->groupsExists($gids); + } else { + $existingGroups = array_filter($gids, fn (string $gid): bool => $backend->groupExists($gid)); + } foreach ($existingGroups as $group) { $backends[$group][] = $backend; } @@ -243,7 +255,7 @@ protected function getGroupsObject(array $gids, array $displayNames = []): array } foreach ($gids as $gid) { if (count($backends[$gid]) === 0) { - continue; + continue; } $this->cachedGroups[$gid] = new Group($gid, $backends[$gid], $this->dispatcher, $this->userManager, $this, $displayNames[$gid]); $groups[$gid] = $this->cachedGroups[$gid]; @@ -295,7 +307,7 @@ public function search(string $search, ?int $limit = null, ?int $offset = 0) { $groups = []; foreach ($this->backends as $backend) { $groupIds = $backend->getGroups($search, $limit ?? -1, $offset ?? 0); - $newGroups = $this->getGroupsObject($groupIds); + $newGroups = $this->getGroupsObjects($groupIds); foreach ($newGroups as $groupId => $group) { $groups[$groupId] = $group; } diff --git a/lib/public/Group/Backend/ABackend.php b/lib/public/Group/Backend/ABackend.php index 4ce4e43055925..274b98655e465 100644 --- a/lib/public/Group/Backend/ABackend.php +++ b/lib/public/Group/Backend/ABackend.php @@ -31,7 +31,7 @@ /** * @since 14.0.0 */ -abstract class ABackend implements GroupInterface { +abstract class ABackend implements GroupInterface, IBatchMethodsBackend { /** * @deprecated 14.0.0 * @since 14.0.0 @@ -68,7 +68,7 @@ public function implementsActions($actions): bool { } /** - * @since 26.0.0 + * @since 28.0.0 */ public function groupsExists(array $gids): array { return array_values(array_filter( @@ -78,7 +78,7 @@ public function groupsExists(array $gids): array { } /** - * @since 26.0.0 + * @since 28.0.0 */ public function getGroupsDetails(array $gids): array { if (!($this instanceof IGroupDetailsBackend || $this->implementsActions(GroupInterface::GROUP_DETAILS))) { diff --git a/lib/public/Group/Backend/IBatchMethodsBackend.php b/lib/public/Group/Backend/IBatchMethodsBackend.php new file mode 100644 index 0000000000000..109f398677f03 --- /dev/null +++ b/lib/public/Group/Backend/IBatchMethodsBackend.php @@ -0,0 +1,61 @@ + + * + * @author Roeland Jago Douma + * @author Carl Schwan + * @author Côme Chilliet + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCP\Group\Backend; + +/** + * @brief Optional interface for group backends + * @since 28.0.0 + */ +interface IBatchMethodsBackend { + /** + * @brief Batch method to check if a list of groups exists + * + * The default implementation in ABackend will just call groupExists in + * a loop. But a GroupBackend implementation should provides a more optimized + * override this method to provide a more optimized way to execute this operation. + * + * @param list $gids + * @return list the list of group that exists + * @since 28.0.0 + */ + public function groupsExists(array $gids): array; + + /** + * @brief Batch method to get the group details of a list of groups + * + * The default implementation in ABackend will just call getGroupDetail in + * a loop. But a GroupBackend implementation should provides a more optimized + * override this method to provide a more optimized way to execute this operation. + * + * @throw \RuntimeException if called on a backend that doesn't implements IGroupDetailsBackend + * + * @return array + * @since 28.0.0 + */ + public function getGroupsDetails(array $gids): array; +} diff --git a/lib/public/Group/Backend/IGroupDetailsBackend.php b/lib/public/Group/Backend/IGroupDetailsBackend.php index a102c8c2eecdd..851c10388e040 100644 --- a/lib/public/Group/Backend/IGroupDetailsBackend.php +++ b/lib/public/Group/Backend/IGroupDetailsBackend.php @@ -41,19 +41,4 @@ interface IGroupDetailsBackend { * @since 14.0.0 */ public function getGroupDetails(string $gid): array; - - - /** - * @brief Batch method to get the group details of a list of groups - * - * The default implementation in ABackend will just call getGroupDetail in - * a loop. But a GroupBackend implementation should provides a more optimized - * override this method to provide a more optimized way to execute this operation. - * - * @throw \RuntimeException if called on a backend that doesn't implements IGroupDetailsBackend - * - * @return array - * @since 26.0.0 - */ - public function getGroupsDetails(array $gids): array; } diff --git a/lib/public/GroupInterface.php b/lib/public/GroupInterface.php index 50374e5a9491f..599a0eb2ff077 100644 --- a/lib/public/GroupInterface.php +++ b/lib/public/GroupInterface.php @@ -107,19 +107,6 @@ public function getGroups(string $search = '', int $limit = -1, int $offset = 0) */ public function groupExists($gid); - /** - * @brief Batch method to check if a list of groups exists - * - * The default implementation in ABackend will just call groupExists in - * a loop. But a GroupBackend implementation should provides a more optimized - * override this method to provide a more optimized way to execute this operation. - * - * @param list $gids - * @return list the list of group that exists - * @since 25.0.0 - */ - public function groupsExists(array $gids): array; - /** * @brief Get a list of user ids in a group matching the given search parameters. *