|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ControllerPolicy; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
|
6
|
|
|
use SilverStripe\Forms\LiteralField; |
|
7
|
|
|
use SilverStripe\Forms\TextField; |
|
8
|
|
|
use SilverStripe\ORM\DataExtension; |
|
9
|
|
|
use SilverStripe\Security\Permission; |
|
10
|
|
|
use SilverStripe\Security\Security; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This extension leverages the CachingPolicy's ability to customise the max-age per originator. |
|
14
|
|
|
* The configuration option is surfaced to the CMS UI. The extension needs to be added |
|
15
|
|
|
* to the object related to the policed controller. |
|
16
|
|
|
*/ |
|
17
|
|
|
class PageControlledPolicy extends DataExtension |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $db = [ |
|
|
|
|
|
|
23
|
|
|
'MaxAge' => 'Varchar' |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Extension point for the CachingPolicy. |
|
28
|
|
|
* |
|
29
|
|
|
* @param int $cacheAge The original cache age value (in seconds) |
|
30
|
|
|
* @return int|null The new cache age value (in seconds) |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getCacheAge($cacheAge) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->owner->MaxAge != '') { |
|
35
|
|
|
return (int)($this->owner->MaxAge * 60); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param FieldList $fields |
|
41
|
|
|
*/ |
|
42
|
|
|
public function updateCMSFields(FieldList $fields) |
|
43
|
|
|
{ |
|
44
|
|
|
// Only admins are allowed to modify this. |
|
45
|
|
|
$member = Security::getCurrentUser(); |
|
46
|
|
|
if (!$member || !Permission::checkMember($member, 'ADMIN')) { |
|
|
|
|
|
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$fields->addFieldsToTab('Root.Caching', [ |
|
51
|
|
|
LiteralField::create('Instruction', '<p>The following field controls the length of time the page will ' . |
|
52
|
|
|
'be cached for. You will not be able to see updates to this page for at most the specified ' . |
|
53
|
|
|
'amount of minutes. Leave empty to set back to the default configured for your site. Set ' . |
|
54
|
|
|
'to 0 to explicitly disable caching for this page.</p>'), |
|
55
|
|
|
TextField::create('MaxAge', 'Custom cache timeout [minutes]') |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|