I am trying to set up an event that triggers when a user is added to a role. I added two conditions. The user is in a role, and the unchanged user is not in a role. After doing a lot of debugging, I found that both the changed and unchanged user's roles are set to the same thing, even though that is the only thing i'm changing. Has anyone else had the same problem? Is there a patch to fix this?

Comments

tborrome’s picture

glazer - did you find out any more on the problem you were seeing?

I have the same problem.

what I found though is that the unchanged user does not seem to have the correct assigned roles mapped to it, which is what's causing the incorrect rule behavior.

I tested the following conditions on a user that has a role "Test Role" assigned.

1. The user is in a role "Test Role", and the unchanged user is not in a "Test Role". This will always return true, even if the user already "Test Role" assigned before running the test. So, the rule gets triggered for any change in the user's profile, which is not correct.

2. The unchanged user is not in "Test Role". This always returns true.

3. The unchanged user is in "Test Role". This always returns false.

Looks like the unchanged user role is not picking up the actual roles assigned to it??

fago’s picture

Status: Active » Fixed

I think the bug is that the unchanged user was copied for later without cloning it - so since php5 takes objects by reference by default the changes appear in the unchanged user too. I fixed that to use drupal_clone(). Please test the next dev-snapshot.

tborrome’s picture

Status: Fixed » Active

I tried the latest module but the problem is still occurring. Actually to be clearer, let me try to explain by describing the use-case:

The workflow use-case is as follows:
When a new role is added to a user, send the user email about this.

To do this, I use 2 conditions: Updated User has a new role and Unchanged User does not have that role.

The workflow logic works if a new role gets added to the user account. The user gets notified via email that he has a new role. So that's great, so far.

But, if I change any other user information like any profile fields (first name, last name), the email gets sent for all the user's existing roles. This may be because the Unchanged User is not picking up all roles assigned when role information hasn't changed. So the workflow module thinks that all those existing roles are new and will send email for each of the user's existing roles.

Hope I conveyed this clear - let me know if more info is needed. thanks.

tborrome’s picture

Also - FYI - here's the rule export for your reference. Please advise if there's something wrong with it. Thanks!

array (
'cfg_13' =>
array (
'#type' => 'configuration',
'#altered' => false,
'#event' => 'user_update',
'#active' => 0,
'#label' => 'Blogger2 role is approved',
'#id' => 1,
'#weight' => '0',
'#module' => 'workflow-ng',
0 =>
array (
'#id' => 2,
'#label' => 'User has Blogger role',
'#type' => 'condition',
'#name' => 'workflow_ng_condition_user_hasrole',
'#settings' =>
array (
'roles' =>
array (
0 => 12,
),
'operation' => 'OR',
),
'#argument map' =>
array (
'account' => 'user',
),
),
1 =>
array (
'#negate' => 1,
'#type' => 'condition',
'#name' => 'workflow_ng_condition_user_hasrole',
'#label' => 'Unchanged user has no Blogger role',
'#id' => 3,
'#argument map' =>
array (
'account_unchanged' => 'user',
),
'#settings' =>
array (
'roles' =>
array (
0 => 12,
),
'operation' => 'OR',
),
),
3 =>
array (
'#type' => 'OR',
'#id' => 4,
),
2 =>
array (
'#label' => 'Send a mail to an arbitrary mail address',
'#id' => 6,
'#type' => 'action',
'#name' => 'workflow_ng_action_mail',
'#settings' =>
array (
'to' => '[account:mail],[account:site-mail]',
'to_args' =>
array (
0 => 'account',
),
'from' => '[account:site-mail]',
'from_args' =>
array (
0 => 'account',
),
'subject' => '[[user:site-name]] Application for Blogger role has been approved',
'subject_args' =>
array (
0 => 'user',
),
'message' => '[account:user],

Your application for the role Blogger in [user:site-name] has been approved.

You can now create Blog Entries by clicking on the My Blog section of the site.

[account_unchanged:site-url]/blog/[account:uid]',
'message_args' =>
array (
0 => 'account',
1 => 'user',
2 => 'account_unchanged',
),
),
),
'#name' => 'cfg_13',
),
)

ron_s’s picture

Version: 5.x-2.1 » 5.x-2.2

I can also confirm this is not working as I would expect in 5.x-2.2. We have a situation where some custom PHP code is run only if a user is given the role of "participant" (role #7). So we have the following workflow condition:

return empty($account_unchanged->roles[7]) && !empty($account->roles[7]);

Therefore if role 7 is initially empty ($account_unchanged), then has a value ($account), execute the custom PHP code. When I set a users role to "participant" and print the values to the page, I see the following:

$account_unchanged->roles[7] =
$account->roles[7] = participant
empty($account_unchanged->roles[7]) = 1
!empty($account->roles[7]) = 1

Looks perfect. However then I attempt to edit some other information on the user profile, and I get the exact same results:

$account_unchanged->roles[7] =
$account->roles[7] = participant
empty($account_unchanged->roles[7]) = 1
!empty($account->roles[7]) = 1

What I would expect to see is this:

$account_unchanged->roles[7] = participant
$account->roles[7] = participant
empty($account_unchanged->roles[7]) =
!empty($account->roles[7]) = 1

I guess maybe a better question for fago is, what is your definition of unchanged user? I think tborrome and I assume "unchanged user" means "previous state user." Therefore "updated user" = "current state" and "unchanged user" = "last state before the current state." Can you confirm if this is your interpretation?

If we are all in agreement, then the current code is not working as expected.