Skip to content

Commit

Permalink
UHF-9832: Added a new permission 'delete user accounts' and let only …
Browse files Browse the repository at this point in the history
…user with that permission to delete users.
  • Loading branch information
khalima committed May 6, 2024
1 parent d469f13 commit 8c21733
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions helfi_platform_config.module
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,28 @@ function helfi_platform_config_config_ignore_settings_alter(array &$settings) {
array_push($settings, $config);
}
}

/**
* Implements hook_user_cancel_methods_alter().
*/
function helfi_platform_config_user_cancel_methods_alter(array &$methods): void {
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = \Drupal::currentUser();

// Only allow user to disable user accounts if the user doesn't have
// a permission to delete user accounts.
$white_listed_methods = [
'user_cancel_block',
'user_cancel_block_unpublish',
];

// Deny access to all non-whitelisted methods if user doesn't have
// the 'delete user accounts' permission.
if (!$account->hasPermission('delete user accounts')) {
foreach ($methods as $name => &$method) {
if (!in_array($name, $white_listed_methods)) {
$method['access'] = FALSE;
}
}
}
}
2 changes: 2 additions & 0 deletions helfi_platform_config.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
delete user accounts:
title: Delete user accounts

0 comments on commit 8c21733

Please sign in to comment.