-
Notifications
You must be signed in to change notification settings - Fork 294
/
DefaultRedirectHandler.php
118 lines (105 loc) · 3.45 KB
/
DefaultRedirectHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2020, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Middleware\UnauthorizedHandler;
use Authorization\Exception\Exception;
use Authorization\Exception\ForbiddenException;
use Authorization\Exception\MissingIdentityException;
use Authorization\Middleware\UnauthorizedHandler\CakeRedirectHandler;
use Cake\Http\ServerRequest;
use Cake\Http\Session;
use Cake\Routing\Router;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* This handler will redirect the response if one of configured exception classes is encountered.
*
* CakePHP Router compatible array URL syntax is supported.
*/
class DefaultRedirectHandler extends CakeRedirectHandler
{
/**
* @inheritDoc
*/
protected $defaultOptions = [
'exceptions' => [
'MissingIdentityException' => MissingIdentityException::class,
'ForbiddenException' => ForbiddenException::class,
],
'url' => [
'plugin' => 'CakeDC/Users',
'controller' => 'Users',
'action' => 'login',
],
'queryParam' => 'redirect',
'statusCode' => 302,
'flash' => [],
];
/**
* @inheritDoc
*/
public function handle(Exception $exception, ServerRequestInterface $request, array $options = []): ResponseInterface
{
$options += $this->defaultOptions;
$response = parent::handle($exception, $request, $options);
$session = $request->getAttribute('session');
if ($session instanceof Session) {
$this->addFlashMessage($session, $options);
}
return $response;
}
/**
* @inheritDoc
*/
protected function getUrl(ServerRequestInterface $request, array $options): string
{
$url = $options['url'];
if (is_callable($url)) {
return $url($request, $options);
}
if ($request->getAttribute('identity') && $request instanceof ServerRequest) {
return $request->referer() ?? '/';
}
if ($options['queryParam'] !== null) {
$url['?'][$options['queryParam']] = (string)$request->getUri();
}
return Router::url($url);
}
/**
* Add a flash message informing location is not authorized.
*
* @param \Cake\Http\Session $session The CakePHP session.
* @param array $options Defined options.
* @return void
*/
protected function addFlashMessage(Session $session, $options): void
{
$messages = (array)$session->read('Flash.flash');
$messages[] = $this->createFlashMessage($options);
$session->write('Flash.flash', $messages);
}
/**
* Create a flash message data.
*
* @param array $options Handler options
* @return array
*/
protected function createFlashMessage($options): array
{
$message = (array)($options['flash'] ?? []);
return $message + [
'message' => __d('cake_d_c/users', 'You are not authorized to access that location.'),
'key' => 'flash',
'element' => 'flash/error',
'params' => [],
];
}
}