drupal - 检查显示的帐户是否是用户帐户

标签 drupal drupal-6 drupal-modules

我需要一个插件函数来检查显示的帐户是用户的帐户还是其他帐户的帐户。我正在使用这个函数:

global $user; $account;
$account = user_load(array('uid' => arg(1)));
if ( $user->uid == $account->uid ) {

} 

我正在模块内执行此操作,但它不起作用。当我转到我的个人资料时,我从未看到该函数的输出。

为什么?

编辑

此代码的原始上下文:

function tpzclassified_menu() { 
  global $user; 
  $account = user_load(array('uid' => 1)); 
  $account = user_load(array('uid' => 1)); 

  if ($user->uid == $account1->uid) { 
    $items['user/%user/classifieds'] = array(
      'title' => 'Meine Kleinanzeigen', 
      'type' => MENU_LOCAL_TASK, 
      'page callback' => 'tpzclassified_user_page', 
      'page arguments' => array(1), 
      'access callback' => 'user_view_access', 
      'access arguments' => array(1), 
      'weight' => 4, 
    ); 
  } 

  return $items; 
} 

最佳答案

I need a function for a plugin to check, if the shown account is the user's account or that from another one.

报告的代码正在做的是验证登录用户是否是 Drupal super 用户(也称为用户#1)。如果这是您真正需要的,则无需调用 user_load() 来加载该用户帐户的用户对象。使用以下代码就足够了:

global $user; 

if ($user->uid == 1) { 
  $items['user/%user/classifieds'] = array(
    'title' => 'Meine Kleinanzeigen', 
    'type' => MENU_LOCAL_TASK, 
    'page callback' => 'tpzclassified_user_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1), 
    'weight' => 4, 
  ); 
} 

return $items; 

在 Drupal 中,不存在具有相同用户 ID 的两个用户,1 是 Drupal super 用户的用户 ID(也称为用户 #1,因为其用户 ID 为 1)。
这个解决方案的问题是,从 Drupal 6 开始,菜单回调被缓存;有条件地添加菜单回调不会产生任何效果,因为只有在安装新模块或更新模块(并调用 update.php)时才会清除菜单缓存。强制 Drupal 清除菜单缓存的唯一方法是使用以下代码:

if (!variable_get('menu_rebuild_needed', FALSE)) {
  variable_set('menu_rebuild_needed', TRUE);
}

如果您想检查当前登录的用户是否正在访问自己的帐户,您可以使用以下代码:

function tpzclassified_menu() { 
  $items['user/%user/classifieds'] = array( 
    'title' => 'Meine Kleinanzeigen', 
    'type' => MENU_LOCAL_TASK, 
    'page callback' => 'tpzclassified_user_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1), 
    'weight' => 4, 
  );

  return $items;  
} 

没有理由使用自定义函数,如 user_view_access()已经检查当前用户是否正在查看自己的帐户。

function user_view_access($account) {
  return $account && $account->uid &&
    (
      // Always let users view their own profile.
      ($GLOBALS['user']->uid == $account->uid) ||
      // Administrators can view all accounts.
      user_access('administer users') ||
      // The user is not blocked and logged in at least once.
      ($account->access && $account->status && user_access('access user profiles'))
  );
}

关于drupal - 检查显示的帐户是否是用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3505911/

相关文章:

Drupal 7 Views - 如何在自定义模板中访问未格式化的 $row 变量?

drupal - 如何使用drupal7中的推送通知模块?以及如何存储设备 token ?

drupal - 显示区 block 中的节点信息

Drupal, View : using AJAX to load the complete node?

Drupal 仅根据位置显示某些内容

javascript - 如何自动调整此 HTML 菜单的大小以适合父级宽度?

javascript - 如何在 Drupal 7 中使用 jQuery Update 中的 jQuery UI

javascript - 添加 JavaScript 文件

drupal-7 - entity_extract_ids() 中的 EntityMalformedException : Missing bundle property on entity of type student.

php - 使用 Drupal 的授权,来自非 Drupal 的 php 应用程序