Skip to content

Commit

Permalink
Merge pull request #626 from owncloud/properly_cleanup_users
Browse files Browse the repository at this point in the history
fix: implement hooks to properly cleanup users upon complete removal
  • Loading branch information
pako81 authored Apr 11, 2024
2 parents a7d31c8 + 2caac73 commit 49606db
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
*
*/

use OCA\CustomGroups\Hooks;

$app = new \OCA\CustomGroups\Application();
$app->registerGroupBackend();
$app->registerNotifier();
Hooks::register();

if (!\defined('PHPUNIT') && !\OC::$CLI) {
$pathInfo = \OC::$server->getRequest()->getPathInfo();
Expand Down
46 changes: 46 additions & 0 deletions lib/Hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @author Pasquale Tripodi <pasquale.tripodi@kiteworks.com>
* @author Ilja Neumann <ilja.neumann@kiteworks.com>
*
* @copyright Copyright (c) 2024, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\CustomGroups;

class Hooks {
public static function register(): void {
\OCP\Util::connectHook(
'OC_User',
'post_deleteUser',
self::class,
'userDelete'
);
}

public static function userDelete($params) {
$customGroupsDbHandler = \OC::$server->query(CustomGroupsDatabaseHandler::class);
foreach ($customGroupsDbHandler->getUserMemberships($params['uid'], null) as $customGroup) {
$members = $customGroupsDbHandler->getGroupMembers($customGroup['group_id']);
if (\count($members) === 1 && $members[0]['user_id'] === $params['uid']) {
// removing custom group as deleted user is the only member/admin left
$customGroupsDbHandler->deleteGroup($customGroup['group_id']);
}
$customGroupsDbHandler->removeFromGroup($params['uid'], $customGroup['group_id']);
}
}
}

0 comments on commit 49606db

Please sign in to comment.