Is there a Drupal 7 function that lists all the user permissions defined from enabled modules?

What I need for a module I am implementing is getting all the permissions and adding to each one a checkbox list.

Is it possible to make the value of the checkbox the same shown in the "permission" column of the "role_permission" table?


There isn't a Drupal function for that. You just need to invoke the hook_permission() implementations done from the enabled modules to get the permissions they implement.

$permissions = module_invoke_all('permission');

foreach ($permissions as $permission => $perm_item) {
  // $permission is the string used in the database.
  // $perm_item['title'] is the string normally used in the UI.

There is a Drupal function for that, user_permission_get_modules(), which returns an associative array, whose keys are the permissions and values are the modules that define those permissions.

If you only need the permissions without the module names, you can use array_keys:

$permissions = user_permission_get_modules();
$permissions = array_keys($permissions);

In a custom module add this code (Devel module needs to be enabled) use the following code.

Alternatively, paste this code into Development->Execute PHP Code (you'll need to TEMPORARILY grant the user the the Devel:Access Developer Information and Devel:Execute PHP Code permissions and remove after testing):

This will output using the dpm() function all the current users' roles and all their permissions (all, granted and denied) in the format ModuleName:PermissionName -> True/False

global $user;
dpm($user->roles, 'user roles');

$permissions = user_permission_get_modules();
foreach ($permissions as $key=>$value) {
  $has_perm = user_access($key);
  $perms["$value:$key"] = $has_perm;  
  if ($has_perm) {
    $perms_true["$value:$key"] = $has_perm;  
  } else {
    $perms_false["$value:$key"] = $has_perm;  
dpm($perms, 'all user permissions');
dpm($perms_true, 'granted user permissions');
dpm($perms_false, 'denied user permissions');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.