XF 2.0 Start a XF session from PHP Application just with the user_id

Adam44

New member
Hello,
I'm searching a good way to connect the users through a PHP App .

1/ When a user logs in my application i create a cookie [user_connected]
2/ When a user logged into my site and goes to the forum , i verify if the cookie [user_connected] exists and if he is disconnected of xf, if it's true i redirect to my PHP application

XF.php
Code:
 public static function runApp($appClass)
    {
        $app = self::setupApp($appClass);
        
        ob_start();

        $response = $app->run();
        
        $redirect_url = 'http://127.0.0.1/PHPAPP/start_xf_session';

        if(isset($_COOKIE['user_connected']) && empty(self::visitor()->user_id))
        {
            $response->redirect($redirect_url)->send($app->request());
        }...

3/ In my PHP Application Based on the user ID he is using i connect the user to his account (I can create an account for him if he doesn't have one yet)
In the forum i have look this :
Code:
$ip = $XF->request->getIp();
$service = $XF->service('XF:User\Login', $input, $ip);
$user = $service->validate($password, $error);
//completeLogin
$XF->session->changeUser($user);
XF::setVisitor($user);
$XF->repository('XF:SessionActivity')->clearUserActivity(0, $ip);
$XF->repository('XF:Ip')->logIp(
  $user->user_id, $ip,
  'user', $user->user_id, 'login'
);
//createVisitorRememberKey
$rememberRepo = $XF->repository('XF:UserRemember');
$key = $rememberRepo->createRememberRecord($user->user_id);
$value = $rememberRepo->getCookieValue($user->user_id, $key);
//$XF->response()->setCookie('user', $value, 365 * 86400); // Uh, didn't work...
setcookie('xf_user', $value, time() + (365 * 86400), '/'); // ugly

This code is working fine but there is one problem ... If i have a high traffic site it's no a good idea to at each login to verify user password (Can causes mysql issues) and it's useless

Code:
$user = $service->validate($password, $error);

So i just retrieve the user object from the user id without verify the password

Code:
$service = $XF->service('XF:User\Login');

$user = $service->getUserFromID(2);

$XF->session->changeUser($user);
....


For it works i have edit the XF\Service\User Login.php class

Code:
public function __construct(\XF\App $app, $login, $ip)
public function __construct(\XF\App $app, $login = null, $ip = null)

and add this method

Code:
    public function getUserFromID($userID)
    {
        return $this->findOne('XF:User', ['user_id' => $userID], ['Auth']);
    }

Do you have any comments or suggestions?
PS : Sorry for my bad english
 
Back
Top Bottom