Completed
Push — master ( 7a48ac...3c190c )
by Robbie
13:00
created

SAMLLoginForm::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 8
nop 3
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Authenticators;
4
5
use SilverStripe\Control\RequestHandler;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\HiddenField;
8
use SilverStripe\Forms\FormAction;
9
use SilverStripe\Security\LoginForm;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * Class SAMLLoginForm
14
 *
15
 * This not very interesting in itself. It's pretty much boiler-plate code to access the authenticator.
16
 *
17
 * @package activedirectory
18
 */
19
class SAMLLoginForm extends LoginForm
20
{
21
    /**
22
     * This field is used in the "You are logged in as %s" message
23
     * @var string
24
     */
25
    public $loggedInAsField = 'FirstName';
26
27
    /**
28
     * @var string
29
     */
30
    protected $authenticator_class = SAMLAuthenticator::class;
31
32
    /**
33
     * The name of this login form, to display in the frontend
34
     * Replaces Authenticator::get_name()
35
     *
36
     * @return string
37
     */
38
    public function getAuthenticatorName()
39
    {
40
        return _t(__CLASS__ . '.AUTHENTICATORNAME', 'SAML');
41
    }
42
43
    /**
44
     * Constructor
45
     *
46
     * @param RequestHandler $controller
47
     * @param string $authenticatorClass
48
     * @param string $name method on the $controller
49
     */
50
    public function __construct(RequestHandler $controller, $authenticatorClass, $name)
51
    {
52
        $backURL = $this->getSession()->get('BackURL');
53
54
        if (!empty($this->getRequest()->requestVar('BackURL'))) {
55
            $backURL = $this->getRequest()->requestVar('BackURL');
56
        }
57
        if ($this->shouldShowLogoutFields()) {
58
            $fields = FieldList::create([
59
                HiddenField::create('AuthenticationMethod', null, $this->authenticator_class, $this)
60
            ]);
61
            $actions = FieldList::create([
62
                FormAction::create(
63
                    'logout',
64
                    _t('SilverStripe\\Security\\Member.BUTTONLOGINOTHER', 'Log in as someone else')
65
                )
66
            ]);
67
        } else {
68
            $fields = $this->getFormFields();
69
            $actions = $this->getFormActions();
70
        }
71
72
        if ($backURL) {
73
            $fields->push(HiddenField::create('BackURL', 'BackURL', $backURL));
74
        }
75
76
        $this->setFormMethod('POST', true);
77
78
        parent::__construct($controller, $name, $fields, $actions);
79
    }
80
81
    protected function getFormFields()
82
    {
83
        return FieldList::create([
84
            HiddenField::create('AuthenticationMethod', null, $this->authenticator_class, $this)
85
        ]);
86
    }
87
88
    protected function getFormActions()
89
    {
90
        return FieldList::create([
91
            FormAction::create('dologin', _t('SilverStripe\\Security\\Member.BUTTONLOGIN', 'Log in'))
92
        ]);
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    protected function shouldShowLogoutFields()
99
    {
100
        if (!Security::getCurrentUser()) {
101
            return false;
102
        }
103
        return true;
104
    }
105
}
106