-
Notifications
You must be signed in to change notification settings - Fork 68
feat: automatically create a teamfolder upon team creation #2675
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| /* extracted by css-entry-points-plugin */ | ||
| @import './dashboard-BQNkvnbt.chunk.css'; | ||
| @import './logger-CNf09jSX.chunk.css'; | ||
| @import './NcActionRouter-vYFtIOzD-BuuqIKuh.chunk.css'; | ||
| @import './NcAvatar-DX-Nk9Es-DvtEJIrI.chunk.css'; | ||
| @import './dashboard-DsW2gQLu.chunk.css'; | ||
| @import './index-UyurHCn6.chunk.css'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| /* extracted by css-entry-points-plugin */ | ||
| @import './settings-admin-D9UlOAjC.chunk.css'; | ||
| @import './logger-CNf09jSX.chunk.css'; | ||
| @import './NcCheckboxRadioSwitch-BVTMQSAg-rNRsAuW3.chunk.css'; | ||
| @import './NcSettingsSection-DmfxX2se-D7mIRwIy.chunk.css'; | ||
| @import './settings-admin-BEZBKJuE.chunk.css'; | ||
| @import './index-UyurHCn6.chunk.css'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| /* extracted by css-entry-points-plugin */ | ||
| @import './settings-team-folders-Kw2lcKKl.chunk.css'; | ||
| @import './index-UyurHCn6.chunk.css'; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,139 @@ | ||||||
| <?php | ||||||
|
|
||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| /** | ||||||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||||||
| */ | ||||||
|
|
||||||
| namespace OCA\Circles\Controller; | ||||||
|
|
||||||
| use OCA\Circles\Db\CircleRequest; | ||||||
| use OCA\Circles\Exceptions\CircleNotFoundException; | ||||||
| use OCA\Circles\Exceptions\InsufficientPermissionException; | ||||||
| use OCA\Circles\Service\PermissionService; | ||||||
| use OCA\Circles\Service\TeamFolderPolicy; | ||||||
| use OCP\AppFramework\Http; | ||||||
| use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||||||
| use OCP\AppFramework\Http\DataResponse; | ||||||
| use OCP\AppFramework\OCS\OCSException; | ||||||
| use OCP\AppFramework\OCS\OCSNotFoundException; | ||||||
| use OCP\AppFramework\OCSController; | ||||||
| use OCP\IRequest; | ||||||
| use OCP\IUser; | ||||||
| use OCP\IUserSession; | ||||||
| use OCP\Teams\ITeamManager; | ||||||
| use OCP\Teams\Team; | ||||||
|
|
||||||
| class TeamFolderController extends OCSController { | ||||||
| public function __construct( | ||||||
| string $appName, | ||||||
| IRequest $request, | ||||||
| private readonly ITeamManager $teamManager, | ||||||
| private readonly TeamFolderPolicy $policy, | ||||||
| private readonly CircleRequest $circleRequest, | ||||||
| private readonly PermissionService $permissionService, | ||||||
| private readonly IUserSession $userSession, | ||||||
| ) { | ||||||
| parent::__construct($appName, $request); | ||||||
| } | ||||||
|
|
||||||
| #[NoAdminRequired] | ||||||
| public function getTeamFolder(string $circleId): DataResponse { | ||||||
| $this->assertAuthenticatedUserIsMember($circleId); | ||||||
| $folder = $this->getProvider()->getTeamFolder($circleId); | ||||||
|
Check failure on line 45 in lib/Controller/TeamFolderController.php
|
||||||
| if ($folder === null) { | ||||||
| throw new OCSNotFoundException('No team folder linked to this team'); | ||||||
| } | ||||||
|
|
||||||
| return new DataResponse($folder->jsonSerialize()); | ||||||
| } | ||||||
|
|
||||||
| #[NoAdminRequired] | ||||||
| public function upgradeTeamFolder(string $circleId): DataResponse { | ||||||
| $circle = $this->getCircle($circleId); | ||||||
| $this->assertAuthenticatedUserIsTeamAdmin($circleId); | ||||||
| $folder = $this->getProvider()->createTeamFolder( | ||||||
|
Check failure on line 57 in lib/Controller/TeamFolderController.php
|
||||||
| new Team( | ||||||
| teamId: $circle->getSingleId(), | ||||||
| displayName: $circle->getDisplayName(), | ||||||
| link: null, | ||||||
| ), | ||||||
| $this->policy->getDefaultQuota(), | ||||||
| ); | ||||||
|
|
||||||
| return new DataResponse([ | ||||||
| 'success' => true, | ||||||
| 'folderId' => $folder->getId(), | ||||||
| 'folder' => $folder->jsonSerialize(), | ||||||
| ]); | ||||||
| } | ||||||
|
|
||||||
| #[NoAdminRequired] | ||||||
| public function unlinkTeamFolder(string $circleId, bool $deleteFolder = false): DataResponse { | ||||||
| $this->assertAuthenticatedUserIsTeamOwner($circleId); | ||||||
| $provider = $this->getProvider(); | ||||||
| if ($deleteFolder) { | ||||||
| $changed = $provider->removeTeamFolder($circleId); | ||||||
|
Check failure on line 78 in lib/Controller/TeamFolderController.php
|
||||||
| } else { | ||||||
| $changed = $provider->unlinkTeamFolder($circleId) !== null; | ||||||
|
Check failure on line 80 in lib/Controller/TeamFolderController.php
|
||||||
| } | ||||||
| if (!$changed) { | ||||||
| throw new OCSNotFoundException('No team folder linked to this team'); | ||||||
| } | ||||||
|
|
||||||
| return new DataResponse(['success' => true]); | ||||||
| } | ||||||
|
|
||||||
| private function getProvider(): \OCP\Teams\ITeamFolderProvider { | ||||||
|
Check failure on line 89 in lib/Controller/TeamFolderController.php
|
||||||
| $provider = $this->teamManager->getTeamFolderProvider(); | ||||||
|
Check failure on line 90 in lib/Controller/TeamFolderController.php
|
||||||
| if ($provider === null) { | ||||||
| throw new OCSNotFoundException('No team folder provider is enabled'); | ||||||
| } | ||||||
|
|
||||||
| return $provider; | ||||||
| } | ||||||
|
|
||||||
| private function getCircle(string $circleId): \OCA\Circles\Model\Circle { | ||||||
| try { | ||||||
| return $this->circleRequest->getCircle($circleId); | ||||||
| } catch (CircleNotFoundException) { | ||||||
| throw new OCSNotFoundException('Team not found'); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private function assertAuthenticatedUserIsMember(string $circleId): void { | ||||||
| try { | ||||||
| $this->permissionService->userMustBeMember($this->getAuthenticatedUser()->getUID(), $circleId); | ||||||
| } catch (InsufficientPermissionException $e) { | ||||||
| throw new OCSException($e->getMessage(), Http::STATUS_FORBIDDEN); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private function assertAuthenticatedUserIsTeamAdmin(string $circleId): void { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| try { | ||||||
| $this->permissionService->userMustBeAtLeastTeamAdminOrServerAdmin($this->getAuthenticatedUser()->getUID(), $circleId); | ||||||
| } catch (InsufficientPermissionException $e) { | ||||||
| throw new OCSException($e->getMessage(), Http::STATUS_FORBIDDEN); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private function assertAuthenticatedUserIsTeamOwner(string $circleId): void { | ||||||
| try { | ||||||
| $member = $this->permissionService->userMustBeMember($this->getAuthenticatedUser()->getUID(), $circleId); | ||||||
| $this->permissionService->memberMustBeOwner($member); | ||||||
| } catch (InsufficientPermissionException $e) { | ||||||
| throw new OCSException($e->getMessage(), Http::STATUS_FORBIDDEN); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private function getAuthenticatedUser(): IUser { | ||||||
| $user = $this->userSession->getUser(); | ||||||
| if ($user === null) { | ||||||
| throw new OCSException('Authentication required', Http::STATUS_UNAUTHORIZED); | ||||||
| } | ||||||
|
|
||||||
| return $user; | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Circles\Listeners; | ||
|
|
||
| use OCA\Circles\Events\CreatingCircleEvent; | ||
| use OCA\Circles\Events\DestroyingCircleEvent; | ||
| use OCA\Circles\Service\TeamFolderPolicy; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use OCP\Teams\ITeamManager; | ||
| use OCP\Teams\Team; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
| * @template-implements IEventListener<CreatingCircleEvent|DestroyingCircleEvent> | ||
| */ | ||
| class TeamFolderLifecycleListener implements IEventListener { | ||
| public function __construct( | ||
| private readonly ITeamManager $teamManager, | ||
| private readonly TeamFolderPolicy $policy, | ||
| private readonly LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function handle(Event $event): void { | ||
| if ($event instanceof DestroyingCircleEvent) { | ||
| $provider = $this->teamManager->getTeamFolderProvider(); | ||
|
Check failure on line 35 in lib/Listeners/TeamFolderLifecycleListener.php
|
||
| if ($provider === null) { | ||
| return; | ||
| } | ||
|
|
||
| // The circle (team) is being destroyed entirely, so remove the | ||
| // team folder and its contents rather than unlinking it. Unlinking | ||
| // would leave an orphaned folder that no team owns anymore and | ||
| // that is hard for an admin to discover and clean up. | ||
| $circle = $event->getCircle(); | ||
| $provider->removeTeamFolder($circle->getSingleId()); | ||
| return; | ||
| } | ||
|
|
||
| if (!$event instanceof CreatingCircleEvent) { | ||
| return; | ||
| } | ||
|
|
||
| $circle = $event->getCircle(); | ||
| if (!$this->policy->shouldCreateTeamFolder($circle)) { | ||
| return; | ||
| } | ||
|
|
||
| $provider = $this->teamManager->getTeamFolderProvider(); | ||
|
Check failure on line 58 in lib/Listeners/TeamFolderLifecycleListener.php
|
||
| if ($provider === null) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $provider->createTeamFolder( | ||
| new Team( | ||
| teamId: $circle->getSingleId(), | ||
| displayName: $circle->getDisplayName(), | ||
| link: null, | ||
| ), | ||
| $this->policy->getDefaultQuota(), | ||
| ); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->error('Failed to auto-create team folder', [ | ||
| 'teamId' => $circle->getSingleId(), | ||
| 'exception' => $e, | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a specific reason
upgradeTeamFolderonly requires admin level whileunlinkTeamFolderrequires owner level? not sure if this was by design, just raising the question