Valid access arguments

2019-05-23 21:27发布

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'),
  );

3条回答
做个烂人
2楼-- · 2019-05-23 21:55

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.

查看更多
爷的心禁止访问
3楼-- · 2019-05-23 22:09

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.

查看更多
劫难
4楼-- · 2019-05-23 22:14

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;
}
查看更多
登录 后发表回答