问题
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'),
);
回答1:
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.
回答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.
回答3:
1- Check a list of valid permissions at: /admin/people/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;
}
来源:https://stackoverflow.com/questions/7891054/valid-access-arguments