Can someone explain “access arguments” in Drupal?

好久不见. 提交于 2019-11-27 14:45:50

问题


Can someone explain "access arguments" in Drupal? Trust me I have tried Googling it but I am just not getting a clear grasp.


回答1:


in /admin/user/permissions you will see lots of access options. they come from drupal modules, and lets the site administrator distribute specific permissions to user roles (drupal provides 'anonymous' and 'registered' roles by default). modules declare them through hook_perm and they are as easy to use as:

function mymodulename_perm {
return array('use custom feature', 'use the other custom feature');
}

and they will show up there, ready to be used. now, in any function of yours, you can check for user access through user_access which is just as easy to use:

if (user_access('use custom feature')) {
  //do something
}



回答2:


Access arguments are the arguments passed to the function that checks if a user has access to a menu.

Given a menu callback definition as the following

  $items['blog/feed'] = array(
    'title' => 'RSS feed',
    'page callback' => 'blog_feed',
    'access callback' => 'custom_module_blog_access',
    'access arguments' => array('feed'),
    'type' => MENU_CALLBACK,
  );

The function custom_module_blog_access() will be called as custom_module_blog_access('feed'). If the function returns TRUE, then the user will be given access to the menu callback; differently, the user will see the error 403 page (access denied). Normally, the access callback is not defined, and by default Drupal will use user_access().




回答3:


For using predefined valid permissions, such as system permissions:

  1. Check the list of valid permissions at: /admin/people/permissions

  1. Copy the permission string and add it to your 'access arguments' array:

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

Reference: Valid access arguments



来源:https://stackoverflow.com/questions/3018986/can-someone-explain-access-arguments-in-drupal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!