Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Drupal对自定义用户配置文件页面的访问控制_Php_Drupal_Drupal 7 - Fatal编程技术网

Php Drupal对自定义用户配置文件页面的访问控制

Php Drupal对自定义用户配置文件页面的访问控制,php,drupal,drupal-7,Php,Drupal,Drupal 7,我正在尝试向Drupal 7用户配置文件添加一个新选项卡。 不幸的是,我找不到正确的访问参数来允许用户查看页面,但通过更改url中的用户ID来限制他查看其他用户的页面 目前管理员可以访问它,但注册用户不能 这是当前代码: $items['user/%user/apples'] = array( 'title' => t('My apples'), 'type' => MENU_LOCAL_TASK, 'description' => t('Configu

我正在尝试向Drupal 7用户配置文件添加一个新选项卡。 不幸的是,我找不到正确的访问参数来允许用户查看页面,但通过更改url中的用户ID来限制他查看其他用户的页面

目前管理员可以访问它,但注册用户不能

这是当前代码:

$items['user/%user/apples'] = array(
    'title' => t('My apples'),
    'type' => MENU_LOCAL_TASK,
    'description' => t('Configure your apples'),
    'access arguments' => array(
      'administer site configuration'
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mysite_apples_config_page'
    ),
    'file' => 'mysite.apples.inc'
);

在哪里可以找到示例?

我认为实现这一点的唯一方法是编写自定义
访问回调
逻辑

在这个回调中,您将检查用户是否与他试图查看的页面具有相同的uid。如果是,请允许他访问;否则,阻止他

function my_custom_access_callback($account) {

    global $user;

    // allow admin users
    if(user_access("administer site configuration")) {
        return TRUE;
    }

    // allow the user to view his own page
    if($user->uid == $account->uid) {
        return TRUE;
    }

    // disallow the rest
    return FALSE;
}
在hook_菜单中,使用新的访问回调:

$items['user/%user/apples'] = array(
    'title' => t('My apples'),
    'type' => MENU_LOCAL_TASK,
    'description' => t('Configure your apples'),
    'access callback' => 'my_custom_access_callback', // use the new callback.
    'access arguments' => array(1), // use %user as the callback argument.
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mysite_apples_config_page'
    ),
    'file' => 'mysite.apples.inc'
);

嗨,这正是我在此期间所做的。谢谢;)有兴趣的人士请参考: