Skip to content
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Types of changes:
- *Security* in case of vulnerabilities.

<!-- changelog-linker -->
## Unreleased
### Security
* Enabling the users of a group, on `users-of-group/set-enabled` or when the group is provisioned again, now only enables the account that owns the group and the users that `users-of-group/set-enabled` disabled. Users disabled by the group admin stay disabled, and a user enabled again by hand loses the billing mark. Members disabled by billing before this version are not enabled again automatically; the group admin enables them in the users settings.
* The account created for the admin of a group is linked to the WordPress login of the same name, as `wordpress_login_backend` requires since its link check. Deploy this version before that one, then run `occ wordpress-login-backend:link-users --dry-run`, check the output and run it again without `--dry-run` to link the accounts created before.

## 0.1.1 - 2026-03-12
### Changed
* Declare compatibility with Nextcloud 33.
Expand Down
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
namespace OCA\AdminGroupManager\AppInfo;

use OCA\AdminGroupManager\Listener\BeforeUserAddedListener;
use OCA\AdminGroupManager\Listener\UserEnabledListener;
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;
use OCP\User\Events\UserChangedEvent;

/**
* @codeCoverageIgnore
Expand All @@ -34,5 +36,6 @@ public function boot(IBootContext $context): void {
public function register(IRegistrationContext $context): void {
$context->registerMiddleWare(InjectionMiddleware::class);
$context->registerEventListener(BeforeUserAddedEvent::class, BeforeUserAddedListener::class);
$context->registerEventListener(UserChangedEvent::class, UserEnabledListener::class);
}
}
48 changes: 35 additions & 13 deletions lib/Controller/AdminGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\AdminGroupManager\Controller;

use OCA\AdminGroupManager\AppInfo\Application;
use OCA\AdminGroupManager\BackgroundJob\EnableAppsForGroup;
use OCA\AdminGroupManager\Controller\Attribute\RestrictIp;
use OCA\Settings\Settings\Admin\Users;
Expand All @@ -22,6 +23,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\ISubAdmin;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IRequest;
Expand All @@ -33,6 +35,8 @@
use Psr\Log\LoggerInterface;

class AdminGroupController extends AEnvironmentAwareOCSController {
public const DISABLED_BY_BILLING = 'disabled_by_billing';

public function __construct(
$appName,
IRequest $request,
Expand All @@ -46,6 +50,7 @@ public function __construct(
protected IEventDispatcher $eventDispatcher,
protected ISecureRandom $secureRandom,
protected IJobList $jobList,
protected IConfig $config,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -102,7 +107,7 @@ public function createAdminGroup(
/**
* Set enabled status
*
* Change the status of all users of a group to be enabled or not.
* Disable the enabled users of a group, or enable again the account that owns the group and the users that this endpoint disabled. Administrators are never changed.
*
* @param string $groupid ID of the group
* @param int<0, 1> $enabled 1 or 0
Expand All @@ -124,12 +129,16 @@ public function setEnabled(
if (!$group instanceof IGroup) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$users = $group->getUsers();
foreach ($users as $user) {
if ($this->isAdministrator($user)) {
if ($enabled) {
$this->enableUsersDisabledByBilling($group);
return new DataResponse();
}
foreach ($group->getUsers() as $user) {
if ($this->isAdministrator($user) || !$user->isEnabled()) {
continue;
}
$user->setEnabled((bool)$enabled);
$this->config->setUserValue($user->getUID(), Application::APP_ID, self::DISABLED_BY_BILLING, '1');
$user->setEnabled(false);
}
return new DataResponse();
}
Expand Down Expand Up @@ -188,6 +197,7 @@ private function createUser($userId, $displayName, $email): IUser {
$password = $passwordEvent->getPassword() ?? $this->secureRandom->generate(20);

$user = $this->userManager->createUser($userId, $password);
$this->config->setUserValue($user->getUID(), 'wordpress_login_backend', 'wordpress_login', $user->getUID());
}

if ($displayName !== '') {
Expand All @@ -213,13 +223,7 @@ private function addGroup(string $groupid, string $displayname = ''): IGroup {
// Check if it exists
$group = $this->groupManager->get($groupid);
if ($group instanceof IGroup) {
$users = $group->getUsers();
foreach ($users as $user) {
if ($this->isAdministrator($user)) {
continue;
}
$user->setEnabled(true);
}
$this->enableUsersDisabledByBilling($group);
} else {
$group = $this->groupManager->createGroup($groupid);
}
Expand Down Expand Up @@ -247,7 +251,25 @@ private function canProvision(string $groupid): bool {
}
$existingGroup = $this->groupManager->get($groupid);
return $existingGroup instanceof IGroup
&& $this->subAdmin->isSubAdminOfGroup($existingUser, $existingGroup);
&& $this->ownsGroup($existingUser, $existingGroup);
}

private function enableUsersDisabledByBilling(IGroup $group): void {
foreach ($group->getUsers() as $user) {
if ($this->isAdministrator($user)) {
continue;
}
if (!$this->ownsGroup($user, $group)
&& $this->config->getUserValue($user->getUID(), Application::APP_ID, self::DISABLED_BY_BILLING) !== '1') {
continue;
}
$user->setEnabled(true);
}
}

private function ownsGroup(IUser $user, IGroup $group): bool {
return $user->getUID() === $group->getGID()
&& $this->subAdmin->isSubAdminOfGroup($user, $group);
}

private function isAdministrator(IUser $user): bool {
Expand Down
37 changes: 37 additions & 0 deletions lib/Listener/UserEnabledListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 OCA\AdminGroupManager\AppInfo\Application;
use OCA\AdminGroupManager\Controller\AdminGroupController;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\User\Events\UserChangedEvent;

/**
* @template-implements IEventListener<Event|UserChangedEvent>
*/
class UserEnabledListener implements IEventListener {
public function __construct(
private IConfig $config,
) {
}

#[\Override]
public function handle(Event $event): void {
if (!$event instanceof UserChangedEvent) {
return;
}
if ($event->getFeature() !== 'enabled' || $event->getValue() !== true) {
return;
}
$this->config->deleteUserValue($event->getUser()->getUID(), Application::APP_ID, AdminGroupController::DISABLED_BY_BILLING);
}
}
2 changes: 1 addition & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
"post": {
"operationId": "admin_group-set-enabled",
"summary": "Set enabled status",
"description": "Change the status of all users of a group to be enabled or not.\nThis endpoint requires admin access",
"description": "Disable the enabled users of a group, or enable again the account that owns the group and the users that this endpoint disabled. Administrators are never changed.\nThis endpoint requires admin access",
"tags": [
"admin_group"
],
Expand Down
2 changes: 1 addition & 1 deletion src/types/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type paths = {
put?: never;
/**
* Set enabled status
* @description Change the status of all users of a group to be enabled or not.
* @description Disable the enabled users of a group, or enable again the account that owns the group and the users that this endpoint disabled. Administrators are never changed.
* This endpoint requires admin access
*/
post: operations["admin_group-set-enabled"];
Expand Down
3 changes: 0 additions & 3 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="lib/Controller/AdminGroupController.php">
<FalsableReturnStatement>
<code><![CDATA[$user]]></code>
</FalsableReturnStatement>
<InvalidArgument>
<code><![CDATA[Users::class]]></code>
<code><![CDATA[Users::class]]></code>
Expand Down
Loading