27

user_access($string, $account = NULL)

$string: The permission, such as "administer nodes", being checked for.

How can I find out a list of available $string values for user_access() function?

11 Answers 11

12

As long as every module can define their own permissions, there is not a "strict" list of those string. You will need to "construct" it if you really need to have such a list programmatically.

You can run this script in a /devel/php page. (Of course, you need the Devel module.)

// Render role/permission overview:
$options = array();
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
  print_r($module);
  // Drupal 6
  // if ($permissions = module_invoke($module, 'perm')) {
  //  print_r($permissions);
  // }

  // Drupal 7
  if ($permissions = module_invoke($module, 'permission')) {
    print_r($permissions);
  }
}
0
27

As @Berdir said, the easiest way to see which permissions are available is to go to the permissions admin/people/permissions in Drupal 7.

The problem I kept running into is that I could never figure out what string I needed to supply as an 'access argument' to actually make use of the permission. Well, here is how you find the string name for the permissions you want to use (This example uses Google Chrome.)

Step one. Go to admin/people/permissions find the permission you would like to use and right click on a check box to the right of the permission you would like to use. Select 'Inspect Element' or just go look at the source.

Example

Next look for the value of the check box and note the value. (In this case the string is 'create coupon content')

enter image description here

This is the string you need to supply as an access argument in hook_menu()

Example Code: (non-relevant items removed, don't forget title, callback, etc. in hook_menu())

function fsrsys_menu() {
    $items = array();
    $items['my-custom-url'] = array(
        'access callback' => 'user_access',
        'access arguments' => array('create coupon content'),
    );
    return $items;
}
9

Here's a D7 version of Haza's answer, modified to use DSM instead of print_r and to leave out modules that don't implement hook_permission:

// Render permission overview:
$options = array();
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
  if ($permissions = module_invoke($module, 'permission')) {
    // List only the modules that have permissions.
    dsm($module);
    dsm($permissions);
  }
}
6

You can use user_permission_get_modules() function, which returns an array of all defined permissions and their modules.

Determine the modules that permissions belong to.

Return value

An associative array in the format $permission => $module.

1
  • 2
    If you wrap it in array_keys() you obviously get only the requested information. You can then also use the result in user_role_grant_permissions, for example to grant the admin role all permissions: user_role_grant_permissions(variable_get("user_admin_role"), array_keys(user_permission_get_modules()));
    – aross
    Commented Apr 28, 2015 at 10:41
4

The easiest way is to check the permission page (D7: admin/people/permissions, D6: admin/user/permissions). Note that you directly use the displayed string in Drupal 6, for 7 you need to have a look at the checkbox name/value. This is because permissions now have a translated title and description in Drupal 7.

So if you want the name for the permission "View published content" in Drupal 7, the value is "access content" which is your permission name.

If you know which module the permission is coming from, you can also check the corresponding hook_perm() (D6) / hook_pemission() (D7) implementation of that module.

In many cases, you want to define your own pemissions and use these, you can do that with the above hooks.

3
  • RE: "The easiest way is to check the permission page (D7: admin/people/permissions, ).". The page shows a list of permission, but they are human readable string rather than machine readable string.
    – gilzero
    Commented Apr 26, 2011 at 8:06
  • 1
    As I wrote below, the machine readable name can be extracted from the value of any of the checkboxes for a given permission.
    – Berdir
    Commented Apr 26, 2011 at 8:27
  • This answer covers Drupal 6 and 7, the accepted one only Drupal 6 - but the accepted answer is a neat trick and could be easily altered for Drupal 7. =)
    – Greg
    Commented Apr 27, 2011 at 13:20
4

Using the Devel Module

  • After installing and enabling the Devel module, navigate to admin/config/development/devel . There is a setting titled, "Display machine names of permissions and modules". Be sure that is checked.

  • Now on the permissions page at people/permissions, hover over the name of the permission you are questioning. It may delay a second or two, but then the string value will show.

3
  • Wow, I didn't know about this. Saw it after I posted my answer. Thanks!
    – Batandwa
    Commented Jun 21, 2014 at 12:47
  • Very useful! TYVM.
    – DrCord
    Commented Aug 4, 2014 at 18:33
  • Sweet, much easier than opening the module code!
    – bloke_zero
    Commented Dec 16, 2016 at 15:29
1

Here is a simple form alter that will add the permission key to the name on the permissions page. That should make it easier to look it up. You will need to add it to a custom module and replace "mymodule" with the name of that module.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_user_admin_permissions_alter(&$form, $form_state) {
  foreach ($form['permission'] as $key => &$item) {
    if (isset($item['#type']) && $item['#type'] == 'item') {
      $item['#markup'] .= ' (' . $key . ')';
    }
  }
}
1

I created this JavaScript snippet that helps me with this. I tested it in Drupal 7 only on the Shiny theme.

It basically outputs the machine name, taken from the first checkbox on that name, on to the first cell, under the label. The script relies on jQuery, which shouldn't be a problem on Drupal 7.

I typically paste it on the Developer Tools console and run it from there. I might create a bookmarklet for it in future.

// Go through each of the rows on the permissin table
rows = jQuery('#permissions tr');
rows.each(function() {
    // Get the first checkbox in the row.
    checkbox = jQuery('.checkbox .form-checkbox', this).first();
    // Get the first cell, containing the permission label.
    nameCell = jQuery('td:first-child', this);
    // In case this is not a permission row
    if(checkbox.length == 0) {
        return;
    }

    // Embed the permission maching name on permission label cell.
    nameCell.append("<div><code>" + checkbox.val() + "</code></div>");
});
2
1
db_query("SELECT DISTINCT {permission} FROM role_permission");
1

This is a simple module to make it easy: Access Arguments List

Regards

0

just drop in some module, you will see machine permissions at /admin/people/permissions

function YOURMODULE_form_user_admin_permissions_alter(&$form, &$form_state) 
{
    $perms = &$form['permission'];

    $group_counter = 0;
    foreach ($perms as $k => &$v) {
        if ($k == $group_counter) { // skip fieldsets
            $group_counter++;
            continue;
        }

        $v['#markup'] .= ' (<span class="description" style="color:brown">'.$k.'</span>)'; 
    }
}

regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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