SAMLLoginForm   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 7
dl 0
loc 124
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 36 7
A shouldShowLogoutFields() 0 10 3
A getMessageFromSession() 0 16 3
A dologin() 0 4 1
A logout() 0 5 1
1
<?php
2
/**
3
 * Class SAMLLoginForm
4
 *
5
 * This not very interesting in itself. It's pretty much boiler-plate code to access the authenticator.
6
 */
7
class SAMLLoginForm extends LoginForm
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * This field is used in the "You are logged in as %s" message
11
     * @var string
12
     */
13
    public $loggedInAsField = 'FirstName';
14
15
    /**
16
     * @var string
17
     */
18
    protected $authenticator_class = 'SAMLAuthenticator';
19
20
    /**
21
     * Constructor
22
     *
23
     * @param Controller $controller
24
     * @param string $name method on the $controller
25
     * @param FieldList $fields
26
     * @param FieldList $actions
27
     * @param bool $checkCurrentUser - show logout button if logged in
28
     */
29
    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
    {
31
        $backURL = Session::get('BackURL');
32
33
        if (isset($_REQUEST['BackURL'])) {
34
            $backURL = $_REQUEST['BackURL'];
35
        }
36
37
        if ($checkCurrentUser && $this->shouldShowLogoutFields()) {
38
            $fields = new FieldList([
39
                new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this)
0 ignored issues
show
Unused Code introduced by
The call to HiddenField::__construct() has too many arguments starting with $this.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
40
            ]);
41
            $actions = new FieldList([
42
                new FormAction("logout", _t('Member.BUTTONLOGINOTHER', "Log in as someone else"))
43
            ]);
44
        } else {
45
            if (!$fields) {
46
                $fields = new FieldList([
47
                    new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this)
0 ignored issues
show
Unused Code introduced by
The call to HiddenField::__construct() has too many arguments starting with $this.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
48
                ]);
49
            }
50
            if (!$actions) {
51
                $actions = new FieldList([
52
                    new FormAction('dologin', _t('Member.BUTTONLOGIN', "Log in"))
53
                ]);
54
            }
55
        }
56
57
        if ($backURL) {
58
            $fields->push(new HiddenField('BackURL', 'BackURL', $backURL));
59
        }
60
61
        $this->setFormMethod('POST', true);
62
63
        parent::__construct($controller, $name, $fields, $actions);
64
    }
65
66
67
    /**
68
     *
69
     *
70
     * @return bool
71
     */
72
    protected function shouldShowLogoutFields()
73
    {
74
        if (!Member::currentUser()) {
75
            return false;
76
        }
77
        if (!Member::logged_in_session_exists()) {
78
            return false;
79
        }
80
        return true;
81
    }
82
83
    /**
84
     * Get message from session
85
     */
86
    protected function getMessageFromSession()
87
    {
88
        // The "MemberLoginForm.force_message session" is set in Security#permissionFailure()
89
        // and displays messages like "You don't have access to this page"
90
        // if force isn't set, it will just display "You're logged in as {name}"
91
        if (($member = Member::currentUser()) && !Session::get('MemberLoginForm.force_message')) {
92
            $this->message = _t(
93
                'Member.LOGGEDINAS',
94
                "You're logged in as {name}.",
95
                ['name' => $member->{$this->loggedInAsField}]
0 ignored issues
show
Documentation introduced by
array('name' => $member-...this->loggedInAsField}) is of type array<string,?,{"name":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
            );
97
        }
98
        Session::set('MemberLoginForm.force_message', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
        parent::getMessageFromSession();
100
        return $this->message;
101
    }
102
103
104
    /**
105
     * Login form handler method
106
     *
107
     * This method is called when the user clicks on "Log in"
108
     *
109
     * @param array $data Submitted data
110
     */
111
    public function dologin($data)
112
    {
113
        call_user_func_array([$this->authenticator_class, 'authenticate'], [$data, $this]);
114
    }
115
116
117
    /**
118
     * Log out form handler method
119
     *
120
     * This method is called when the user clicks on "logout" on the form
121
     * created when the parameter <i>$checkCurrentUser</i> of the
122
     * {@link __construct constructor} was set to TRUE and the user was
123
     * currently logged in.
124
     */
125
    public function logout()
126
    {
127
        $s = new Security();
128
        $s->logout(false);
129
    }
130
}
131