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

Drupal access control to custom user profile page

我正在尝试向 Drupal 7 用户配置文件添加新选项卡。 不幸的是,我找不到正确的访问参数让用户查看页面,而是通过更改 url.

中的 userid 来限制他查看其他用户的页面

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

这是当前代码:

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

在哪里可以找到示例?

我认为可以实现的唯一方法是编写自定义 access callback 逻辑。

在此回调中,您将检查用户是否与他试图查看的页面具有相同的 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_menu 中使用新的访问回调:

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