Passed
Push — master ( 16e851...25995c )
by Daniel
09:34
created

SessionController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
18
19
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
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