3

我正在构建一个自定义 joomla 组件,并且正在研究如何在不使用帐户密码的情况下设置登录会话。我已经有了:

$app = &JFactory::getApplication('site');

$result = $app->login(array(

    'username' => 'james',
    'password' => 'password'

));

这显然需要用户密码。我可以访问用户 ID 和用户名,因此可以使用其中任何一个句柄。是否有其他对象或方法我可以用来登录用户,或者是否有一些我可以使用的自定义解决方案,例如手动设置所有必需的 $_SESSION 变量?

在此先感谢,非常感谢任何帮助:)

4

2 回答 2

6

重要提示:是否需要在 // 注册所需的会话变量之后添加会话对象

// Register the needed session variables
$session =& JFactory::getSession();
$session->set('user', $jUser);

太好了,谢谢

于 2013-01-09T15:03:22.997 回答
5
//log user in
if(!JFactory::getUser()->id)
{
    $email = (string)$response['linkedin']->{'email-address'};

    $db = JFactory::getDbo();
    $app = JFactory::getApplication();

    $sql = "SELECT * FROM #__users WHERE email = " . $db->quote($email);
    $db->setQuery($sql);
    $result = $db->loadObject();

    if($result->id)
    {
            $jUser = JFactory::getUser($result->id);
            //$userarray = array();
            //$userarray['username'] = $jUser->username;
            //$userarray['password'] = $jUser->password;
            //$app->login($userarray);              

            $instance = $jUser;     
            $instance->set('guest', 0);

            // Register the needed session variables
            $session =& JFactory::getSession();
            $session->set('user',$jUser);


            // Check to see the the session already exists.                        
            $app->checkSession();

            // Update the user related fields for the Joomla sessions table.
            $db->setQuery(
                    'UPDATE '.$db->quoteName('#__session') .
                    ' SET '.$db->quoteName('guest').' = '.$db->quote($instance->get('guest')).',' .
                    '   '.$db->quoteName('username').' = '.$db->quote($instance->get('username')).',' .
                    '   '.$db->quoteName('userid').' = '.(int) $instance->get('id') .
                    ' WHERE '.$db->quoteName('session_id').' = '.$db->quote($session->getId())
            );
            $db->query();

            // Hit the user last visit field
            $instance->setLastVisit();          

            //return true;

            $app->redirect('index.php?option=com_community&view=profile');
    }
    else
    {

            $url = "index.php?option=com_community&view=register";
            $app->redirect($url,'We did not find your email address in our system. Please register.');
            //echo "redirect to registration page";
            //exit();


            //$url = 'index.php?option=com_users&view=registration&name=' . $user_profile['name'] . '&username=' . $user_profile['username'] . '&email=' . $user_profile['email'];
            //$url = JRoute::_($url);
            //$app->redirect($url);
    }

}
于 2012-09-08T19:31:31.210 回答