gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:48:06 26 4
gpt4 key购买 nike

我需要一个插件功能来检查显示的帐户是用户帐户还是另一个帐户。我为此使用此功能:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com