Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions lib/Controller/AdminGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> $apps List of app ids to enable
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
* @return DataResponse<Http::STATUS_OK|Http::STATUS_CONFLICT, list<empty>, 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)]
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
28 changes: 28 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}
}
}
}
}
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/types/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Loading