Problem/Motivation

Role is deleted when module is uninstalled

Steps to reproduce

  1. Create a role like Content Editor
  2. Enable modules: Token Content Access and Token Content Access nodes
  3. Give Content Editor permissions: Administer Token Content Access settings for Content
  4. Disable modules: Token Content Access and Token Content Access nodes

Content Editor role is deleted

Comments

sassafrass created an issue. See original summary.

lobodakyrylo’s picture

Status: Active » Closed (works as designed)

I suppose this behaviour is based on how Drupal dependencies work. When for the role you enable any permission, defined in any module, then the module dependency will be added to this role. It means that if you uninstall the module, it will delete the role. To avoid it, you have to disable all the permissions.

You can test it on other modules such as taxonomy or node and you will see the same behaviour. I close this thicket because everything works as designed. If you need solution for this issue, you have to create ticket for the core module.

dieterholvoet’s picture

I encountered the same problem today. Turns out, if you uninstall the module, by default it will not delete the entire role - that would be bad behaviour. The only time the entire role will be deleted is if the permission being removed isn't defined in a permissions.yml file. This can happen if you uninstall the module while the module files are already gone. To prevent this from happening, first uninstall the module, and then remove the module files.

dieterholvoet’s picture

Status: Closed (works as designed) » Active

I'm not an user of this module so I can't do this, but you should probably check TcaPermissionGenerator to make sure the permissions which are granted in the role are still present if the module is being uninstalled.

lobodakyrylo’s picture

@DieterHolvoet it works for every core and contribution module that uses own permissions. When you pick the permission for specific role, it's added to user.role.role_name configuration with dependencies to the module. Then when you delete the module, it requires to delete all the configurations that have dependencies to this module, so the user.role.role_name will be deleted. This is how dependencies work.

If you know how to avoid it, please, tell me but I suppose it works as designed since modules such as node and taxonomy have the same issue.

dieterholvoet’s picture

Then when you delete the module, it requires to delete all the configurations that have dependencies to this module, so the user.role.role_name will be deleted. This is how dependencies work.

That's not true, core has a way of dealing with this. The code responsible for removing dependencies from roles instead of deleting the whole role can be found in Drupal\user\Entity\Role::onDependencyRemoval. See #2571235: [regression] Roles should depend on objects that are building the granted permissions.

  • lobodacyril committed 5cebff7 on 3.0.x
    Issue #3270190: Role is deleted when module is uninstalled
    
lobodakyrylo’s picture

Status: Active » Fixed

Dietergolvoet yowre rigt. Now I got iur point. I misunderstood you for the first time and checked everything not accurate. I fixed the issue and now it should work (added additional dependencies). Thank you for your help.

lobodakyrylo’s picture

Version: 2.0.0 » 3.0.x-dev

Issue were fixed for 3.0.x version.

Status: Fixed » Closed (fixed)

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