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

时间:2016-02-22 12:42:45

标签: php joomla

我有一个userId - $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中有wp_set_current_user($uid); wordpress user_load_by_name(),joomla是否有类似的功能?请帮忙。!!!

谢谢!

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我对Joomla知之甚少,所以我无法实现它,但这是我的想法:

我记得joomla有一个切换用户插件。我搜索了它,发现了this。应该可以移植此代码以按用户ID更改用户,就像您需要它一样。注意到他们在点击admin-cp。

中的链接后正在做的事情