-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add batch methods in user backends #32912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3270b7f
Add batch methods in user backends
CarlSchwan a5fa1e7
Fix psalm errors about groupExists return type
come-nc 2c8b415
Move new methods to a new interface in OCP
come-nc 35069ad
Fix psalm spotted type errors
come-nc d18bb7e
Build query once instead of in-loop
come-nc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| * @author Vincent Petry <[email protected]> | ||
| * @author Vinicius Cubas Brand <[email protected]> | ||
| * @author voxsim "Simon Vocella" | ||
| * @author Carl Schwan <[email protected]> | ||
| * | ||
| * @license AGPL-3.0 | ||
| * | ||
|
|
@@ -41,6 +42,8 @@ | |
|
|
||
| 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; | ||
| use OCP\GroupInterface; | ||
|
|
@@ -74,10 +77,10 @@ class Manager extends PublicEmitter implements IGroupManager { | |
| private IEventDispatcher $dispatcher; | ||
| private LoggerInterface $logger; | ||
|
|
||
| /** @var \OC\Group\Group[] */ | ||
| /** @var array<string, IGroup> */ | ||
| private $cachedGroups = []; | ||
|
|
||
| /** @var (string[])[] */ | ||
| /** @var array<string, list<string>> */ | ||
| private $cachedUserGroups = []; | ||
|
|
||
| /** @var \OC\SubAdmin */ | ||
|
|
@@ -185,7 +188,7 @@ protected function getGroupObject($gid, $displayName = null) { | |
| if ($backend->implementsActions(Backend::GROUP_DETAILS)) { | ||
| $groupData = $backend->getGroupDetails($gid); | ||
| if (is_array($groupData) && !empty($groupData)) { | ||
| // take the display name from the first backend that has a non-null one | ||
| // take the display name from the last backend that has a non-null one | ||
| if (is_null($displayName) && isset($groupData['displayName'])) { | ||
| $displayName = $groupData['displayName']; | ||
| } | ||
|
|
@@ -198,10 +201,68 @@ protected function getGroupObject($gid, $displayName = null) { | |
| if (count($backends) === 0) { | ||
| return null; | ||
| } | ||
| /** @var GroupInterface[] $backends */ | ||
| $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName); | ||
| return $this->cachedGroups[$gid]; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Batch method to create group objects | ||
| * | ||
| * @param list<string> $gids List of groupIds for which we want to create a IGroup object | ||
| * @param array<string, string> $displayNames Array containing already know display name for a groupId | ||
| * @return array<string, IGroup> | ||
| */ | ||
| protected function getGroupsObjects(array $gids, array $displayNames = []): array { | ||
| $backends = []; | ||
| $groups = []; | ||
| foreach ($gids as $gid) { | ||
| $backends[$gid] = []; | ||
| if (!isset($displayNames[$gid])) { | ||
| $displayNames[$gid] = null; | ||
| } | ||
| } | ||
| foreach ($this->backends as $backend) { | ||
| if ($backend instanceof IGroupDetailsBackend || $backend->implementsActions(GroupInterface::GROUP_DETAILS)) { | ||
|
||
| /** @var IGroupDetailsBackend $backend */ | ||
| if ($backend instanceof IBatchMethodsBackend) { | ||
| $groupDatas = $backend->getGroupsDetails($gids); | ||
| } else { | ||
| $groupDatas = []; | ||
| foreach ($gids as $gid) { | ||
| $groupDatas[$gid] = $backend->getGroupDetails($gid); | ||
| } | ||
| } | ||
| foreach ($groupDatas as $gid => $groupData) { | ||
come-nc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!empty($groupData)) { | ||
| // take the display name from the last backend that has a non-null one | ||
| if (isset($groupData['displayName'])) { | ||
| $displayNames[$gid] = $groupData['displayName']; | ||
| } | ||
| $backends[$gid][] = $backend; | ||
| } | ||
| } | ||
| } else { | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| foreach ($gids as $gid) { | ||
| if (count($backends[$gid]) === 0) { | ||
| continue; | ||
| } | ||
| $this->cachedGroups[$gid] = new Group($gid, $backends[$gid], $this->dispatcher, $this->userManager, $this, $displayNames[$gid]); | ||
| $groups[$gid] = $this->cachedGroups[$gid]; | ||
| } | ||
| return $groups; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $gid | ||
| * @return bool | ||
|
|
@@ -246,13 +307,9 @@ 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); | ||
| foreach ($groupIds as $groupId) { | ||
| $aGroup = $this->get($groupId); | ||
| if ($aGroup instanceof IGroup) { | ||
| $groups[$groupId] = $aGroup; | ||
| } else { | ||
| $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); | ||
| } | ||
| $newGroups = $this->getGroupsObjects($groupIds); | ||
| foreach ($newGroups as $groupId => $group) { | ||
| $groups[$groupId] = $group; | ||
| } | ||
| if (!is_null($limit) and $limit <= 0) { | ||
| return array_values($groups); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.