From daec486972eb06cda1e798cbcbe4070f6facfd97 Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Fri, 13 Nov 2026 20:11:23 -0400 Subject: [PATCH 1/3] fix: stop subadmins from adding users of other groups to their groups Signed-off-by: YvesCesar --- lib/AppInfo/Application.php | 3 ++ lib/Controller/AdminGroupController.php | 11 +++++ lib/Listener/BeforeUserAddedListener.php | 61 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 lib/Listener/BeforeUserAddedListener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index adc4c77..ba0ede9 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -8,11 +8,13 @@ namespace OCA\AdminGroupManager\AppInfo; +use OCA\AdminGroupManager\Listener\BeforeUserAddedListener; use OCA\AdminGroupManager\Middleware\InjectionMiddleware; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Group\Events\BeforeUserAddedEvent; /** * @codeCoverageIgnore @@ -31,5 +33,6 @@ public function boot(IBootContext $context): void { #[\Override] public function register(IRegistrationContext $context): void { $context->registerMiddleWare(InjectionMiddleware::class); + $context->registerEventListener(BeforeUserAddedEvent::class, BeforeUserAddedListener::class); } } diff --git a/lib/Controller/AdminGroupController.php b/lib/Controller/AdminGroupController.php index 8ae029b..5b03461 100644 --- a/lib/Controller/AdminGroupController.php +++ b/lib/Controller/AdminGroupController.php @@ -113,6 +113,9 @@ public function setEnabled( } $users = $group->getUsers(); foreach ($users as $user) { + if ($this->isAdministrator($user)) { + continue; + } $user->setEnabled((bool)$enabled); } return new DataResponse(); @@ -202,6 +205,9 @@ private function addGroup(string $groupid, string $displayname = ''): IGroup { if ($group instanceof IGroup) { $users = $group->getUsers(); foreach ($users as $user) { + if ($this->isAdministrator($user)) { + continue; + } $user->setEnabled(true); } } else { @@ -218,6 +224,11 @@ private function addGroup(string $groupid, string $displayname = ''): IGroup { return $group; } + private function isAdministrator(IUser $user): bool { + return $this->groupManager->isAdmin($user->getUID()) + || $this->groupManager->isDelegatedAdmin($user->getUID()); + } + private function setGroupQuota(string $groupId, string $quota): void { $quota = \OCP\Util::computerFileSize($quota); $this->appConfig->setValueString('groupquota', 'quota_' . $groupId, (string)$quota); diff --git a/lib/Listener/BeforeUserAddedListener.php b/lib/Listener/BeforeUserAddedListener.php new file mode 100644 index 0000000..f9ddb03 --- /dev/null +++ b/lib/Listener/BeforeUserAddedListener.php @@ -0,0 +1,61 @@ + + */ +class BeforeUserAddedListener implements IEventListener { + public function __construct( + private IUserSession $userSession, + private IGroupManager $groupManager, + private ISubAdmin $subAdmin, + ) { + } + + public function handle(Event $event): void { + if (!$event instanceof BeforeUserAddedEvent) { + return; + } + $author = $this->userSession->getUser(); + if (!$author instanceof IUser) { + return; + } + if ($this->groupManager->isAdmin($author->getUID()) || $this->groupManager->isDelegatedAdmin($author->getUID())) { + return; + } + if ($this->canBeManagedBy($event->getUser(), $author)) { + return; + } + throw new OCSException('This user cannot be added to the group', Http::STATUS_FORBIDDEN); + } + + private function canBeManagedBy(IUser $target, IUser $author): bool { + $targetGroupIds = $this->groupManager->getUserGroupIds($target); + if ($targetGroupIds === []) { + return $target->getLastLogin() === 0 && !$this->subAdmin->isSubAdmin($target); + } + $managedGroupIds = array_map( + static fn (IGroup $group): string => $group->getGID(), + $this->subAdmin->getSubAdminsGroups($author), + ); + return array_diff($targetGroupIds, $managedGroupIds) === []; + } +} From 90ab1d364cd67f7407200255018a60b1cd7a3942 Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Fri, 13 Nov 2026 20:12:01 -0400 Subject: [PATCH 2/3] fix: refuse to provision a group over an account it does not own Signed-off-by: YvesCesar --- lib/Controller/AdminGroupController.php | 40 ++++++++++++++++++++----- openapi.json | 28 +++++++++++++++++ src/types/openapi/openapi.ts | 14 +++++++++ 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/lib/Controller/AdminGroupController.php b/lib/Controller/AdminGroupController.php index 5b03461..3d77cd3 100644 --- a/lib/Controller/AdminGroupController.php +++ b/lib/Controller/AdminGroupController.php @@ -60,10 +60,12 @@ public function __construct( * @param string $email Email of admin * @param string $quota Group quota in "human readable" format. Default value is 1Gb. * @param list $apps List of app ids to enable - * @return DataResponse, array{}> + * @return DataResponse, array{}> + * @throws OCSException * * 200: OK * 401: Unauthorized + * 409: An account with this ID exists and is not the admin of this group */ #[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/admin-group', requirements: ['apiVersion' => '(v1)'])] #[AuthorizedAdminSetting(settings:Users::class)] @@ -76,13 +78,24 @@ public function createAdminGroup( string $quota = '1Gb', array $apps = [], ): DataResponse { + if ($email !== '' && !$this->mailer->validateMailAddress($email)) { + throw new OCSException('Invalid email'); + } + try { + $this->userManager->validateUserId($groupid); + } catch (\InvalidArgumentException $exception) { + throw new OCSException($exception->getMessage(), 101); + } + if (!$this->canProvision($groupid)) { + return new DataResponse([], Http::STATUS_CONFLICT); + } $group = $this->addGroup($groupid, $displayname); - $this->setGroupQuota($groupid, $quota); - $this->enableApps($apps, $groupid); $user = $this->createUser($groupid, $displayname, $email); - $this->setUserQuota($user, $quota); $group->addUser($user); $this->addSubAdmin($user, $group); + $this->setGroupQuota($groupid, $quota); + $this->setUserQuota($user, $quota); + $this->enableApps($apps, $groupid); return new DataResponse(); } @@ -186,9 +199,6 @@ private function createUser($userId, $displayName, $email): IUser { } } if ($email !== '') { - if (!$this->mailer->validateMailAddress($email)) { - throw new OCSException('Invalid email'); - } $user->setSystemEMailAddress($email); } return $user; @@ -224,6 +234,22 @@ private function addGroup(string $groupid, string $displayname = ''): IGroup { return $group; } + private function canProvision(string $groupid): bool { + if (strtolower($groupid) === 'admin') { + return false; + } + $existingUser = $this->userManager->get($groupid); + if (!$existingUser instanceof IUser) { + return true; + } + if ($this->isAdministrator($existingUser)) { + return false; + } + $existingGroup = $this->groupManager->get($groupid); + return $existingGroup instanceof IGroup + && $this->subAdmin->isSubAdminOfGroup($existingUser, $existingGroup); + } + private function isAdministrator(IUser $user): bool { return $this->groupManager->isAdmin($user->getUID()) || $this->groupManager->isDelegatedAdmin($user->getUID()); diff --git a/openapi.json b/openapi.json index d5e52ed..a7cbe3b 100644 --- a/openapi.json +++ b/openapi.json @@ -157,6 +157,34 @@ } } } + }, + "409": { + "description": "An account with this ID exists and is not the admin of this group", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } } } } diff --git a/src/types/openapi/openapi.ts b/src/types/openapi/openapi.ts index ae6391c..dee222e 100644 --- a/src/types/openapi/openapi.ts +++ b/src/types/openapi/openapi.ts @@ -141,6 +141,20 @@ export interface operations { }; }; }; + /** @description An account with this ID exists and is not the admin of this group */ + 409: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + ocs: { + meta: components["schemas"]["OCSMeta"]; + data: unknown; + }; + }; + }; + }; }; }; "admin_group-set-enabled": { From b90284209ca56cc01cf7188a42b07914f3bc0b3e Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Sat, 14 Nov 2026 10:33:16 -0400 Subject: [PATCH 3/3] fix: mark the subadmin listener handler as an override Signed-off-by: YvesCesar --- lib/Listener/BeforeUserAddedListener.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Listener/BeforeUserAddedListener.php b/lib/Listener/BeforeUserAddedListener.php index f9ddb03..bdd81d5 100644 --- a/lib/Listener/BeforeUserAddedListener.php +++ b/lib/Listener/BeforeUserAddedListener.php @@ -30,6 +30,7 @@ public function __construct( ) { } + #[\Override] public function handle(Event $event): void { if (!$event instanceof BeforeUserAddedEvent) { return;