Role Assignment Not Working

Created on 18 March 2024, 8 months ago
Updated 2 April 2024, 8 months ago

Problem/Motivation

Role Assignment Not Working

Steps to reproduce

When a content is updated, add a role to the author. I removed all conditions, just to test the role assignment function. At the end, always the current user's role is added, but not for content's author. I displayed all information, including [node:author], all correct.

I attached the exported files for your reference.

Thank you for your help!

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Fixed

Version

1.1

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sdsc
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Does the user who updates the entity, also have the permission to assign the role to themselves? Most likely not, therefore, you need to switch the user first before you can do that.

    Note, when debugging ECA (see Debugging) you can see that the action doesn't execute because of access denied.

    Second note: if you export an ECA model from the ECA overview page, you get an archive file which contains everything needed, and you can upload that archive to issues like this, not the individual files. That way, other can import that archive into their Drupal site for testing.

  • The author of the entity is a regular user, who cannot assign a role to themselves. I, as the admin, can. I update the entity, and the ECA rule kicks to assign the role to the author, but ends up assigned to mine. Looks that the ECA update role only updates current log in user's role.

    I think it is still a bug.

    The archive file with "tar" cannot be loaded.

    Regards,

  • 🇩🇪Germany jurgenhaas Gottmadingen

    The user_add_role_action action plugin is part of Drupal core, and it certainly adds the role to the user account that's provided as the argument. In your case, that's the author of the node, which is not necessarily the current user.

    The archive file with "tar" cannot be loaded.

    When exporting from ECA, you get a *.tar.gz file which can be uploaded without any problems.

  • I loaded the file. Appreciate your help!

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Well, don't think I can help other than advising you that you need to switch the user context with the action, as the user doesn't have permission as you already confirmed.

  • I am confused by your comment "as the user doesn't have permission as you already confirmed". Only admin can access all users' account to manually change their roles, that is what I mean author of an entity cannot change their roles themselves. But all users' role can be changed via a function when certain conditions met.

    I put the [node:author] in the argument, it ends up the role was added to mine (I am testing the code and I am not the author). This is the issue I am experiencing. I displayed [node:author] in message, correct information.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    I am confused by your comment "as the user doesn't have permission as you already confirmed". Only admin can access all users' account to manually change their roles, that is what I mean author of an entity cannot change their roles themselves. But all users' role can be changed via a function when certain conditions met.

    ECA models are processed in the user context of the current user. So, only actions can be executed, that the current user has permission to do so. That's what you can see when you use the debugging techniques that I linked to above.

    If you want to do things outside of those limitations, you need to use the action to switch the user before doing such privileged actions.

    In our video library there are many examples that demonstrate that.

    I put the [node:author] in the argument, it ends up the role was added to mine (I am testing the code and I am not the author). This is the issue I am experiencing. I displayed [node:author] in message, correct information.

    The action plugin from Drupal core needs a user entity. From looking at the token browser (or in the debug log, see above), it looks as if [node:author] is not an entity, it's an array.

    To resolve that, you may have to load the user entity by its ID that you get from the token [node:author:uid]. The is an action plugin for loading entities, that's what should help here.

  • I tried [node:author],[node:author:roles],[node:author:uid] in the argument, still the role was added to mine not to the author. I switched to the action "PRE-CONFIGURED: ADD THE FORUM ROLE TO THE SELECTED USER(S). add forum" still the same result.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    I'm not surprised, none of those tokens is a user entity. Have you tried my recommendation to load the user entity by the author id?

    Also, have you tried debugging? What's the output at that point?

  • Yes, I tried your recommendation using [node:author:uid], no luck. Here is the log, looks like the code didn't check the argument:

    User Time Message
    家慧中医 11:33:55 Lazy appliance check for event eca.content_entity.update regarding ECA ID process_qz81va1 resulted to apply.
    - user (Entity user/1/家慧中医)
    家慧中医 11:33:55 Loading ECA configurations for event eca.content_entity.update: process_qz81va1.
    - user (Entity user/1/家慧中医)
    家慧中医 11:33:55 Check visit updated (Event_1oxmtw1) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.
    - user (Entity user/1/家慧中医)
    家慧中医 11:33:55 Start visit updated (Event_1oxmtw1) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.
    家慧中医 11:33:55 Check action successor print (Activity_094ckp5) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.
    - entity (Entity node/visit/65327/07/07/2018 - 18:18)
    - node (Entity node/visit/65327/07/07/2018 - 18:18)
    - event (DTO)
    - machine-name (string "eca.content_entity.update")
    - user (Entity user/1/家慧中医)
    家慧中医 11:33:55 Unconditional print (Activity_094ckp5) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.
    家慧中医 11:33:55 Execute print (Activity_094ckp5) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.
    家慧中医 11:33:55 Check action successor add forum (Activity_0qotvgg) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.
    - entity (Entity node/visit/65327/07/07/2018 - 18:18)
    - node (Entity node/visit/65327/07/07/2018 - 18:18)
    - event (DTO)
    - machine-name (string "eca.content_entity.update")
    - user (Entity user/1/家慧中医)
    家慧中医 11:33:55 Unconditional add forum (Activity_0qotvgg) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.
    家慧中医 11:33:55 Execute add forum (Activity_0qotvgg) from ECA Edit User Role (process_qz81va1) for event Drupal\eca_content\Event\ContentEntityUpdate.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    The log doesn't show anything about the model loading the user entity from the author uid. Am I missing something?

  • 🇩🇪Germany jurgenhaas Gottmadingen

    And then, when you've loaded the author entity into a token, you should only provide the token name in the field entity of the Add a role to the selected users action, since this field asks for "Provide the token name of the user that this action should operate with.", not for a token with the brackets.

    You can read more about tokens and token names here: Tokens

  • I got it! [node:author:uid] is token, while token name is node or user. So the "add a role to the selected user" is by default to add the role to THE log-in user, not any selected user. I think the argument is not even necessary, but misleading.

    Thank you for your help!

  • 🇩🇪Germany jurgenhaas Gottmadingen

    That's not the point. You need to use the Load entity action to load the user for [node:author:uid], then you can use that token as an argument to your assign role action.

  • Oh, I see!! I added "load the entity" of the author, named it as "this_author", and then in the entity argument, I put "this_author". It WORKS now!!!!

    Wow! Thank you really very much! not just for this issue, but to understand how ECA works!

    Thank you again for your patience!!

  • Status changed to Fixed 8 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Glad we got through that together. Happy modelling with ECA ;-)

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

Production build 0.71.5 2024