Skip to content

Commit

Permalink
chore: Leverage loginUrl helper + add isset*() methods to `UserFe…
Browse files Browse the repository at this point in the history
…edback` class
  • Loading branch information
hellopablo committed Jun 20, 2024
1 parent 6299c0f commit d39334c
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 17 deletions.
23 changes: 7 additions & 16 deletions src/Common/Service/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -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));
}
}

Expand Down
115 changes: 114 additions & 1 deletion src/Common/Service/UserFeedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}

0 comments on commit d39334c

Please sign in to comment.