|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OC\Group; |
| 10 | + |
| 11 | +use OCP\EventDispatcher\IEventDispatcher; |
| 12 | +use OCP\IGroup; |
| 13 | +use OCP\IGroupManager; |
| 14 | +use OCP\IUser; |
| 15 | +use OCP\IUserManager; |
| 16 | +use OCP\Server; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +class LazyGroup implements IGroup { |
| 20 | + private ?IGroup $group = null; |
| 21 | + private ?bool $isDeleted = null; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private string $gid, |
| 25 | + private IGroupManager $groupManager, |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + #[\Override] |
| 30 | + public function getGID(): string { |
| 31 | + return $this->gid; |
| 32 | + } |
| 33 | + |
| 34 | + private function getGroup(): IGroup { |
| 35 | + if ($this->group === null) { |
| 36 | + $this->group = $this->groupManager->get($this->gid); |
| 37 | + ; |
| 38 | + } |
| 39 | + if ($this->group === null) { |
| 40 | + Server::get(LoggerInterface::class)->debug('Trying to use the deleted group: "' . $this->gid . '"', ['app' => 'core']); |
| 41 | + $this->isDeleted = true; |
| 42 | + $this->group = new Group($this->gid, [], Server::get(IEventDispatcher::class), Server::get(IUserManager::class)); |
| 43 | + } else { |
| 44 | + $this->isDeleted = false; |
| 45 | + } |
| 46 | + return $this->group; |
| 47 | + } |
| 48 | + |
| 49 | + #[\Override] |
| 50 | + public function getDisplayName(): string { |
| 51 | + // Use display name cache from IGroupManager |
| 52 | + return $this->groupManager->getDisplayName($this->gid); |
| 53 | + } |
| 54 | + |
| 55 | + #[\Override] |
| 56 | + public function setDisplayName(string $displayName): bool { |
| 57 | + return $this->group->setDisplayName($displayName); |
| 58 | + } |
| 59 | + |
| 60 | + #[\Override] |
| 61 | + public function getUsers(): array { |
| 62 | + return $this->getGroup()->getUsers(); |
| 63 | + } |
| 64 | + |
| 65 | + #[\Override] |
| 66 | + public function inGroup(IUser $user): bool { |
| 67 | + return $this->getGroup()->inGroup($user); |
| 68 | + } |
| 69 | + |
| 70 | + #[\Override] |
| 71 | + public function addUser(IUser $user): void { |
| 72 | + $this->getGroup()->addUser($user); |
| 73 | + } |
| 74 | + |
| 75 | + #[\Override] |
| 76 | + public function removeUser(IUser $user): void { |
| 77 | + $this->getGroup()->removeUser($user); |
| 78 | + } |
| 79 | + |
| 80 | + #[\Override] |
| 81 | + public function searchUsers(string $search, ?int $limit = null, ?int $offset = null): array { |
| 82 | + return $this->getGroup()->searchUsers($search, $limit, $offset); |
| 83 | + } |
| 84 | + |
| 85 | + #[\Override] |
| 86 | + public function count($search = ''): int|bool { |
| 87 | + return $this->getGroup()->count($search); |
| 88 | + } |
| 89 | + |
| 90 | + #[\Override] |
| 91 | + public function countDisabled(): int|bool { |
| 92 | + return $this->getGroup()->countDisabled(); |
| 93 | + } |
| 94 | + |
| 95 | + #[\Override] |
| 96 | + public function searchDisplayName(string $search, ?int $limit = null, ?int $offset = null): array { |
| 97 | + return $this->getGroup()->searchDisplayName($search, $limit, $offset); |
| 98 | + } |
| 99 | + |
| 100 | + #[\Override] |
| 101 | + public function getBackendNames(): array { |
| 102 | + return $this->getGroup()->getBackendNames(); |
| 103 | + } |
| 104 | + |
| 105 | + #[\Override] |
| 106 | + public function delete(): bool { |
| 107 | + return $this->getGroup()->delete(); |
| 108 | + } |
| 109 | + |
| 110 | + #[\Override] |
| 111 | + public function canRemoveUser(): bool { |
| 112 | + return $this->getGroup()->canRemoveUser(); |
| 113 | + } |
| 114 | + |
| 115 | + #[\Override] |
| 116 | + public function canAddUser(): bool { |
| 117 | + return $this->getGroup()->canAddUser(); |
| 118 | + } |
| 119 | + |
| 120 | + #[\Override] |
| 121 | + public function hideFromCollaboration(): bool { |
| 122 | + return $this->getGroup()->hideFromCollaboration(); |
| 123 | + } |
| 124 | + |
| 125 | + #[\Override] |
| 126 | + public function isDeleted(): bool { |
| 127 | + if ($this->isDeleted === null) { |
| 128 | + $this->getGroup(); |
| 129 | + } |
| 130 | + return $this->isDeleted === true ? true : $this->getGroup()->isDeleted(); |
| 131 | + } |
| 132 | +} |
0 commit comments