mirror of
https://github.com/standardebooks/web.git
synced 2025-07-14 02:21:55 -04:00
Switch logged in user to static typed variable instead of in $GLOBALS
This commit is contained in:
parent
acb30b897c
commit
1449148989
25 changed files with 88 additions and 91 deletions
|
@ -3,17 +3,17 @@ use Ramsey\Uuid\Uuid;
|
|||
use Safe\DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* @property User $User
|
||||
* @property string $Url
|
||||
*/
|
||||
class Session{
|
||||
use Traits\Accessor;
|
||||
|
||||
public static ?User $User = null;
|
||||
|
||||
public int $UserId;
|
||||
public DateTimeImmutable $Created;
|
||||
public string $SessionId;
|
||||
|
||||
protected User $_User;
|
||||
public string $_Url;
|
||||
|
||||
|
||||
|
@ -42,8 +42,8 @@ class Session{
|
|||
*/
|
||||
public function Create(?string $identifier = null, ?string $password = null): void{
|
||||
try{
|
||||
$this->User = User::GetIfRegistered($identifier, $password);
|
||||
$this->UserId = $this->User->UserId;
|
||||
Session::$User = User::GetIfRegistered($identifier, $password);
|
||||
$this->UserId = Session::$User->UserId;
|
||||
|
||||
$existingSessions = Db::Query('
|
||||
SELECT SessionId,
|
||||
|
@ -76,26 +76,6 @@ class Session{
|
|||
}
|
||||
}
|
||||
|
||||
public static function GetLoggedInUser(): ?User{
|
||||
$sessionId = HttpInput::Str(COOKIE, 'sessionid');
|
||||
|
||||
if($sessionId !== null){
|
||||
$result = Db::Query('
|
||||
SELECT u.*
|
||||
from Users u
|
||||
inner join Sessions s using (UserId)
|
||||
where s.SessionId = ?
|
||||
', [$sessionId], User::class);
|
||||
|
||||
if(sizeof($result) > 0){
|
||||
self::SetSessionCookie($sessionId);
|
||||
return $result[0];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function SetSessionCookie(string $sessionId): void{
|
||||
/** @throws void */
|
||||
setcookie('sessionid', $sessionId, ['expires' => intval((new DateTimeImmutable('+1 week'))->format(Enums\DateTimeFormat::UnixTimestamp->value)), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); // Expires in two weeks
|
||||
|
@ -122,4 +102,22 @@ class Session{
|
|||
|
||||
return $result[0] ?? throw new Exceptions\SessionNotFoundException();
|
||||
}
|
||||
|
||||
public static function InitializeFromCookie(): void{
|
||||
$sessionId = HttpInput::Str(COOKIE, 'sessionid');
|
||||
|
||||
if($sessionId !== null){
|
||||
$result = Db::Query('
|
||||
SELECT u.*
|
||||
from Users u
|
||||
inner join Sessions s using (UserId)
|
||||
where s.SessionId = ?
|
||||
', [$sessionId], User::class);
|
||||
|
||||
if(sizeof($result) > 0){
|
||||
self::SetSessionCookie($sessionId);
|
||||
Session::$User = $result[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue