在 Joomla 中将用户 ID 设置为当前用户


set user id as the current user in Joomla

我有一个用户ID - $uid .

我想让这个用户登录到 joomla 中的 s/m。

我试过这个 -

$currentUser = new JUser($uid);
$mainframe = JFactory::getApplication('site');
$credentials = array();
$credentials['username'] = $currentUser->username;
$credentials['password'] = $currentUser->password; //this is the hashed password
//perform the login action
$mainframe->login($credentials);
$newuser = JFactory::getUser(); //this doesn't get updated to the new user ($uid) details

如何使用 uid 本身使用户成为当前用户?

更新:我刚刚发现用户可以加载JUser::load()

试用 2-

$user = JUser::getInstance($uid);
$user->load($uid); //this should load the new user.
$newuser = JFactory::getUser(); //still i get the anonymous details here 
            //($newuser->id = 0)

由于drupal中有wordpress user_load_by_name()wp_set_current_user($uid);,Joomla是否有类似的功能?请帮忙!!

谢谢!

有点晚了,但它可以帮助将来的人。为了为 JFactory::getUser() 设置用户,您需要在会话中设置它:

JFactory::getSession()->set('user', JFactory::getUser($uid));

我对 Joomla 了解不多,所以我无法实现它,但这是我的想法:

我记得有一个用于joomla的切换用户插件。我搜索了它并找到了这个。应该可以移植此代码以按用户 ID 更改用户,就像您需要的那样。注意他们在单击管理员-cp中的链接后正在执行的其他操作。