Problem/Motivation

It was discovered when writing the MongoDB user storage that User::preCreate() always adds the authenticated user role, to the list of roles for a user. Always? you ask, you mean, except for the anonymous user, right? No, I mean always.

$anonymous = User::create(['uid' => 0]);
$roles = $anonymous->getRoles();
$roles === ['authenticated'] // TRUE;

Proposed resolution

This could be fixed by checking the user ID in User::preCreate(). Instead I propose removing that code and adding the appropriate role (DRUPAL_ANONYMOUS_RID and DRUPAL_AUTHENTICATED_RID) in User::getRoles().

Remaining tasks

User interface changes

API changes

CommentFileSizeAuthor
#3 2374081-3-user-pre-create.patch3.47 KBtstoeckler

Comments

chx’s picture

Issue summary: View changes

This is why you need alternate storage engines in core.

tstoeckler’s picture

The reason this currently works is because core's UserStorage decides to never save the authenticated and anonymous users, so this never gets persisted.

tstoeckler’s picture

Status: Active » Needs review
StatusFileSize
new3.47 KB

Let's see if this works.

There are a bunch of changes which seem out of scope speaking strictly in terms of the bug in the issue summary, but in terms of making UserStorage independent of this I would consider them in scope. Now UserStorage knows nothing of DRUPAL_ANONYMOUS_RID or DRUPAL_AUTHENTICATED_RID. Storages now just need to remember to call getRoles(TRUE).

On an also related note: Is it just me or is a parameter name called $exclude_locked_roles = FALSE backwards? Wouldn't $include_locked_roles = TRUE be more self-including, which would make the caller do getRoles(FALSE). Thoughts?

Status: Needs review » Needs work

The last submitted patch, 3: 2374081-3-user-pre-create.patch, failed testing.

berdir’s picture

Would be great to focus on #2248977: Complete support for multi-value base fields in ContentEntitySchemaHandler and use it for the user.roles field if that is still doable, because that is fixing up a lot related to users/roles.

berdir’s picture

Status: Needs work » Closed (duplicate)

Ok, that issue is RTBC, as soon as it is committed, we can close this as a duplicate I think.