简体   繁体   English

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

[英]set user id as the current user in Joomla

I have a userId - $uid . 我有一个userId- $uid

I wanted to make this user as logged in to the s/m in joomla. 我想让该用户登录到joomla中的s / m。

I've tried this - 我已经试过了-

$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

How can I make the user as the current user by using the uid itself? 如何通过使用uid本身使该用户成为当前用户?

Update : I just found that user can be loaded with JUser::load() 更新 :我刚刚发现可以用JUser :: load()加载用户

Trial 2- 试用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)

As there is wp_set_current_user($uid); 由于有wp_set_current_user($uid); for wordpress user_load_by_name() in drupal, is there any similar function for joomla ? 对于drupal中的wordpress user_load_by_name() ,joomla是否有任何类似的功能? Please help.!!! 请帮忙。!!!

Thanks! 谢谢!

A bit late, but it can help someone in the future. 有点晚了,但是将来可以对某人有所帮助。 In order to set the user for JFactory::getUser(), you need to set it in the session: 为了设置JFactory :: getUser()的用户,您需要在会话中进行设置:

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

I don't have much knowledge about Joomla so I was not able to implement it but here is my idea: 我对Joomla并不了解,所以我无法实现它,但这是我的想法:

I remembered that there was a switch user plugin for joomla. 我记得有一个joomla的切换用户插件。 I searched for it and found this . 我搜索它,发现了这个 It should be possible to port this code to change user by user-id, like you need it. 应该可以移植此代码,以根据需要通过用户ID更改用户。 Noting else they are doing after clicking on the link in admin-cp. 注意在单击admin-cp中的链接后,它们正在执行其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM