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": {