user_role_permissions
Definition
user_role_permissions($roles = array(), $reset = FALSE)
modules/user/user.module, line 465
Description
Determine the permissions for one or more roles.
Parameters
$roles An array whose keys are the role IDs of interest, such as $user->roles.
$reset Optional parameter - if TRUE data in the static variable is rebuilt.
Return value
An array indexed by role ID. Each value is an array whose keys are the permission strings for the given role ID.
Code
<?php
function user_role_permissions($roles = array(), $reset = FALSE) {
static $stored_permissions = array();
if ($reset) {
// Clear the data cached in the static variable.
$stored_permissions = array();
}
$role_permissions = $fetch = array();
if ($roles) {
foreach ($roles as $rid => $name) {
if (isset($stored_permissions[$rid])) {
$role_permissions[$rid] = $stored_permissions[$rid];
}
else {
// Add this rid to the list of those needing to be fetched.
$fetch[] = $rid;
// Prepare in case no permissions are returned.
$stored_permissions[$rid] = array();
}
}
if ($fetch) {
// Get from the database permissions that were not in the static variable.
// Only role IDs with at least one permission assigned will return rows.
$result = db_query("SELECT r.rid, p.permission FROM {role} r INNER JOIN {role_permission} p ON p.rid = r.rid WHERE r.rid IN (" . db_placeholders($fetch) . ")", $fetch);
while ($row = db_fetch_array($result)) {
$stored_permissions[$row['rid']][$row['permission']] = TRUE;
}
foreach ($fetch as $rid) {
// For every rid, we know we at least assigned an empty array.
$role_permissions[$rid] = $stored_permissions[$rid];
}
}
}
return $role_permissions;
}
?> 