Using the latest release of entity API and either 7.x-2.1 or 7.x-2.x-dev of Rules, I've encountered an issue where adding a role to the user via rules deletes the existing user picture (provided by core).

However, if I manually edit an account and add the same role, the picture is maintained upon clicking save.

This rule uses the commerce_sp feature (modified to add the role). Edit: however, I have the same issue when running views bulk operations to update the role.

Rule Export

{ "rules_commerce_sp_subscription_started_update" : {
    "LABEL" : "Subscription started (update)",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "commerce_sp" ],
    "REQUIRES" : [ "rules", "rules_scheduler" ],
    "ON" : [ "user_update" ],
    "IF" : [
      { "NOT data_is_empty" : { "data" : [ "account:field-commerce-sp-validity" ] } },
      { "data_is_empty" : { "data" : [ "account-unchanged:field-commerce-sp-validity" ] } }
    ],
    "DO" : [
      { "component_rules_commerce_sp_subscription_start" : { "user" : [ "account" ] } },
      { "schedule" : {
          "component" : "rules_commerce_sp_subscription_end",
          "date" : [ "account:field-commerce-sp-validity" ],
          "identifier" : "expire-[account:uid]",
          "param_user" : [ "account" ]
        }
      },
      { "user_add_role" : { "account" : [ "account" ], "roles" : { "value" : { "6" : "6" } } } }
    ]
  }
}

Rules debug information:

"Reacting on event Completing the checkout process.
0 ms Reacting on event Completing the checkout process.
12.69 ms Evaluating conditions of rule Update the order status on checkout completion. [edit]
12.716 ms AND evaluated to TRUE.
"

Rule Update the order status on checkout completion fires. [edit]
0 ms Rule Update the order status on checkout completion fires.
11.72 ms Evaluating the action commerce_order_update_state. [edit]
45.698 ms Rule Update the order status on checkout completion has fired.
58.69 ms Evaluating conditions of rule Apply bought subscription time. [edit]
58.707 ms AND evaluated to TRUE.
"

Rule Apply bought subscription time fires. [edit]
0 ms Rule Apply bought subscription time fires.
0.624 ms Looping over the list items of commerce-order:commerce-line-items [edit]
1.104 ms Evaluating the action component_rules_commerce_sp_line_item_is_subscription_product. [edit]
"

Evaluating rule Line item is subscription product. [edit]
0 ms Evaluating rule Line item is subscription product.
0.282 ms Evaluating conditions of rule Line item is subscription product. [edit]
2.359 ms The condition data_is evaluated to TRUE [edit]
6.176 ms The condition data_is evaluated to TRUE [edit]
7.643 ms The condition data_is evaluated to TRUE [edit]
7.66 ms AND evaluated to TRUE.
"

Rule Line item is subscription product fires. [edit]
0 ms Rule Line item is subscription product fires.
2.463 ms Evaluating the action data_calc. [edit]
2.618 ms Added the provided variable subscription_time of type duration [edit]
3.211 ms Evaluating the action component_rules_commerce_sp_subscription_time_to_validity. [edit]
"

Evaluating rule set Apply bought subscription time to user subscription validity. [edit]
0 ms Evaluating rule set Apply bought subscription time to user subscription validity.
0.302 ms Evaluating conditions of rule User has no subscription yet. [edit]
0.669 ms The condition data_is_empty evaluated to TRUE [edit]
0.684 ms AND evaluated to TRUE.
"

Rule User has no subscription yet fires. [edit]
0 ms Rule User has no subscription yet fires.
38.027 ms Evaluating the action data_set. [edit]
39.127 ms Rule User has no subscription yet has fired.
40.001 ms Evaluating conditions of rule Add the time period to the validity date. [edit]
40.046 ms AND evaluated to TRUE.
"

Rule Add the time period to the validity date fires. [edit]
0 ms Rule Add the time period to the validity date fires.
1.928 ms Evaluating the action data_calc. [edit]
2.931 ms Added the provided variable result of type date [edit]
4.624 ms Evaluating the action data_set. [edit]
6.263 ms Rule Add the time period to the validity date has fired.
46.797 ms Finished evaluation of rule set Apply bought subscription time to user subscription validity.
51.01 ms Rule Line item is subscription product has fired.
58.928 ms Finished evaluation of rule Line item is subscription product.
60.938 ms Rule Apply bought subscription time has fired.
120.316 ms Evaluating conditions of rule Assign an anonymous order to a pre-existing user. [edit]
122.801 ms The condition data_is evaluated to FALSE [edit]
122.846 ms AND evaluated to FALSE.
123.661 ms Evaluating conditions of rule Create a new account for an anonymous order. [edit]
125.543 ms The condition data_is evaluated to FALSE [edit]
125.585 ms AND evaluated to FALSE.
125.994 ms Evaluating conditions of rule Send an order notification e-mail. [edit]
126.033 ms AND evaluated to TRUE.
"

Rule Send an order notification e-mail fires. [edit]
0 ms Rule Send an order notification e-mail fires.
261.69 ms Evaluating the action mail. [edit]
2625.304 ms Rule Send an order notification e-mail has fired.
2752.262 ms Evaluating conditions of rule Create Coffee Alert. [edit]
2757.264 ms The condition commerce_order_contains_product evaluated to FALSE [edit]
2757.291 ms AND evaluated to FALSE.
2757.43 ms Evaluating conditions of rule Create Dinner Alert. [edit]
2759.492 ms The condition commerce_order_contains_product evaluated to FALSE [edit]
2759.53 ms AND evaluated to FALSE.
2759.649 ms Evaluating conditions of rule Create Fly Alert. [edit]
2762.739 ms The condition commerce_order_contains_product evaluated to FALSE [edit]
2762.767 ms AND evaluated to FALSE.
2762.904 ms Evaluating conditions of rule Create Flowers Alert. [edit]
2765.059 ms The condition commerce_order_contains_product evaluated to FALSE [edit]
2765.082 ms AND evaluated to FALSE.
2765.246 ms Evaluating conditions of rule Create Drink Alert. [edit]
2767.54 ms The condition commerce_order_contains_product evaluated to FALSE [edit]
2767.565 ms AND evaluated to FALSE.
2768.145 ms Evaluating conditions of rule Create Ciao Alert. [edit]
2770.081 ms The condition commerce_order_contains_product evaluated to FALSE [edit]
2770.105 ms AND evaluated to FALSE.
2770.305 ms Saved list_item:order:owner of type user.
"

Reacting on event After updating an existing user account. [edit]
0 ms Reacting on event After updating an existing user account.
1.9 ms Evaluating conditions of rule Subscription validity is unset. [edit]
4.98 ms The condition data_is evaluated to FALSE [edit]
5.621 ms The condition data_is_empty evaluated to FALSE [edit]
5.662 ms AND evaluated to FALSE.
5.875 ms Evaluating conditions of rule Subscription started (update). [edit]
6.402 ms The condition data_is_empty evaluated to FALSE [edit]
7.776 ms The condition data_is_empty evaluated to TRUE [edit]
7.816 ms AND evaluated to TRUE.
"

Rule Subscription started (update) fires. [edit]
0 ms Rule Subscription started (update) fires.
1.092 ms Evaluating the action user_add_role. [edit]

6.483 ms Evaluating the action component_rules_commerce_sp_subscription_start. [edit]
"

Evaluating rule set Subscription start. [edit]
0 ms Evaluating rule set Subscription start.
0.417 ms Finished evaluation of rule set Subscription start.
24.114 ms Evaluating the action schedule. [edit]
46.396 ms Rule Subscription started (update) has fired.
54.415 ms Evaluating conditions of rule Subscription validity changed. [edit]
55.82 ms The condition data_is evaluated to FALSE [edit]
56.08 ms The condition data_is_empty evaluated to FALSE [edit]
56.401 ms The condition data_is_empty evaluated to TRUE [edit]
56.42 ms AND evaluated to FALSE.
56.491 ms Saved account of type user.
"

Reacting on event After updating an existing user account. [edit]
0 ms Reacting on event After updating an existing user account.
1.227 ms Evaluating conditions of rule Subscription validity is unset. [edit]
3.278 ms The condition data_is evaluated to FALSE [edit]
3.724 ms The condition data_is_empty evaluated to FALSE [edit]
3.763 ms AND evaluated to FALSE.
3.976 ms Not evaluating reaction rule Subscription started (update) to prevent recursion. [edit]
4.112 ms Evaluating conditions of rule Subscription validity changed. [edit]
7.217 ms The condition data_is evaluated to FALSE [edit]
7.694 ms The condition data_is_empty evaluated to FALSE [edit]
8.219 ms The condition data_is_empty evaluated to TRUE [edit]
8.256 ms AND evaluated to FALSE.
8.399 ms Finished reacting on event After updating an existing user account.
317.708 ms Finished reacting on event After updating an existing user account.
3590.513 ms Finished reacting on event Completing the checkout process.

Comments

webdrips’s picture

Component: Rules Engine » Rules Core

Note to get this working correctly, I had to create a separate rule and add the role using the user saved instead of user updated action.

yseki’s picture

I have de same problem.

  • Enabled user pictures
  • Added a rule to add a role to users after user update.

I was debugging the proccess and I saw what happens.

First the account is saved, including the user picture, then it is called again, but without the information about de picture. As I could saw, the second proccess of user_save is called before the first user save was completed so, the picture has not the information about if it was already removed from temporary directory and it was not updated to account object so, it try to remove again.

When I have disabled the rule, the function user_save() was called only once, and the picture was ok.

In my opinion it is definitly a bug.

yseki’s picture

Here is the step by step to reproduce the bug.

  1. Just to make sure that not was anything else that could interfere, start the process in a clean Drupal 7 installation.
  2. Install rules module, from http://drupal.org/project/rules
  3. Create a new boolean field at account settings called field_check_to_add_new_role
  4. Create a new User Role, I called as Dummy Role
  5. Create a new Rule to add Dummy Role
    • EVENT: After updating an existing user account
    • CONDITION: NOT Data value is empty. Parameter: Data to check: [account:field-check-to-add-new-role]
    • ACTIONS: Add user role. Parameter: User: [account], Roles: dummy role
  6. Update a user account, send a user picture, check the option field-check-to-add-new-role and you will see the error;
  7. Remove desable the rule;
  8. Update a user account again, send a user picture, check the option field-check-to-add-new-role and the error is gone!

As I saw debugging the action, its happen because the usual process to save users roles is to call the function user_save($account, array('roles' => $roles_array)) with all roles to user inside the $roles_array variable.

The point is in user.module file, at line 565 there is module_invoke_all('entity_update', $account, 'user'); I think this is the point. When this function is called, began the process from Rules Module, and it calls the user_save() again, but without the correct information about the picture already updated, because it is not at temporary directory anymore, but the $edit variable is still pointing to there, so the error apear and the user picture is lost.

fenda’s picture

I'm getting this problem too.. it's destroying all of my users pictures!

yseki’s picture

Hello drupaljoe,

To avoid this problem, I removed all Roles after user update and programmatically I implemented the things that I needed.
I hope klausi or fago will solve this issue in the next release.

Best regards,

Yuri Seki
www.yuriseki.com

yseki’s picture

Issue summary: View changes

Edit: however, I have the same issue when running views bulk operations to update the role.

sprucemoose’s picture

I have been having the same problem, but with profile2 fields set to trigger a rule that removes a role.

EVENT: After updating an existing profile
CONDITION: NOT Data value is empty. Parameter: Data to check: [profile:field-check-to-add-new-role]
ACTIONS: Add user role. Parameter: User: [account], Roles: dummy role

deletes the user picture.

Changing the EVENT to 'before saving a profile' seems to intermittently fail to add the role and sometimes the trigger field does not save properly. I have not investigated this much further.

Changing the EVENT to 'profile is viewed' worked fine for me.