2

How can I look up the valid access arguments? I looked in menu_router, but I believe that only gives some of them.

$items['admin/page'] = array(
   'access arguments' => array('access administration pages'),
  );
apaderno
  • 28,547
  • 16
  • 75
  • 90
Chris Muench
  • 17,444
  • 70
  • 209
  • 362

3 Answers3

2

Invoke hook_permission() across all modules:

$permissions = module_invoke_all('permission');

If I remember rightly array_keys($permissions) will then give you a list of valid permission machine names. The labels/descriptions/other settings for each permissions are in each individual array item.

Clive
  • 36,918
  • 8
  • 87
  • 113
  • Is there somewhere I can look in the database? – Chris Muench Oct 25 '11 at 15:10
  • Good question I've never actually looked before! They don't seem to be stored en masse in the db, rather associations between roles and permissions are stored in `role_permission`. My guess is that the permissions admin page calls `module_invoke_all('permission')` every time it's run and rebuilds the `role_permission` table on save. That way all of the permissions never need to be stored and Drupal can infer that a role without a permission entry in the table *doesn't* have access to the page/resource – Clive Oct 25 '11 at 15:17
  • This actually kind of makes sense thinking about it, if all the permissions were stored then Drupal would have to de-duplicate between the list it held in the database and all those provided by modules (which may have changed since the db was last updated) – Clive Oct 25 '11 at 15:19
2

Actually, you are interested to the values of the access arguments where the access callback is "user_access" (the default value); as a module can use a different access callback, the values for the access arguments can theoretically be infinite.

The alternative to invoking all the implementations of hook_permission() is to use code similar to the following one:

$permissions = array();
db_query("SELECT permission FROM {role_permission}");

foreach ($result as $row) {
  $permissions[$row->permission] = TRUE;
}

array_keys($permissions) will then give you the list of all the permissions.

I took the query from user_role_permissions(); the difference is that the function is interested in the permissions associated to the role passed as argument.

apaderno
  • 28,547
  • 16
  • 75
  • 90
0

1- Check a list of valid permissions at: /admin/people/permissions

Drupal 7, List of valid permissions

2- Specify the permission in your menu hook:

function webforms_advanced_router_menu() {

  $items['admin/config/mymodule'] = [
    'title' => 'MyModule',
    'page callback' => 'drupal_get_form',
    'access callback' => '_mymodule_admin_form',
    'access arguments' => array('administer site configuration'),
    'type' => MENU_CALLBACK
  ];

  return $items;
}
Eduardo Chongkan
  • 752
  • 7
  • 12