|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests\HTTPCacheControlIntegrationTest; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
|
6
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
|
7
|
|
|
use SilverStripe\Dev\TestOnly; |
|
8
|
|
|
use SilverStripe\Forms\FieldList; |
|
9
|
|
|
use SilverStripe\Forms\Form; |
|
10
|
|
|
use SilverStripe\Forms\FormAction; |
|
11
|
|
|
use SilverStripe\Forms\TextField; |
|
12
|
|
|
use SilverStripe\Security\Permission; |
|
13
|
|
|
use SilverStripe\Security\SecurityToken; |
|
14
|
|
|
|
|
15
|
|
|
class SessionController extends Controller implements TestOnly |
|
16
|
|
|
{ |
|
17
|
|
|
private static $url_segment = 'HTTPCacheControlIntegrationTest_SessionController'; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
20
|
|
|
'showform', |
|
21
|
|
|
'privateaction', |
|
22
|
|
|
'publicaction', |
|
23
|
|
|
'showpublicform', |
|
24
|
|
|
'Form', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
protected function init() |
|
28
|
|
|
{ |
|
29
|
|
|
parent::init(); |
|
30
|
|
|
// Prefer public by default |
|
31
|
|
|
HTTPCacheControlMiddleware::singleton()->publicCache(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getContent() |
|
35
|
|
|
{ |
|
36
|
|
|
return '<p>Hello world</p>'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function showform() |
|
40
|
|
|
{ |
|
41
|
|
|
// Form should be set to private due to CSRF |
|
42
|
|
|
SecurityToken::enable(); |
|
43
|
|
|
return $this->renderWith('BlankPage'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function showpublicform() |
|
47
|
|
|
{ |
|
48
|
|
|
// Public form doesn't use CSRF and thus no session usage |
|
49
|
|
|
SecurityToken::disable(); |
|
50
|
|
|
return $this->renderWith('BlankPage'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
* @throws \SilverStripe\Control\HTTPResponse_Exception |
|
56
|
|
|
*/ |
|
57
|
|
|
public function privateaction() |
|
58
|
|
|
{ |
|
59
|
|
|
if (!Permission::check('ANYCODE')) { |
|
60
|
|
|
$this->httpError(403, 'Not allowed'); |
|
61
|
|
|
} |
|
62
|
|
|
return 'ok'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function publicaction() |
|
66
|
|
|
{ |
|
67
|
|
|
return 'Hello!'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function Form() |
|
71
|
|
|
{ |
|
72
|
|
|
$form = new Form( |
|
73
|
|
|
$this, |
|
74
|
|
|
'Form', |
|
75
|
|
|
new FieldList(new TextField('Name')), |
|
76
|
|
|
new FieldList(new FormAction('submit', 'Submit')) |
|
77
|
|
|
); |
|
78
|
|
|
$form->setFormMethod('GET'); |
|
79
|
|
|
return $form; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|