Problem/Motivation

When a user has 'administer permissions' they have a number of interface they can use to assign roles to users. The module finds these user interfaces and makes them respect the new permissions the module introduces. This means a lot of extra code and the possibility in the future that new user interfaces and mechanisms for assigning roles are missed and thus breaks the expectations the module lays out.

Proposed resolution

Instead of locking down users with 'administer permissions', make this permission override all existing role_delegation permissions. If you have 'administer permissions', role_delegation is irrelevant.

When you are assigned a role_delegation permission and NOT the 'administer permissions' permission, then you get access to an extra user interface for assigning roles. This means there is only a SINGLE place that access checking is required.

Remaining tasks

Patch.

User interface changes

Removal of all code that interacts with core forms.

API changes

Updated expectations of what role_delegation permissions do.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

benjy created an issue. See original summary.

Sam152’s picture

Title: Clean up role_delegation_form_alter() » Change module architecture to grant extra access instead of removing existing access
Issue summary: View changes
benjy’s picture

Status: Active » Needs review
FileSize
13.94 KB

I agree, its not for this module to try and lock things down. I already found an issue that would have been a security vulnerability if it wasn't that administer users was a restricted permission.

Heres the user/%/edit roles widget removed and any kind of VBO filtering.

benjy’s picture

TODO

  1. Refactor the class that adds the form elements and move all that logic into the form with clean-ups.
  2. Add new tests to ensure the access control around the assignable roles works as expected
  3. Update the project page.
  4. Create an issue against core to fix the issue with access to VBO roles when you have "administer users"
benjy’s picture

benjy’s picture

Refactored the form and turned the helper into a real service. Not sure what to call the service, its simply called RoleDelegation at the moment, maybe RoleManager or RoleDelegationManager?

benjy’s picture

Removed more classes, consolidated the permission callback into the same service and then adding some more kernel tests which now have 100% coverage for for the RoleDelegation service and the AccessCheck.

I just want to do some more clean-up here, rename a couple of things and then its just the web tests that need fleshing out again.

benjy’s picture

OK, added some new web tests as well. I converted the role delete test into a kernel test because it didn't need WTB and I removed the role rename test since it wasn't testing any code of ours, it was only the role label been renamed anyway.

benjy’s picture

Removed un-needed setUp(), ready for a review here.

benjy’s picture

Just removed the README which was empty anyway. Also looks like rewriting the tests fixed the tests for PHP7 and SQLite which is a nice bonus.

Sam152’s picture

Looking good. RTBC after comments read.

  1. +++ b/src/Form/RoleDelegationSettingsForm.php
    @@ -17,17 +19,57 @@ use Drupal\user\UserInterface;
    +   * @param \Drupal\Core\Session\AccountInterface $current_user
    +   *   The current user viewing the form.
    

    Incomplete docs.

  2. +++ b/src/Form/RoleDelegationSettingsForm.php
    @@ -17,17 +19,57 @@ use Drupal\user\UserInterface;
    +      '#title' => isset($form['account']['roles']['#title']) ? $form['account']['roles']['#title'] : t('Roles'),
    

    Left over? Just set the title.

  3. +++ b/src/Form/RoleDelegationSettingsForm.php
    @@ -17,17 +19,57 @@ use Drupal\user\UserInterface;
    +      '#description' => isset($form['account']['roles']['#description']) ? $form['account']['roles']['#description'] : t('Change roles assigned to user.'),
    

    This too?

  4. +++ b/src/Form/RoleDelegationSettingsForm.php
    @@ -42,22 +84,13 @@ class RoleDelegationSettingsForm extends FormBase {
    +      $value === 0 ? $account->removeRole($rid) : $account->addRole($rid);
    

    Could value ever not be an int and cause issues here?

  5. +++ b/src/Form/RoleDelegationSettingsForm.php
    @@ -42,22 +84,13 @@ class RoleDelegationSettingsForm extends FormBase {
    +      $account->save();
    

    Why not save the account outside the loop?

  6. +++ b/src/RoleDelegation.php
    @@ -0,0 +1,53 @@
    +      if ($account->hasPermission(sprintf('assign %s role', $role->id())) || $account->hasPermission('assign all roles')) {
    

    Could return early by checking this.

  7. +++ b/src/RoleDelegation.php
    @@ -0,0 +1,53 @@
    +    $all_roles = user_roles(TRUE);
    

    Can almost use Role::loadMultiple() here. It's not giving you that much.

benjy’s picture

Could value ever not be an int and cause issues here?

No, I think Form API handles that.

All the rest of the feedback and the RoleDelegation service renamed to DelegatableRoles as discussed.

benjy’s picture

Fixed the patch file.

The last submitted patch, 12: 2693405-12.patch, failed testing.

benjy’s picture

Separated out the permission generation. This is ready for committing now.

Sam152’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 15: 2693405-15.patch, failed testing.

benjy’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
36.94 KB
814 bytes

Missing groups on the kernel tests.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 18: 2693405-16.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
FileSize
36.93 KB
394 bytes

Fixed tests.

benjy’s picture

Kernel tests in right folder.

benjy’s picture

Status: Needs review » Reviewed & tested by the community

Back to rtbc, will commit tonight and update project page.

benjy’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

  • benjy committed a925b94 on 8.x-1.x
    Issue #2693405 by benjy, Sam152: Change module architecture to grant...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.