In default Drupal, we show a checkbox for removing the authenticated user role but we don't let you uncheck the box because then account would not have any roles. thats a useless checkbox! this patch fixes this.

In addition, this patch adjusts the login and access system so that logged in users *always* get the authenticated user role. This is analogous to how we handle anonymous users (they always receive the anon user role). Only harm can happen by not making this assumption. There are situations on table sharing sites where users are created but they are not in any role. This patch makes that problem disappear.

With this patch, the authenticated or anonymous user role is attached to $user->roles during sess_read() or user_load(). This completeness in $user->roles enabled me to simplify the query in node_access() a bit by removing JOIN. Further, some special case code is removed in various places.

I think this adds a bit of UI improvement, and more robustness.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chx’s picture

With INSERT INTO role (rid, name) VALUES (2, 'authenticated user'); in database definition, a simple define (DRUPAL_AUTHENTICATED_RID, 2); would suffice instead of the db function. Otherwise, very cool patch.

moshe weitzman’s picture

that define could cause troubles for existing sites with strange setups. since it has minimal benefit, i propose to go with the patch as is. anyone else care to review this? if you do, please explicitly say whether you tested it or not.

chx’s picture

moshe, my problem is that you add a query to every page served to non-anon users. IIf someone has a real weirdo setup, then (s)he can correct the define.

killes@www.drop.org’s picture

I think I don't like this patch. First, it adds overhead, second, here on drupal.org I on occassion ban users by taking "authenticated" user away and granting the "restricted users" role. We could of course change "authenticated users" to have only a minimal set of permissions but then we'd need to grant additional rights to every new user.

One can of course question the wisdom of having a "restricted users" role and not blocking the users directly.

I think we should instead make sure that at least one checkbox of that checkbox set is checked.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the reviews.

I changed to a define() per chx' suggestion. You convinced me.

@killes: i see your point. however, on balance this is a step forward for drupal. the state you describe could only make sense to a developer i think. you have a restricted user role but very few sites will have that. this starts to sound like a "who's on first" comedy routine. imagine a username=joe who is in this state:

q: is joe logged authenticated
a: yes

q: is joe a user?
a: yes

q: so joe is an authenticated user?
a: no

q: so joe is anonymous?
a: no, he's joe

q: what poermissions does joe have?
a: anonymous

moshe weitzman’s picture

Assigned: Unassigned » moshe weitzman
FileSize
9.84 KB

now with a patch

Gerhard Killesreiter’s picture

Moshe: the problem is not that I have this setup, but drupal.org has it. :P

I dont know what propted the introduction of the "restricted user" role. if we find out that we don't need it, we can go with this patch.

Dries’s picture

The 'restricted user' role was introduced to block registered users from posting. We could have blocked these users as well. Still, punishing certain users without having to block them might be a sensible use case?

If we have 'DRUPAL_AUTHENTICATED_RID' we should have 'DRUPAL_ANONYMOUS_RID' as well and use it across the code. Example:

  if ($rid != 0 && $rid != DRUPAL_AUTHENTICATED_RID) {

Also, please depricate:

function _user_authenticated_id() {
  return db_result(db_query("SELECT rid FROM {role} WHERE name = 'authenticated user'"));
}
Dries’s picture

Some more observations:

In my role table is an anonymous user too. I guess that should be cleaned up as well?

mysql> select * from role;
+-----+--------------------+
| rid | name               |
+-----+--------------------+
|   1 | anonymous user     |
|   2 | authenticated user |
|   3 | administrator      |
+-----+--------------------+

In the users_roles table are registered users as well:

mysql> select * from users_roles where uid = 1;
+-----+-----+
| uid | rid |
+-----+-----+
|   1 |   2 |
|   1 |   3 |
+-----+-----+
Stefan Nagtegaal’s picture

And, maybe it's worthwhile at the same time, to fix the role names 'Authenticated user' and 'Anonymous user' which are untranslatable.. :-)

moshe weitzman’s picture

FileSize
10.82 KB

I implemented all feedback except for Stefan's request to translate these 2 system roles. Thats for another patch.

moshe weitzman’s picture

FileSize
12.67 KB

more cleanup. we are no longer dependant on role names, only rids.

@stefan - once this is committed, you may find all the places where we output a role name and wrap in a t(). i see roles, permissions, input filters, user overview, user edit, ... or you could make sure all functions use user_roles() instead of custom query. then do th t() only in user_roles().

-moshe

moshe weitzman’s picture

more people are running into this problem - http://drupal.org/node/26700.

would be nice if a CVS reviewer commented here :). i am OK with postponing until 4.8 if thats desired. a bit disappointed, but OK.

Dries’s picture

Haven't test it. Code looks good.

Does $key need to be escaped?

-            $row[] = array('data' => form_render($form['checkboxes'][$rid][$key]), 'align' => 'center');
+            $row[] = array('data' => form_render($form['checkboxes'][$rid][$key]), 'align' => 'center', 'title' => $key);
moshe weitzman’s picture

yes it does. good catch. i will upload again tonight. unless someone says differently, i will change form API to do this automatically for all title elements. those should always be plain text. the function that needs the check_plain() in this case is http://drupaldocs.org/api/head/function/theme_checkbox

chx’s picture

Status: Reviewed & tested by the community » Needs work

Just make sure that we do not double escape some titles. I am OK with the change. But, as you are promising a new patch, I changed the status.

webchick’s picture

Question.. will this patch prevent the logintoboggan module from working? That module allows users to instantly sign up and pick their own passwords without having to futz around in their e-mail, thus increasing usability.

If you use this option, users are added to a "pre-authorized" group of your choosing to give them a set of minimal permissions (better than anon but less than 'authenticated user') until such time that they've validated their e-mail address and then get bumped up to 'fully' authenticated user. But if authenticated user applies for ALL logged in users, it seems we would lose this option for functionality.

Crell’s picture

@webchick: I think the better solution would be to do it the other way around. Make authenticated user a minimalist set of permissions, and something like "validated user" the "real" role. Then use hook_user() to automatically put a user into "validated user" on email validation.

Similar solution for Drupal.org and the "restricted but not banned" issue that killes mentioned. It might even be good to generalize "put into role on user creation" functionality into a module, if someone hasn't done it already.

This gets a conceptual +1 from me, although I've not looked at the code yet.

moshe weitzman’s picture

Status: Needs work » Reviewed & tested by the community

Upon further review, that escaping is not needed. The content are permission names, and those are module defined, not user defined. And we inherently trust our active modules, because they can execute arbitrary php, delete data, etc.

ready to commit

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Thanks.

Cvbge’s picture

Status: Fixed » Reviewed & tested by the community
FileSize
748 bytes

Original patch did not remove INSERTs into user_roles in database.pgsql

Cvbge’s picture

FileSize
796 bytes

Rerolled from the top.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Thanks.

markus_petrux’s picture

Sorry, shouldn't these statements be removed as well?

 INSERT INTO role (name) VALUES ('anonymous user');
 INSERT INTO role (name) VALUES ('authenticated user');
moshe weitzman’s picture

those two INSERTS are still necessary ... a future patch work on creating get/set functions for roles and assuring that all functions use them. then eliminating those records is easy.

@Cvbge - good catch on the postgres schema. sorry i missed that.

dww’s picture

i discovered this thread because a small patch i submitted for user.module (see http://drupal.org/node/47735 for details) applies cleanly in 4.6 but fails to patch against the HEAD . one "side effect" of this whole discussion about 'authenticated user' is that the db_query() inside user_access() was "simplif[ied]... a bit by removing JOIN". well, that broke my nice clean patch from working in HEAD. ;) cvs annotate on the modified db_query() line in HEAD lead me here... (side note: drupal and drupal.org is great... i love the fact that the cvs log messages in your repository include node ids about the thread that lead to the patch -- if only we were so well organized and coordinated where i work).

i can see that removing an unnecessary JOIN in the default case is a good thing, but i'm wondering if any of you user.module experts would comment on my other thread.

thanks much!
-derek

Anonymous’s picture

Status: Fixed » Closed (fixed)
marcoBauli’s picture

Version: x.y.z » 5.12

umm..still experiencing this on 5.12, new users don't have the 'authenticated user' role attached by default.

Am i the only?

aamin’s picture

I am also facing the same problem, can someone please tell me how can I use this patch. that is where should I copy it.

Thanks.

infohari’s picture

hi all,
i am totally new to the drupal environment, how to set user permission on the user registration. can any one address me please
with regards,
hari