|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Auth
|
|
|
|
|
{
|
|
|
|
|
private static $driver;
|
|
|
|
|
|
|
|
|
|
private static $user;
|
|
|
|
|
|
|
|
|
|
private static $userResolved = false;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function getDriver()
|
|
|
|
|
{
|
|
|
|
|
if (self::$driver === null) {
|
|
|
|
|
self::$driver = Factory::createAuth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$driver;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function login($uid, $time)
|
|
|
|
|
{
|
|
|
|
|
self::forgetUser();
|
|
|
|
|
try {
|
|
|
|
|
self::getDriver()->login($uid, $time);
|
|
|
|
|
} finally {
|
|
|
|
|
// Authentication cookies or Redis session identifiers changed.
|
|
|
|
|
// Never return a user resolved against the previous identity.
|
|
|
|
|
self::forgetUser();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getUser()
|
|
|
|
|
{
|
|
|
|
|
if (!self::$userResolved) {
|
|
|
|
|
$user = self::getDriver()->getUser();
|
|
|
|
|
self::$user = $user;
|
|
|
|
|
self::$userResolved = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Forget only the resolved identity while retaining the request-scoped
|
|
|
|
|
* driver and its Redis connection.
|
|
|
|
|
*/
|
|
|
|
|
public static function forgetUser()
|
|
|
|
|
{
|
|
|
|
|
self::$user = null;
|
|
|
|
|
self::$userResolved = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Explicit lifecycle hook for long-running commands and isolated tests.
|
|
|
|
|
* Normal PHP HTTP requests reset static state automatically.
|
|
|
|
|
*/
|
|
|
|
|
public static function resetRequestCache()
|
|
|
|
|
{
|
|
|
|
|
self::forgetUser();
|
|
|
|
|
self::$driver = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function logout()
|
|
|
|
|
{
|
|
|
|
|
self::forgetUser();
|
|
|
|
|
try {
|
|
|
|
|
\App\Services\Cloud\CloudSessionService::revokeCurrent();
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
self::getDriver()->logout();
|
|
|
|
|
} finally {
|
|
|
|
|
self::forgetUser();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|