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

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;
}

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);
  }
}

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);
  }
}

Tags:

Users