backdrop-contrib / entity_plus

This module wraps in a variety of additional entity-related functionality from various sources. Partial port of D7 Entity API.
https://backdropcms.org/project/entity_plus
GNU General Public License v2.0
3 stars 11 forks source link

Entity metadata wrapper sets the user roles incorrectly #134

Closed argiepiano closed 2 years ago

argiepiano commented 2 years ago

EMW sets the user roles incorrectly. The problem is the code under entity_plus_metadata_user_set_properties(), which follows the role pattern used in D7. In Backdrop, these are different. Setting the role editor for an authenticated user should result in a user's role array:

array(0 => 'authenticated', 1 => 'editor)

Instead the resulting user's role array looks like:

array('authenticated' => 'Authenticated', 'editor' => 'Editor')

which results on incorrect "Authenticated" and "Editor" roles being saved in the user_roles table for that user.

PR coming.

argiepiano commented 2 years ago

Further clarification, as my explanation above may not be completely clear.

The roles for each user are saved in the table users_roles with two columns: uid and role. The role column is the machine name of the role. The bug I fixed resulted in the human name being saved in the role column instead of the machine name.

To test you can do the following

  1. Load a user (e.g. $u = user_load(1);)
  2. Create a wrapper ($w = emw('user', $u);)
  3. Add a role ($w->roles[] = 'editor';)
  4. Save the entity ($w->save();)
  5. Check the users_roles table to make sure machine_names are present (not human names)

If you do the above BEFORE the patch, you'll see human names. After the patch, machine names

argiepiano commented 2 years ago

Furthermore, this bug really messed up the users_roles table, creating all sort of errors, for example, for Views that output roles