From d39334c7cefe29ab961fc85daec94cd0696fd9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20de=20la=20Pen=CC=83a?= Date: Thu, 20 Jun 2024 13:34:58 +0100 Subject: [PATCH] chore: Leverage `loginUrl` helper + add `isset*()` methods to `UserFeedback` class --- src/Common/Service/ErrorHandler.php | 23 ++---- src/Common/Service/UserFeedback.php | 115 +++++++++++++++++++++++++++- 2 files changed, 121 insertions(+), 17 deletions(-) diff --git a/src/Common/Service/ErrorHandler.php b/src/Common/Service/ErrorHandler.php index 3676b4b6..29134d79 100644 --- a/src/Common/Service/ErrorHandler.php +++ b/src/Common/Service/ErrorHandler.php @@ -274,12 +274,10 @@ public function show404(bool $bLogError = false) /** * By default we log this, but allow a dev to skip it. Additionally, skip * if it's a HEAD request. - * * Reasoning: I often use HEAD requests to check the existence of a file * in JS before fetching it. I feel that these shouldn't be logged. A * direct GET/POST/etc request to a non-existent file is more likely a * user following a dead link so these _should_ be logged. - * * If you disagree, open up an issue and we'll work something out. */ @@ -366,25 +364,18 @@ public function show401( /** @var Input $oInput */ $oInput = Factory::service('Input'); - if (is_null($sFlashMessage)) { - $sFlashMessage = 'Sorry, you need to be logged in to see that page.'; + if (!is_null($sFlashMessage)) { + $oUserFeedback->error($sFlashMessage); + } elseif (!$oUserFeedback->issetError()) { + $oUserFeedback->error('Sorry, you need to be logged in to see that page.'); } - $oUserFeedback->error($sFlashMessage); - if (is_null($sReturnUrl)) { - if ($oInput->server('REQUEST_URI')) { - $sReturnUrl = $oInput->server('REQUEST_URI'); - } elseif (uri_string()) { - $sReturnUrl = uri_string(); - } else { - $sReturnUrl = ''; - } + // Allow helper to determine what he returnTo should be + $sReturnUrl = ''; } - $sReturnUrl = $sReturnUrl ? '?return_to=' . urlencode($sReturnUrl) : ''; - - redirect('auth/login' . $sReturnUrl); + redirect(loginUrl($sReturnUrl)); } } diff --git a/src/Common/Service/UserFeedback.php b/src/Common/Service/UserFeedback.php index 2d99e875..c8b8e5a1 100644 --- a/src/Common/Service/UserFeedback.php +++ b/src/Common/Service/UserFeedback.php @@ -8,7 +8,6 @@ * @category Library * @author Nails Dev Team * @link - * * @todo Add "actions" to messages (i.e. buttons) * @todo Match the naming convention of Bootstrap Alerts */ @@ -103,6 +102,20 @@ public function set(string $sType, string $sMessage): self // -------------------------------------------------------------------------- + /** + * Detect whether a specific feedback message has been set + * + * @param string $sType + * + * @return bool + */ + public function isset(string $sType): bool + { + return isset($this->aMessages[$sType]); + } + + // -------------------------------------------------------------------------- + /** * Validates a given type * @@ -265,6 +278,18 @@ public function getSuccess(): Message // -------------------------------------------------------------------------- + /** + * Whether the "success" feedback message is set + * + * @return bool + */ + public function issetSuccess(): bool + { + return $this->isset(static::TYPE_SUCCESS); + } + + // -------------------------------------------------------------------------- + /** * Set a "error" feedback message * @@ -293,6 +318,18 @@ public function getError(): Message // -------------------------------------------------------------------------- + /** + * Whether the "error" feedback message is set + * + * @return bool + */ + public function issetError(): bool + { + return $this->isset(static::TYPE_ERROR); + } + + // -------------------------------------------------------------------------- + /** * Set a "warning" feedback message * @@ -321,6 +358,18 @@ public function getWarning(): Message // -------------------------------------------------------------------------- + /** + * Whether the "warning" feedback message is set + * + * @return bool + */ + public function issetWarning(): bool + { + return $this->isset(static::TYPE_WARNING); + } + + // -------------------------------------------------------------------------- + /** * Set a "info" feedback message * @@ -349,6 +398,18 @@ public function getInfo(): Message // -------------------------------------------------------------------------- + /** + * Whether the "info" feedback message is set + * + * @return bool + */ + public function issetInfo(): bool + { + return $this->isset(static::TYPE_INFO); + } + + // -------------------------------------------------------------------------- + /** * Set a "positive" feedback message * @@ -379,6 +440,19 @@ public function getPositive(): Message // -------------------------------------------------------------------------- + /** + * Whether the "positive" feedback message is set + * + * @return bool + * @deprecated + */ + public function issetPositive(): bool + { + return $this->isset(static::TYPE_POSITIVE); + } + + // -------------------------------------------------------------------------- + /** * Set a "negative" feedback message * @@ -409,6 +483,19 @@ public function getNegative(): Message // -------------------------------------------------------------------------- + /** + * Whether the "negative" feedback message is set + * + * @return bool + * @deprecated + */ + public function issetNegative(): bool + { + return $this->isset(static::TYPE_NEGATIVE); + } + + // -------------------------------------------------------------------------- + /** * Set a "message" feedback message * @@ -439,6 +526,19 @@ public function getMessage(): Message // -------------------------------------------------------------------------- + /** + * Whether the "message" feedback message is set + * + * @return bool + * @deprecated + */ + public function issetMessage(): bool + { + return $this->isset(static::TYPE_MESSAGE); + } + + // -------------------------------------------------------------------------- + /** * Set a "notice" feedback message * @@ -466,4 +566,17 @@ public function getNotice(): Message { return $this->get(static::TYPE_NOTICE); } + + // -------------------------------------------------------------------------- + + /** + * Whether the "notice" feedback message is set + * + * @return bool + * @deprecated + */ + public function issetNotice(): bool + { + return $this->isset(static::TYPE_NOTICE); + } }