When a role is changed as a result of a PayPal subscription, other modules are not notified.
For details and an example, see here: http://drupal.org/node/137206

CommentFileSizeAuthor
#4 lm_paypal_subscriptions-gain_role.patch1.16 KBtoemaz

Comments

LeeMcL’s picture

According to http://drupal.org/node/137206 I am missing calls to hooks to inform other modules of role changes. I cannot find what these hooks are called so I've posted a comment on the end of that issue asking for their names so I can add them.

I guess these hooks are new as they didn't exist when I wrote LM_PayPal.

Lee

MrGeek’s picture

The author posted a response to your question here:
http://drupal.org/node/137206#comment-233399

toemaz’s picture

I use lm_payal subscriptions for D5 and I'm in need of capturing when a role has been assigned to a user. Using user_save instead of inserting the role directly into the database solve this issue. So I rewrote function lm_paypal_user_gain_role a little. The same should be done with

function lm_paypal_user_gain_role($uid, $rid) {
  _lm_paypal_subscriptions_ini();
  global $_lm_paypal_subscriptions_menu_rebuild;

  $account = user_load(array('uid' => $uid));
  if (!array_key_exists($rid, $account->roles)) {
    user_save($account, array('roles' => array_merge(array_keys($account->roles) + array($rid))));
    
    if ($_lm_paypal_debug) {
      watchdog(LM_PAYPAL_SUBSCRIPTIONS, t('Added user to role (uid %uid, rid %rid)', array('%uid' => $uid, '%rid' => $rid)));
    }

    if ($_lm_paypal_subscriptions_menu_rebuild) {
      menu_rebuild();
    }
  }
  else {
    watchdog(LM_PAYPAL_SUBSCRIPTIONS, t('User has role already (uid %uid, rid %rid)', array('%uid' => $uid, '%rid' => $rid)), WATCHDOG_ERROR);
  }
}

The same rewrite should be done for lm_paypal_user_loose_role

Remark: this code is for the D5 release but it's applicable to D6 as well since user_load and user_save didn't change.

toemaz’s picture

Version: master » 6.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new1.16 KB

Attached patch which correctly uses user_save instead of directly performing an sql query which is not according the Drupal coding standards. The function user_save() has to be called so other modules can be invoked!

lyricnz’s picture

That looks okay from a visual inspection. Did you run the simpletests?

lyricnz’s picture

Ugh, simpletests seem pretty broken at the moment, due something committed by another maintainer.

fehin’s picture

I ran 6x with the patch through coder and it did not change the user_save line. Can I use that line as-is in 7x?

I need the user_save function for creating a rule to flag user on role change. I'm having a problem with that now because the current setup does not indicate account as been updated when the new role is assigned.

toemaz’s picture

fehin’s picture

Hi toemaz, thanks for the link.
The only difference between D6 and D7 is "array=" is replaced with "edit=". I'm not that good with drupal API yet but it looks like your current code should work with D7 too. Am I correct?

toemaz’s picture

That's correct, but I haven't checked it beyond checking the function only.

fehin’s picture

Thank you! I appreciate it.

john franklin’s picture

Status: Needs review » Fixed

Fixed in b38bc43. Modified version of the patch in #4 using user_multiple_role_edit().

Status: Fixed » Closed (fixed)

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