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
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
51 changes: 44 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 @@ -113,6 +126,9 @@ public function setEnabled(
}
$users = $group->getUsers();
foreach ($users as $user) {
if ($this->isAdministrator($user)) {
continue;
}
$user->setEnabled((bool)$enabled);
}
return new DataResponse();
Expand Down Expand Up @@ -183,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 All @@ -202,6 +215,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 {
Expand All @@ -218,6 +234,27 @@ 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());
}

private function setGroupQuota(string $groupId, string $quota): void {
$quota = \OCP\Util::computerFileSize($quota);
$this->appConfig->setValueString('groupquota', 'quota_' . $groupId, (string)$quota);
Expand Down
62 changes: 62 additions & 0 deletions lib/Listener/BeforeUserAddedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AdminGroupManager\Listener;

use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSException;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Group\Events\BeforeUserAddedEvent;
use OCP\Group\ISubAdmin;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;

/**
* @template-implements IEventListener<Event|BeforeUserAddedEvent>
*/
class BeforeUserAddedListener implements IEventListener {
public function __construct(
private IUserSession $userSession,
private IGroupManager $groupManager,
private ISubAdmin $subAdmin,
) {
}

#[\Override]
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) === [];
}
}
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