Skip to content

Commit

Permalink
Merge pull request #1009 from CakeDC/feature/addUnauthoritzedUrlInfla…
Browse files Browse the repository at this point in the history
…shMessage

add config option to include Unauthorized Url in returned Flash Message
  • Loading branch information
steinkel authored Oct 27, 2023
2 parents 0de2fd0 + 9537acd commit 0ddaba1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Middleware/UnauthorizedHandler/DefaultRedirectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Authorization\Exception\ForbiddenException;
use Authorization\Exception\MissingIdentityException;
use Authorization\Middleware\UnauthorizedHandler\CakeRedirectHandler;
use Cake\Core\Configure;
use Cake\Http\ServerRequest;
use Cake\Http\Session;
use Cake\Routing\Router;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function handle(
$response = parent::handle($exception, $request, $options);
$session = $request->getAttribute('session');
if ($session instanceof Session) {
$options['request'] = $request;
$this->addFlashMessage($session, $options);
}

Expand Down Expand Up @@ -117,11 +119,16 @@ protected function createFlashMessage($options): array
{
$message = (array)($options['flash'] ?? []);

$unauthorizedUrl = '';
if (Configure::read('debug')) {
$unauthorizedUrl = __d('cake_d_c/users', 'Location = ') . (string)$options['request']->getUri();
}

return $message + [
'message' => __d('cake_d_c/users', 'You are not authorized to access that location.'),
'key' => 'flash',
'element' => 'flash/error',
'params' => [],
];
'message' => __d('cake_d_c/users', 'You are not authorized to access that location.') . $unauthorizedUrl,
'key' => 'flash',
'element' => 'flash/error',
'params' => [],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,24 @@ public function loginAsUserId($id)
*/
public function testRedirectToLogin()
{
Configure::write('debug', false);
$this->enableRetainFlashMessages();
$this->get('/pages/home');

$this->assertRedirectContains('/login?redirect=%2Fpages%2Fhome');
$this->assertFlashMessage('You are not authorized to access that location.');
}

public function testRedirectToLoginDebug()
{
Configure::write('debug', true);
$this->enableRetainFlashMessages();
$this->get('/pages/home');

$this->assertRedirectContains('/login?redirect=%2Fpages%2Fhome');
$this->assertFlashMessage('You are not authorized to access that location.Location = http://localhost/pages/home');
}

/**
* Test login action with get request
*
Expand Down

0 comments on commit 0ddaba1

Please sign in to comment.