For a long time I've been wondering if this was possible, and eventually I decided to try writing a patch for it :)

The attached patch removes Drupal's hardcoded anonymous and authenticated user roles and replaces them with a simple system where any role on a site can be auto-assigned to users (a) before they log in, and (b) right after they have registered for an account.

The patch is basically functional and has the following advantages:

  1. The "anonymous user" and "authenticated user" roles that Drupal currently has are weird and confusing. Why? Because "anonymous" and "authenticated" represent states of interaction with a computer, but most other roles represent categories of people (think "Editor", "Administrator", etc). Things would be much clearer if the roles that users are automatically assigned work the same way as other roles on the site. For example, in this initial patch the default install profile starts off with anonymous users being assigned to a "Guest" role and newly-registered users being granted a "Member" role, but the expert profile doesn't start with any roles at all, and sites are free to use whatever roles are meaningful to them.
  2. The patch makes it possible to auto-assign the same role for both (a) and (b) above, if you want unauthenticated and authenticated users on your site to share the same base set of permissions. (No more clicking two checkboxes in a row on the permissions page!)
  3. Some sites have no need for "authenticated users" at all (for example, single user blogs). Currently, Drupal forces that role on them, but this patch means it won't anymore.
  4. If your site registration policies get more strict (for example, if you previously allowed open registration but then later switched to requiring email verification), this patch allows you to easily separate your users into "before" and "after" by switching the role that is auto-assigned to newly registered users. For example, you could create a new role called "Verified Member" and auto-assign that; you can then safely grant this new role more permissions knowing that anyone who gets it must have gone through more verification steps during registration than users who got the older "Member" role.
  5. Any patch which involves deleting as much ugly, special-case code as this one does can't be a bad thing :)

Screenshots are attached as well. I think this patch actually makes sense, but it's a big change so someone please tell me where I went wrong with it :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

David_Rothstein’s picture

Note that the following are possible disadvantages or things that still need work:

  1. The patch is still missing an upgrade path from D6, as well as numerous changes to the user interface text that would be required if this goes in.
  2. With this patch, a site can potentially find itself in a situation where there is no single role that all logged-in users have in common. This would complicate efforts to give all logged-in users a common experience. It's easy to fix, however, by adding a new role to the site and bulk updating all users to have it. It's not clear if that functionality needs to be in core (I'm guessing there's already a contrib module that provides it), but since the upgrade path from D6 will need this kind of bulk update anyway, maybe it should be there.
  3. We can no longer say "Login or register to post comments" in comment.module, since we have no way to know whether a given user will actually be able to do this when they log in; instead, we can basically just say "register to post comments". Given that this text occurs in a themable function, I don't think it's that big of a deal, though. (Worst case, we could probably make the text into a theme setting or something, so sites could set it individually.)
  4. Because the default role assignments are made in the installation profile and not hardcoded in user.module, clicking on the "Reset to defaults" button on the user settings page has the unfortunate effect of not actually going back to the "real" defaults. This is really a more general problem with installation profiles, but it's particularly noticeable here because changing those settings can have a drastic effect on the site. I'm not sure what the best solution is -- possibly it's the case that these settings are advanced enough that they don't belong on the main user settings page anyway. If they got their own page, then that page simply wouldn't have a "Reset to defaults" button and then there wouldn't be a problem.
David_Rothstein’s picture

Status: Needs work » Needs review

This certainly still needs work as per the comment above, but I'm very very curious to see what the testbot will say about it...

webchick’s picture

Wow. Interesting patch. subscribing for now.

sun’s picture

Status: Needs review » Needs work

drupal_anonymous_role() and drupal_registration_role() always need to return values, because there is a certain amount of functionality that depends on it. (mostly in contrib modules)

David_Rothstein’s picture

@sun: I'm not exactly sure what you mean. Can you give an example?

BTW, I ran the tests locally and it's not as bad as it could be. Got something like 11,000 passes and 90 failures (all the failures were in contact.module, and probably easy to fix). Then it hit the user.module tests and SimpleTest exploded and crashed :) Probably to be expected. So mostly it seems like the tests that this breaks are well-concentrated where they should be, which is a good thing.

Damien Tournoud’s picture

Interesting patch. Removing the hard-coded "authenticated user" role makes a lot of sense. On the other hand, removing the hard-coded "anonymous user" role only makes sense if we could dynamically add a role to an anonymous user, which is currently impossible. This would be truly awesome, as it would mean that we can do some kind of segmentation between anonymous users.

ksenzee’s picture

Love this idea. Anywhere you really do need a check for "is this user logged in or not?" you can just use $user->uid == 0, so I don't think David's first point in #1 is actually that much of an issue.

catch’s picture

I really like this. For a while I've been thinking about use cases like giving every user a role on registration, but taking that role away again if they abuse the site (but don't necessarily do enough to warrant blocking). This would allow that out of the box without any nasty hacks.

yoroy’s picture

Subscribe. And kudos for a well written issue. It helps a lot.

joshmiller’s picture

Sounds like we have two states any role can be:

  1. Authenticated
  2. Anonymous

According to Dries and Catch in the discussion going on over at d7ux.org: Admin Header, we may need to add one more state: Administrator.

I believe this is what #182023: Add a third default role to core for handling Administrative duties is all about. I will be cross-posting there.

zirvap’s picture

What does a site look like to a user without any role? Can she view any content? Or, well, view or do anything at all?

I can see why it could be useful to give the same role to both logged in and not logged in users, but I don't see how it's a good thing to make it possible to have users without any role at all.

Dries’s picture

Issue tags: +Favorite-of-Dries

This patch is very interesting indeed. Tracking it.

catch’s picture

@zirvap - if you remove all permissions from anonymous and authenticated users, then they can't access anything anyway, so there's really no change there.

sun’s picture

Perhaps offtopic, but related:

The {users_roles}.tid column exists in Drupal's schema and was supposed to allow tagging of user roles (which was never implemented).

EvanDonovan’s picture

This is a great idea. Subscribing.

joshmiller’s picture

Another related patch that has been committed: #480660: Add an 'administrator' role to core

michaelfavia’s picture

I attempted to remedy this same problem with a much less powerful but also much simpler solution. i made the names of the roles editable and added descriptions to the roles #393406: "Edit" links on roles admin page are confusing to users: "Edit permissions" should be the primary link. While i put all of my weight behind this patch instead, i would like to offer it as a partial solution if your isnt generally acceptable by everyone. It solves the first part of your issue but fails to offer the same benefits of the rest which I personally find valuable.

I do have a small concern with hiding all of these options under "site settings > user" though. I would never think to look there and i know my users wouldnt. This is why i advocated moving user settings under a menu local task for admin/user/user #391412: Move contact form, post, and user settings below Site configuration (original title was: Move "user settings" under user as a local menu task) and role settings under roles (admin/user/roles). This is inline with putting the configuration of items in place with the items they modify. We are exacerbating the "i have to go all the way across my site to change a setting for roles when I'm already in roles section".

i realize that nobody likes the menu local tasks because of issues with visibility, etc but I think we can mitigate/fix those with decent design, the new admin bar (with hierarchy), and add child links below admin menu items in some similar fashion to: http://drupal.org/files/issues/screenshot1_0_5.png

dropcube’s picture

Interesting, subscribing.

Pasqualle’s picture

subscribe

ronald_istos’s picture

Think this is well worth it - subscribing

sun’s picture

Version: 7.x-dev » 8.x-dev
mparker17’s picture

Subscribe

SeanBannister’s picture

sub

xjm’s picture

Very interesting; tracking.

Help fix "+1 subscribe" posts: see http://drupal.org/node/34496 and http://3281d.com/2009/03/27/death-to-subscribe-comments

ergonlogic’s picture

More than two years without activity... Is this still relevant, or blocked by some other issue?

Anonymous’s picture

I've also never understood the annonymouse and authenticated(HARDCODED) roles when all you have to do to determine who is registered and who is not is just $GLOBALS['user']->uid == 0 condition. I think that this should be totally removed from the core since it makes absolutely no sense at all.

yoroy’s picture

Still relevant. I don't think blocked on anything else, it needs someone to pick it back up and reroll the initial patch from git.

klaasvw’s picture

Assigned: Unassigned » klaasvw
FileSize
29.36 KB

Here's a reroll of the original patch together with some additional changes for code that was introduced since the previous patch. The main difference is that the standard install profile will create anonymous and registered roles while the minimal one comes with no roles.

I still need to work on this because it doesn't remove the roles from the tests. They are being used in several tests for checking functionality for anonymous users that involves permissions assigned to the anonymous role (e.g. posting comments).

As was originally stated by David, this is a tough one to implement and has a possible issue. Right now the registered role gets assigned automatically to new users. How do we handle existing users or switching the role? I was thinking of two ways:

- Always assign the role on user load instead of assigning it in the database. This might be an issue if we want tighter control over users and want to be able to remove the role from a single user.
- Provide an option for a batch job to run when the role is changed, assigning the role to all users. This introduces additional problems like removing the previous registered role and it's not great for sites with many user accounts.

Personally I'm in favor of the first option. It basically functions like the authenticated user role used to work and it doesn't introduce any messy things like having to run batch jobs. If having tighter control over the roles is an issue there's always the possibility to override or remove the role on hook_user_load for modules.

Any thoughts or suggestions on how to tackle this?

Also, any ideas on making tests relying on the roles compatible with this? Should the test profile always create these roles?

tim.plunkett’s picture

Status: Needs work » Needs review

Sending to the testbot.

Status: Needs review » Needs work
Issue tags: -Favorite-of-Dries

The last submitted patch, remove-hardcoded-roles-468768-28.patch, failed testing.

klaasvw’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Issue tags: +Favorite-of-Dries

The last submitted patch, remove-hardcoded-roles-468768-28.patch, failed testing.

amc’s picture

Possibly related (and may be able to be incorporated into this patch): #749298: Anonymous user name should be used on permissions screen

mgifford’s picture

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.