function uc_roles_grant in Ubercart 6.2
Same name and namespace in other branches
- 7.3 uc_roles/uc_roles.module \uc_roles_grant()
Grants a role to a given user.
This function grants a given role to a user's list of roles. If there is a previous record of this user/role combination, it is first removed. The function then saves the user (if $user_save is TRUE). Next, a check to verify the role actually exists, if not, no expiration data is stored. The menu cache is flushed, as new menu items may be visible after the new role is granted. The function notifies the user of the role grant.
Parameters
$account: A Drupal user object.
$rid: A Drupal role ID.
$timestamp: When this role will expire.
$save_user: Optimization to prevent unnecessary user saving when calling from hook_user().
$silent: When set to TRUE will suppress any Drupal messages from this function.
2 calls to uc_roles_grant()
- uc_roles_action_order_renew in uc_roles/
uc_roles.ca.inc - Renews an orders product roles.
- uc_roles_user_submit in uc_roles/
uc_roles.module - Implements hook_user_submit().
File
- uc_roles/
uc_roles.module, line 1233
Code
function uc_roles_grant(&$account, $rid, $timestamp, $save_user = TRUE, $silent = FALSE) {
global $user;
// First, delete any previous record of this user/role association.
uc_roles_delete($account, $rid, $silent);
if ($save_user) {
// Punch the role into the user object.
$roles_list = $account->roles + array(
$rid => _uc_roles_get_name($rid),
);
$account = user_save($account, array(
'roles' => $roles_list,
));
}
// If the role expires, keep a record.
if (!is_null($timestamp)) {
db_query("INSERT INTO {uc_roles_expirations} (uid, rid, expiration) VALUES (%d, %d, %d)", $account->uid, $rid, $timestamp);
}
// Flush visible menu items, since our permissions could've changed.
_uc_roles_flush_menu_cache($account);
// Display the message if appropriate.
if (!$silent) {
$role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $rid));
if ($user->uid == $account->uid) {
$message = t('You have been granted the %role role.', array(
'%role' => $role_name,
));
}
else {
$message = t('%user has been granted the %role role.', array(
'%user' => $account->name,
'%role' => $role_name,
));
}
if ($timestamp) {
$message .= t(' It will expire on %date', array(
'%date' => format_date($timestamp, 'small'),
));
}
drupal_set_message($message);
}
module_invoke_all('uc_roles_grant', $account, $rid, $timestamp);
}