global $user ROLES and PERMISSIONS

Public

function user_role_save

7 user.module user_role_save($role)
Save a user role to the database.

Parameters

$role: A role object to modify or add. If $role->rid is not specified, a new role will be created.

Return value

Status constant indicating if role was created or updated. Failure to write the user role record will return FALSE. Otherwise. SAVED_NEW or SAVED_UPDATED is returned depending on the operation performed.

Get raw version
php
  1. function user_role_save($role) {
  2. if ($role->name) {
  3. // Prevent leading and trailing spaces in role names.
  4. $role->name = trim($role->name);
  5. }
  6. if (!isset($role->weight)) {
  7. // Set a role weight to make this new role last.
  8. $query = db_select('role');
  9. $query->addExpression('MAX(weight)');
  10. $role->weight = $query->execute()->fetchField() + 1;
  11. }
  12.  
  13. // Let modules modify the user role before it is saved to the database.
  14. module_invoke_all('user_role_presave', $role);
  15.  
  16. if (!empty($role->rid) && $role->name) {
  17. $status = drupal_write_record('role', $role, 'rid');
  18. module_invoke_all('user_role_update', $role);
  19. }
  20. else {
  21. $status = drupal_write_record('role', $role);
  22. module_invoke_all('user_role_insert', $role);
  23. }
  24.  
  25. // Clear the user access cache.
  26. drupal_static_reset('user_access');
  27. drupal_static_reset('user_role_permissions');
  28.  
  29. return $status;
  30. }

if working with multiple rolls, access them this way

$user->roles[0] admin
$user->roles[1] anonymous user
$user->roles[2] authenticated user1
$user->roles[3] authenticated user2

Keep in mind that $user->roles is an associative array keyed by the role id (rid)... i.e., with foreach($user->roles as $k => $v), $k is the rid and $v is the role name...

Get raw version
php
  1. function user_role_permissions($roles = array()) {
  2. $cache = &drupal_static(__FUNCTION__, array());
  3.  
  4. $role_permissions = $fetch = array();
  5.  
  6. if ($roles) {
  7. foreach ($roles as $rid => $name) {
  8. if (isset($cache [$rid])) {
  9. $role_permissions [$rid] = $cache [$rid];
  10. }
  11. else {
  12. // Add this rid to the list of those needing to be fetched.
  13. $fetch [] = $rid;
  14. // Prepare in case no permissions are returned.
  15. $cache [$rid] = array();
  16. }
  17. }
  18.  
  19. if ($fetch) {
  20. // Get from the database permissions that were not in the static variable.
  21. // Only role IDs with at least one permission assigned will return rows.
  22. $result = db_query("SELECT rid, permission FROM {role_permission} WHERE rid IN (:fetch)", array(':fetch' => $fetch));
  23.  
  24. foreach ($result as $row) {
  25. $cache [$row->rid][$row->permission] = TRUE;
  26. }
  27. foreach ($fetch as $rid) {
  28. // For every rid, we know we at least assigned an empty array.
  29. $role_permissions [$rid] = $cache [$rid];
  30. }
  31. }
  32. }
  33.  
  34. return $role_permissions;
  35. }

function hook_user_role_insert

7 user.api.php hook_user_role_insert($role)
Respond to creation of a new user role.

Modules implementing this hook can act on the user role object when saved to the database. It's recommended that you implement this hook if your module adds additional data to user roles object. The module should save its custom additions to the database.

Parameters

$role: A user role object.

Related topics

Hooks
Allow modules to interact with the Drupal core.

Get raw version
php
  1. function hook_user_role_insert($role) {
  2. // Save extra fields provided by the module to user roles.
  3. db_insert('my_module_table')->fields(array(
  4. 'rid' => $role->rid,
  5. 'role_description' => $role->description,
  6. ))->execute();
  7. }

function user_role_permissions

7 user.module user_role_permissions($roles = array())
8 user.module user_role_permissions(array $roles)
Determine the permissions for one or more roles.

Parameters

$roles: An array whose keys are the role IDs of interest, such as $user->roles.

Return value

If $roles is a non-empty array, an array indexed by role ID is returned. Each value is an array whose keys are the permission strings for the given role ID. If $roles is empty nothing is returned.

Get raw version
php
  1. function mymodule_enable() {
  2. $newRoles = array('new role', 'other role');
  3. foreach ($newRoles as $r){
  4. if(!user_role_load_by_name($r)){
  5. $role = new stdClass();
  6. $role->name = $r;
  7. user_role_save($role);
  8. }
  9. }
  10. }
  11.  
  12. $admin_role = user_role_load_by_name('administrator');
  13. $roles = array($admin_role->rid => $admin_role->name);