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 @@ + +namespace Exceptions; + +class InvalidLoginException extends AppException{ + protected $message = 'We couldn’t validate your login information.'; +} diff --git a/lib/Exceptions/InvalidUserException.php b/lib/Exceptions/InvalidUserException.php index 36818da3..b270a3ba 100644 --- a/lib/Exceptions/InvalidUserException.php +++ b/lib/Exceptions/InvalidUserException.php @@ -2,5 +2,5 @@ namespace Exceptions; class InvalidUserException extends AppException{ - protected $message = 'We couldn’t validate your login information.'; + protected $message = 'We couldn’t locate that user.'; } diff --git a/lib/Session.php b/lib/Session.php index 22706b08..e147ea84 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -1,4 +1,6 @@ + +use Exceptions\InvalidLoginException; use Ramsey\Uuid\Uuid; use Safe\DateTime; use function Safe\strtotime; @@ -33,33 +35,38 @@ class Session extends PropertiesBase{ // ******* public function Create(?string $email = null, ?string $password = null): void{ - $this->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){ if($isAdminView){ ?>