OCA\Richdocuments\Controller\DocumentController::loginUser PHP Method

loginUser() private method

This function should only be used from public controller methods where no existing session exists, for example, when loolwsd is directly calling a public method with its own access token. After validating the access token, and retrieving the correct user with help of access token, it can be set as current user with help of this method.
private loginUser ( string $userid )
$userid string
    private function loginUser($userid)
    {
        $users = \OC::$server->getUserManager()->search($userid, 1, 0);
        if (count($users) > 0) {
            $user = array_shift($users);
            if (strcasecmp($user->getUID(), $userid) === 0) {
                // clear the existing sessions, if any
                \OC::$server->getSession()->close();
                // initialize a dummy memory session
                $session = new \OC\Session\Memory('');
                // wrap it
                $cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
                $session = $cryptoWrapper->wrapSession($session);
                // set our session
                \OC::$server->setSession($session);
                \OC::$server->getUserSession()->setUser($user);
            }
        }
    }