-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OPES-1549: Allow to delete users #206
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,21 @@ declare(strict_types=1); | |
/** | ||
* Implements hook_user_cancel_methods_alter(). | ||
* | ||
* Remove the option to delete users while keeping the option to block them. | ||
* Conditionally removes the option to delete users. | ||
*/ | ||
function oe_authentication_user_cancel_methods_alter(&$methods) { | ||
if (\Drupal::currentUser()->id() == 1) { | ||
$current_user = \Drupal::currentUser(); | ||
$enable_user_delete = \Drupal::configFactory() | ||
->get('oe_authentication.settings') | ||
->get('enable_user_delete_options'); | ||
|
||
if ( | ||
$current_user->id() == 1 || | ||
( | ||
$enable_user_delete === TRUE && | ||
$current_user->hasPermission('access user delete options') | ||
) | ||
) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (side note) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the idea of modifing/refactoring the current code/behaviour just the minimum to allow the deletion. |
||
$restricted_options = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
administer authentication configuration: | ||
title: 'Administer Authentication configuration' | ||
restrict access: false | ||
access user delete options: | ||
title: 'Use the user delete options in "User cancel form"' | ||
restrict access: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel uneasy about introducing a permission with such a generic name that could easily clash with permissions introduced by other modules or by future Drupal core versions. If this has been agreed with others, then ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ofc, if any other module or core is coming up with a similar option and/or permission, we do likely have a problem either way, independent of the name clash. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This said, I don't have a good suggestion on namespacing the permission. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name seems a bit generic indeed.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to: access user delete options |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,12 @@ public function buildForm(array $form, FormStateInterface $form_state) { | |
'#title' => $this->t('Force two factor authentication'), | ||
'#default_value' => $this->config(static::CONFIG_NAME)->get('force_2fa'), | ||
]; | ||
$form['enable_user_delete_options'] = [ | ||
'#type' => 'checkbox', | ||
'#title' => $this->t('Enable the user delete options'), | ||
'#description' => $this->t('With this option enabled, users with the "access user delete options" permission will be allowed to delete users.'), | ||
'#default_value' => $this->config(static::CONFIG_NAME)->get('enable_user_delete_options'), | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative would be to use radios for this, instead of single checkbox. The internal setting can remain a boolean. With radios we can properly describe the effect of each option, without saying "if this is checked" or "if this is unchecked". E.g.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I think about it more, wouldn't this setting belong into "Account settings" form, next to "When canceling a user account"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The module uses the checkbox for other boolean fields, like 2fa authentication. |
||
return parent::buildForm($form, $form_state); | ||
} | ||
|
||
|
@@ -72,6 +78,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { | |
->set('assurance_level', $form_state->getValue('assurance_level')) | ||
->set('ticket_types', $form_state->getValue('ticket_types')) | ||
->set('force_2fa', (bool) $form_state->getValue('force_2fa')) | ||
->set('enable_user_delete_options', (bool) $form_state->getValue('enable_user_delete_options')) | ||
->save(); | ||
parent::submitForm($form, $form_state); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,63 @@ | ||
@api | ||
@DrupalLogin | ||
Feature: Manage users | ||
Because users are managed externally | ||
I should not be able to delete users on the site | ||
Unless I am superuser user | ||
Or module configuration to access the user delete options is enabled and I have | ||
the permission 'access user delete options' | ||
|
||
@DrupalLogin | ||
Scenario: Users should not be able to delete other users | ||
Given I am logged in as a user with the "administer users" permissions | ||
When I visit my user page | ||
And I click "Edit" | ||
And I click "Cancel account" | ||
Then I should see "Disable the account and keep its content." | ||
And I should not see "Delete the account and its content." | ||
And I should see "Disable the account and unpublish its content." | ||
And I should not see "Delete the account" | ||
|
||
Scenario: As the superuser I can access the delete options | ||
Given users: | ||
| name | mail | roles | | ||
| foo | [email protected] | | | ||
And I am logged in as the superuser user | ||
When I visit the foo user page | ||
And I click "Edit" | ||
And I click "Cancel account" | ||
Then I should see "Disable the account and keep its content." | ||
And I should see "Disable the account and unpublish its content." | ||
And I should see "Delete the account and its content." | ||
And I should see "Delete the account and make its content belong to the Anonymous user." | ||
|
||
Scenario: Users should not be able to delete other users when the configuration | ||
is not enabled but the permission 'access user delete options' has been given. | ||
And I am logged in as a user with the "administer users,access user delete options" permissions | ||
When I visit my user page | ||
And I click "Edit" | ||
And I click "Cancel account" | ||
Then I should see "Disable the account and keep its content." | ||
And I should see "Disable the account and unpublish its content." | ||
And I should not see "Delete the account" | ||
|
||
Scenario: Users should not be able to delete other users when the configuration | ||
is enabled but the permission 'access user delete options' has not been given. | ||
Given the site is configured to display user delete options | ||
And I am logged in as a user with the "administer users" permissions | ||
When I visit my user page | ||
And I click "Edit" | ||
And I click "Cancel account" | ||
Then I should see "Disable the account and keep its content." | ||
And I should see "Disable the account and unpublish its content." | ||
And I should not see "Delete the account" | ||
|
||
Scenario: Users should be able to delete other users when the configuration is | ||
enabled and the permission 'access user delete options' has been given. | ||
Given the site is configured to display user delete options | ||
And I am logged in as a user with the "administer users,access user delete options" permissions | ||
When I visit my user page | ||
And I click "Edit" | ||
And I click "Cancel account" | ||
Then I should see "Disable the account and keep its content." | ||
And I should see "Disable the account and unpublish its content." | ||
And I should see "Delete the account and its content." | ||
And I should see "Delete the account and make its content belong to the Anonymous user." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't
->hasPermission()
already cover the user 1 case?I mean it returns true for user 1 for all perms, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, actually this includes admin users.
The way permissions are calculated is: is it admin or has the permission?
Which grants access to the users.
Not sure about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I want to keep functionality as is, I think is better to check the permission directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discard the direct comparisson, inverting the option seems a better idea.
We have two options to allow to see the options: