You are here

function _role_action in Ubercart 5

Function will perform actions on a role toward a user

Parameters

$op: The action to take

  • delete: remove a role expiration
  • grant: add new role
  • revoke: remove role
  • renew: update role's expiration date

$user: The user object that is getting updated

$rid: The role id that has actions being performed

$timestamp: The timestamp for the applicable action

3 calls to _role_action()
uc_roles_cron in uc_roles/uc_roles.module
Implementation of hook_cron().
uc_roles_order in uc_roles/uc_roles.module
Implementation of hook_order().
uc_roles_user in uc_roles/uc_roles.module
Implementation of hook_user().

File

uc_roles/uc_roles.module, line 1090
Grants roles upon accepted payment of products

Code

function _role_action($op, $user, $rid, $timestamp = NULL) {
  switch ($op) {
    case 'delete':
      db_query("DELETE FROM {uc_roles_expirations} WHERE uid = %d AND rid = %d", $user->uid, $rid);
      break;
    case 'grant':
      if (!in_array($rid, array_keys($user->roles))) {
        $roles_list = $user->roles + array(
          $rid => _get_role_name($rid),
        );
        user_save($user, array(
          'roles' => $roles_list,
        ));
        if (!is_null($timestamp)) {
          db_query("INSERT INTO {uc_roles_expirations} (uid, rid, expiration) VALUES (%d, %d, %d)", $user->uid, $rid, $timestamp);
        }
      }
      break;
    case 'revoke':
      $roles_list = $user->roles;
      unset($roles_list[$rid]);
      user_save($user, array(
        'roles' => $roles_list,
      ));
      db_query("DELETE FROM {uc_roles_expirations} WHERE uid = %d AND rid = %d", $user->uid, $rid);
      break;
    case 'renew':
      if (!is_null($timestamp)) {
        $expiration = db_result(db_query("SELECT expiration FROM {uc_roles_expirations} WHERE uid = %d AND rid = %d", $user->uid, $rid));
        if ($expiration) {
          db_query("UPDATE {uc_roles_expirations} SET expiration = %d WHERE uid = %d AND rid = %d", $timestamp, $user->uid, $rid);

          //Add role in case it doesn't exist
          if (!in_array($rid, array_keys($user->roles))) {
            $roles_list = $user->roles + array(
              $rid => _get_role_name($rid),
            );
            user_save($user, array(
              'roles' => $roles_list,
            ));
          }
        }
      }
      break;
    default:
      break;
  }

  //Clear menu cache that might not contain new items granted by new permission
  if ($op == 'grant' || $op == 'revoke') {
    cache_clear_all($user->uid . ':', 'cache_menu', TRUE);
  }
}