|
13 | 13 |
|
14 | 14 | use Exception; |
15 | 15 | use OCA\Circles\Service\SyncService; |
| 16 | +use OCP\App\IAppManager; |
16 | 17 | use OCP\EventDispatcher\Event; |
17 | 18 | use OCP\EventDispatcher\IEventListener; |
| 19 | +use OCP\Files\Config\IMountProviderCollection; |
| 20 | +use OCP\Files\Config\IUserMountCache; |
18 | 21 | use OCP\Group\Events\UserRemovedEvent; |
| 22 | +use OCP\Server; |
| 23 | +use Psr\Log\LoggerInterface; |
| 24 | +use Throwable; |
19 | 25 |
|
20 | | -/** @template-implements IEventListener<UserRemovedEvent|Event> */ |
| 26 | +/** @template-implements IEventListener<UserRemovedEvent> */ |
21 | 27 | class GroupMemberRemoved implements IEventListener { |
22 | 28 | /** @var SyncService */ |
23 | 29 | private $syncService; |
24 | 30 |
|
25 | | - public function __construct(SyncService $syncService) { |
| 31 | + /** @var IAppManager */ |
| 32 | + private $appManager; |
| 33 | + |
| 34 | + /** @var IUserMountCache */ |
| 35 | + private $userMountCache; |
| 36 | + |
| 37 | + /** @var IMountProviderCollection */ |
| 38 | + private $mountProviderCollection; |
| 39 | + |
| 40 | + /** @var LoggerInterface */ |
| 41 | + private $logger; |
| 42 | + |
| 43 | + public function __construct( |
| 44 | + SyncService $syncService, |
| 45 | + IAppManager $appManager, |
| 46 | + IUserMountCache $userMountCache, |
| 47 | + IMountProviderCollection $mountProviderCollection, |
| 48 | + LoggerInterface $logger, |
| 49 | + ) { |
26 | 50 | $this->syncService = $syncService; |
| 51 | + $this->appManager = $appManager; |
| 52 | + $this->userMountCache = $userMountCache; |
| 53 | + $this->mountProviderCollection = $mountProviderCollection; |
| 54 | + $this->logger = $logger; |
27 | 55 | } |
28 | 56 |
|
| 57 | + #[\Override] |
29 | 58 | public function handle(Event $event): void { |
30 | 59 | if (!($event instanceof UserRemovedEvent)) { |
31 | 60 | return; |
32 | 61 | } |
33 | 62 |
|
34 | | - $group = $event->getGroup(); |
35 | 63 | $user = $event->getUser(); |
| 64 | + $group = $event->getGroup(); |
| 65 | + |
36 | 66 | try { |
37 | 67 | $this->syncService->groupMemberRemoved($group->getGID(), $user->getUID()); |
38 | 68 | } catch (Exception $e) { |
39 | 69 | } |
| 70 | + |
| 71 | + if (!$this->groupHasAssociatedGroupFolder($group->getGID())) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + // refresh list of mounts for user |
| 77 | + $mounts = $this->mountProviderCollection->getMountsForUser($user); |
| 78 | + $mounts[] = $this->mountProviderCollection->getHomeMountForUser($user); |
| 79 | + $this->userMountCache->registerMounts($user, $mounts); |
| 80 | + } catch (Exception $e) { |
| 81 | + $this->logger->debug('Failed to refresh mounts for user ' . $user->getUID(), ['exception' => $e]); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private function groupHasAssociatedGroupFolder(string $groupId): bool { |
| 86 | + if (!$this->appManager->isEnabledForUser('groupfolders')) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + $folderManager = Server::get(\OCA\GroupFolders\Folder\FolderManager::class); |
| 92 | + return $folderManager->hasFolderForGroup($groupId); |
| 93 | + } catch (Throwable $e) { |
| 94 | + $this->logger->debug('Failed to check if group ' . $groupId . ' has an associated team folder', ['exception' => $e]); |
| 95 | + return false; |
| 96 | + } |
40 | 97 | } |
41 | 98 | } |
0 commit comments