|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ControllerPolicy; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Middleware\HTTPMiddleware; |
|
6
|
|
|
use SilverStripe\Core\Extension; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This extension will register the policy with the middleware system to be run at process() stage |
|
10
|
|
|
* of the middleware control pipeline. This is done with the help of the ControllerPolicyMiddleware. |
|
11
|
|
|
* |
|
12
|
|
|
* This will override any specific headers that have been set by the default HTTP::add_cache_headers, which is |
|
13
|
|
|
* actually what we want. The policies are applied in the order they are added, so if there are two added the |
|
14
|
|
|
* latter will override the former. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ControllerPolicyApplicator extends Extension |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var HTTPMiddleware |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $middleware; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $policies = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param HTTPMiddleware $middleware |
|
30
|
|
|
*/ |
|
31
|
|
|
public function setMiddleware(HTTPMiddleware $middleware) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->middleware = $middleware; |
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return HTTPMiddleware |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getMiddleware() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->middleware; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set the policies for this controller. Will set, not add to the list. |
|
47
|
|
|
* |
|
48
|
|
|
* @param mixed $policies |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setPolicies($policies) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!is_array($policies)) { |
|
53
|
|
|
$policies = [$policies]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->policies = $policies; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the policies for this controller |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getPolicies() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->policies; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Register the requested policies with the global request filter. This doesn't mean the policies will be |
|
71
|
|
|
* executed at this point - it will rather be delayed until the Director::callMiddleware runs. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function onAfterInit() |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$this->getPolicies()) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Flip the policy array, so the first element in the array is the one applying last. |
|
80
|
|
|
// This is needed so the policies on inheriting Controllers are in the intuitive order: |
|
81
|
|
|
// the more specific overrides the less specific. |
|
82
|
|
|
$policies = array_reverse($this->getPolicies()); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($policies as $policy) { |
|
85
|
|
|
$this->getMiddleware()->requestPolicy($this->owner, $policy); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|