LDAPSecurityController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 14
dl 0
loc 136
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPasswordResetLink() 0 7 1
A ChangePasswordForm() 0 4 1
A lostpassword() 0 37 4
A LostPasswordForm() 0 11 1
A Link() 0 4 1
A passwordsent() 0 24 3
1
<?php
2
/**
3
 * Class LDAPSecurityController
4
 *
5
 * This controller overrides the default Security controller with functionality
6
 * for resetting passwords.
7
 */
8
class LDAPSecurityController extends Security
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...
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $allowed_actions = [
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
        'index',
15
        'lostpassword',
16
        'LostPasswordForm',
17
        'ChangePasswordForm',
18
        'passwordsent'
19
    ];
20
21
    /**
22
     * This static function is *intentionally* overloaded from Security so
23
     * the user accesses this controller and uses the LDAP change password
24
     * form rather than the "standard" one provided by Security.
25
     *
26
     * @param Member $member
27
     * @param $autologinToken
28
     * @return string
29
     */
30
    public static function getPasswordResetLink($member, $autologinToken)
31
    {
32
        $autologinToken = urldecode($autologinToken);
33
        $selfControllerClass = __CLASS__;
34
        $selfController = new $selfControllerClass();
35
        return $selfController->Link('changepassword') . "?m={$member->ID}&t=$autologinToken";
36
    }
37
38
    /**
39
     * Factory method for the lost password form
40
     *
41
     * @return Form Returns the lost password form
42
     */
43
    public function ChangePasswordForm()
44
    {
45
        return Object::create('LDAPChangePasswordForm', $this, 'ChangePasswordForm');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Object::create('... 'ChangePasswordForm'); (Object) is incompatible with the return type of the parent method Security::ChangePasswordForm of type Security.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
    }
47
48
    public function lostpassword()
49
    {
50
        $controller = $this->getResponseController(_t('LDAPSecurityController.LOSTPASSWORDHEADER', 'Lost password'));
51
52
        // if the controller calls Director::redirect(), this will break early
53
        if (($response = $controller->getResponse()) && $response->isFinished()) {
54
            return $response;
55
        }
56
57
        if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')==='yes') {
58
            $customisedController = $controller->customise([
59
                'Content' =>
60
                    '<p>' .
61
                    _t(
62
                        'LDAPSecurityController.NOTERESETPASSWORDUSERNAMEOREMAIL',
63
                        'Enter your username or your email address and we will send you a link with which '
64
                        . 'you can reset your password'
65
                    ) .
66
                    '</p>',
67
                'Form' => $this->LostPasswordForm(),
68
            ]);
69
        } else {
70
            $customisedController = $controller->customise([
71
                'Content' =>
72
                    '<p>' .
73
                    _t(
74
                        'LDAPSecurityController.NOTERESETPASSWORDUSERNAME',
75
                        'Enter your username and we will send you a link with which you can reset your password'
76
                    ) .
77
                    '</p>',
78
                'Form' => $this->LostPasswordForm(),
79
            ]);
80
        }
81
82
        //Controller::$currentController = $controller;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
        return $customisedController->renderWith($this->getTemplatesFor('lostpassword'));
84
    }
85
86
    /**
87
     * Factory method for the lost password form
88
     *
89
     * @return Form Returns the lost password form
90
     */
91
    public function LostPasswordForm()
92
    {
93
        $email = new EmailField('Email', _t('Member.EMAIL', 'Email'));
94
        $action = new FormAction('forgotPassword', _t('Security.BUTTONSEND', 'Send me the password reset link'));
95
        return LDAPLoginForm::create($this,
96
            'LostPasswordForm',
97
            new FieldList([$email]),
98
            new FieldList([$action]),
99
            false
100
        );
101
    }
102
103
    /**
104
     * @param null $action
105
     * @return String
106
     */
107
    public function Link($action = null)
108
    {
109
        return Controller::join_links(Director::baseURL(), 'LDAPSecurity', $action);
110
    }
111
112
    /**
113
     * Show the "password sent" page, after a user has requested
114
     * to reset their password.
115
     *
116
     * @param SS_HTTPRequest $request The SS_HTTPRequest for this action.
117
     * @return string Returns the "password sent" page as HTML code.
118
     */
119
    public function passwordsent($request)
120
    {
121
        $controller = $this->getResponseController(_t('Security.LOSTPASSWORDHEADER', 'Lost Password'));
122
123
        // if the controller calls Director::redirect(), this will break early
124
        if (($response = $controller->getResponse()) && $response->isFinished()) {
125
            return $response;
126
        }
127
128
        $username = Convert::raw2xml(rawurldecode($request->param('ID')));
129
130
        $customisedController = $controller->customise([
131
            'Title' => _t('LDAPSecurity.PASSWORDSENTHEADER', "Password reset link sent to '{username}'",
132
                ['username' => $username]),
0 ignored issues
show
Documentation introduced by
array('username' => $username) is of type array<string,array|strin...rname":"array|string"}>, 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...
133
            'Content' =>
134
                "<p>"
135
                . _t('LDAPSecurity.PASSWORDSENTTEXT',
136
                    "Thank you! A reset link has been sent to '{username}', provided an account exists.",
137
                    ['username' => $username])
0 ignored issues
show
Documentation introduced by
array('username' => $username) is of type array<string,array|strin...rname":"array|string"}>, 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...
138
                . "</p>",
139
            'Username' => $username
140
        ]);
141
        return $customisedController->renderWith($this->getTemplatesFor('passwordsent'));
142
    }
143
}
144