1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Startup; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Control\HTTPResponse; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\Core\Kernel; |
10
|
|
|
use SilverStripe\Security\Permission; |
11
|
|
|
use SilverStripe\Security\Security; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Specialised Director class used by ErrorControlChain to handle error and redirect conditions |
15
|
|
|
* |
16
|
|
|
* @internal This class is experimental API and may change without warning |
17
|
|
|
*/ |
18
|
|
|
class ErrorDirector extends Director |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Redirect with token if allowed, or null if not allowed |
22
|
|
|
* |
23
|
|
|
* @param HTTPRequest $request |
24
|
|
|
* @param ConfirmationTokenChain $confirmationTokenChain |
25
|
|
|
* @param Kernel $kernel |
26
|
|
|
* @return null|HTTPResponse Redirection response, or null if not able to redirect |
27
|
|
|
*/ |
28
|
|
|
public function handleRequestWithTokenChain( |
29
|
|
|
HTTPRequest $request, |
30
|
|
|
ConfirmationTokenChain $confirmationTokenChain, |
31
|
|
|
Kernel $kernel |
32
|
|
|
) { |
33
|
|
|
Injector::inst()->registerService($request, HTTPRequest::class); |
34
|
|
|
|
35
|
|
|
// Next, check if we're in dev mode, or the database doesn't have any security data, or we are admin |
36
|
|
|
$reload = function (HTTPRequest $request) use ($confirmationTokenChain, $kernel) { |
|
|
|
|
37
|
|
|
if ($kernel->getEnvironment() === Kernel::DEV || !Security::database_is_ready() || Permission::check('ADMIN')) { |
38
|
|
|
return $confirmationTokenChain->reloadWithTokens(); |
39
|
|
|
} |
40
|
|
|
return null; |
41
|
|
|
}; |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
return $this->callMiddleware($request, $reload); |
45
|
|
|
} finally { |
46
|
|
|
// Ensure registered request is un-registered |
47
|
|
|
Injector::inst()->unregisterNamedObject(HTTPRequest::class); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.