From 1c3640fab60e8e2c9de5c3849d08c57e01d9c70c Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Mon, 8 Jan 2024 20:21:42 -0600 Subject: [PATCH] More type hinting --- lib/Artwork.php | 18 ++++++-- lib/Exceptions/InvalidLoginException.php | 6 +++ lib/Exceptions/InvalidUserException.php | 2 +- lib/Session.php | 53 ++++++++++++++---------- lib/User.php | 4 ++ www/artworks/get.php | 2 +- 6 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 lib/Exceptions/InvalidLoginException.php diff --git a/lib/Artwork.php b/lib/Artwork.php index 7cb71a4a..9117e11c 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -72,17 +72,27 @@ class Artwork extends PropertiesBase{ return $this->_UrlName; } - protected function GetSubmitter(): User{ + protected function GetSubmitter(): ?User{ if($this->_Submitter === null){ - $this->_Submitter = User::Get($this->SubmitterUserId); + try{ + $this->_Submitter = User::Get($this->SubmitterUserId); + } + catch(Exceptions\InvalidUserException){ + // Return null + } } return $this->_Submitter; } - protected function GetReviewer(): User{ + protected function GetReviewer(): ?User{ if($this->_Reviewer === null){ - $this->_Reviewer = User::Get($this->ReviewerUserId); + try{ + $this->_Reviewer = User::Get($this->ReviewerUserId); + } + catch(Exceptions\InvalidUserException){ + // Return null + } } return $this->_Reviewer; diff --git a/lib/Exceptions/InvalidLoginException.php b/lib/Exceptions/InvalidLoginException.php new file mode 100644 index 00000000..cbc758f8 --- /dev/null +++ b/lib/Exceptions/InvalidLoginException.php @@ -0,0 +1,6 @@ +User = User::GetIfRegistered($email, $password); - $this->UserId = $this->User->UserId; + try{ + $this->User = User::GetIfRegistered($email, $password); + $this->UserId = $this->User->UserId; - $existingSessions = Db::Query(' - SELECT SessionId, - Created - from Sessions - where UserId = ? - ', [$this->UserId]); + $existingSessions = Db::Query(' + SELECT SessionId, + Created + from Sessions + where UserId = ? + ', [$this->UserId]); - if(sizeof($existingSessions) > 0){ - $this->SessionId = $existingSessions[0]->SessionId; - $this->Created = $existingSessions[0]->Created; + if(sizeof($existingSessions) > 0){ + $this->SessionId = $existingSessions[0]->SessionId; + $this->Created = $existingSessions[0]->Created; + } + else{ + $uuid = Uuid::uuid4(); + $this->SessionId = $uuid->toString(); + $this->Created = new DateTime(); + Db::Query(' + INSERT into Sessions (UserId, SessionId, Created) + values (?, + ?, + ?) + ', [$this->UserId, $this->SessionId, $this->Created]); + } + + self::SetSessionCookie($this->SessionId); } - else{ - $uuid = Uuid::uuid4(); - $this->SessionId = $uuid->toString(); - $this->Created = new DateTime(); - Db::Query(' - INSERT into Sessions (UserId, SessionId, Created) - values (?, - ?, - ?) - ', [$this->UserId, $this->SessionId, $this->Created]); + catch(Exceptions\InvalidUserException){ + throw new InvalidLoginException(); } - - self::SetSessionCookie($this->SessionId); } public static function GetLoggedInUser(): ?User{ diff --git a/lib/User.php b/lib/User.php index dfba00f8..93046b26 100644 --- a/lib/User.php +++ b/lib/User.php @@ -114,6 +114,10 @@ class User extends PropertiesBase{ // *********** public static function Get(?int $userId): User{ + if($userId === null){ + throw new Exceptions\InvalidUserException(); + } + $result = Db::Query(' SELECT * from Users diff --git a/www/artworks/get.php b/www/artworks/get.php index e1e91318..90c7f900 100644 --- a/www/artworks/get.php +++ b/www/artworks/get.php @@ -82,7 +82,7 @@ catch(Exceptions\ArtworkNotFoundException){ Submitted by - SubmitterUserId === null){ ?>AnonymousSubmitter->Name !== null){ ?> Submitter->Name) ?>Submitter->Email) ?> + Submitter === null){ ?>AnonymousSubmitter->Name !== null){ ?> Submitter->Name) ?>Submitter->Email) ?> Reviewer !== null){ ?>