Skip to content

Commit 49606db

Browse files
author
Pasquale Tripodi
authored
Merge pull request #626 from owncloud/properly_cleanup_users
fix: implement hooks to properly cleanup users upon complete removal
2 parents a7d31c8 + 2caac73 commit 49606db

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

appinfo/app.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
*
2020
*/
2121

22+
use OCA\CustomGroups\Hooks;
23+
2224
$app = new \OCA\CustomGroups\Application();
2325
$app->registerGroupBackend();
2426
$app->registerNotifier();
27+
Hooks::register();
2528

2629
if (!\defined('PHPUNIT') && !\OC::$CLI) {
2730
$pathInfo = \OC::$server->getRequest()->getPathInfo();

lib/Hooks.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @author Pasquale Tripodi <pasquale.tripodi@kiteworks.com>
4+
* @author Ilja Neumann <ilja.neumann@kiteworks.com>
5+
*
6+
* @copyright Copyright (c) 2024, ownCloud GmbH
7+
* @license AGPL-3.0
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
23+
namespace OCA\CustomGroups;
24+
25+
class Hooks {
26+
public static function register(): void {
27+
\OCP\Util::connectHook(
28+
'OC_User',
29+
'post_deleteUser',
30+
self::class,
31+
'userDelete'
32+
);
33+
}
34+
35+
public static function userDelete($params) {
36+
$customGroupsDbHandler = \OC::$server->query(CustomGroupsDatabaseHandler::class);
37+
foreach ($customGroupsDbHandler->getUserMemberships($params['uid'], null) as $customGroup) {
38+
$members = $customGroupsDbHandler->getGroupMembers($customGroup['group_id']);
39+
if (\count($members) === 1 && $members[0]['user_id'] === $params['uid']) {
40+
// removing custom group as deleted user is the only member/admin left
41+
$customGroupsDbHandler->deleteGroup($customGroup['group_id']);
42+
}
43+
$customGroupsDbHandler->removeFromGroup($params['uid'], $customGroup['group_id']);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)