From: William Mortada Date: Sat, 2 Dec 2023 19:01:32 +0000 (+0000) Subject: Standalone - remove role from users on deletion X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=261adafc1a160b1c5d58307c6ed2c1b59cdf2a75;p=civicrm-core.git Standalone - remove role from users on deletion --- diff --git a/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php b/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php index 45a85eafe8..de1bf5ba18 100644 --- a/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php +++ b/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php @@ -10,6 +10,21 @@ class CRM_Standaloneusers_BAO_Role extends CRM_Standaloneusers_DAO_Role implemen * @param \Civi\Core\Event\PostEvent $event */ public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { + // Remove role from users on deletion + if ($event->action === 'delete') { + $users = \Civi\Api4\User::get(FALSE) + ->addSelect('id', 'roles') + ->addWhere('roles', 'CONTAINS', $event->id) + ->execute(); + foreach ($users as $user) { + $roles = array_diff($user['roles'], [$event->id]); + \Civi\Api4\User::update(FALSE) + ->addValue('roles', $roles) + ->addWhere('id', '=', $user['id']) + ->execute(); + } + } + // Reset cache Civi::cache('metadata')->clear(); }