Completed
Push — master ( 197387...875417 )
by Damian
11s
created

LDAPSecurityController::passwordsent()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 2
nop 1
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Control;
4
5
use SilverStripe\ActiveDirectory\Authenticators\LDAPLoginForm;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Core\Object;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Forms\EmailField;
12
use SilverStripe\Forms\FormAction;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Security\Security;
15
16
/**
17
 * Class LDAPSecurityController
18
 *
19
 * This controller overrides the default Security controller with functionality
20
 * for resetting passwords.
21
 *
22
 * @package activedirectory
23
 */
24
class LDAPSecurityController extends Security
25
{
26
    /**
27
     * @var array
28
     */
29
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
30
        'index',
31
        'lostpassword',
32
        'LostPasswordForm',
33
        'ChangePasswordForm',
34
        'passwordsent'
35
    ];
36
37
    /**
38
     * This static function is *intentionally* overloaded from Security so
39
     * the user accesses this controller and uses the LDAP change password
40
     * form rather than the "standard" one provided by Security.
41
     *
42
     * @param Member $member
43
     * @param $autologinToken
44
     * @return string
45
     */
46
    public static function getPasswordResetLink($member, $autologinToken)
47
    {
48
        $autologinToken = urldecode($autologinToken);
49
        $selfControllerClass = __CLASS__;
50
        $selfController = new $selfControllerClass();
51
        return $selfController->Link('changepassword') . "?m={$member->ID}&t=$autologinToken";
52
    }
53
54
    /**
55
     * Factory method for the lost password form
56
     *
57
     * @return Form Returns the lost password form
58
     */
59
    public function ChangePasswordForm()
60
    {
61
        return Object::create('SilverStripe\\ActiveDirectory\\Forms\\LDAPChangePasswordForm', $this, 'ChangePasswordForm');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \SilverStripe\Cor... 'ChangePasswordForm'); (SilverStripe\ActiveDirec...\LDAPSecurityController) is incompatible with the return type of the parent method SilverStripe\Security\Security::ChangePasswordForm of type SilverStripe\Security\ChangePasswordForm.

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...
62
    }
63
64
    public function lostpassword()
65
    {
66
        $controller = $this->getResponseController(_t('LDAPSecurityController.LOSTPASSWORDHEADER', 'Lost password'));
67
68
        // if the controller calls Director::redirect(), this will break early
69
        if (($response = $controller->getResponse()) && $response->isFinished()) {
70
            return $response;
71
        }
72
73
        if (Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'allow_email_login') === 'yes') {
74
            $customisedController = $controller->customise([
75
                'Content' =>
76
                    _t(
77
                        'LDAPSecurityController.NOTERESETPASSWORDUSERNAMEOREMAIL',
78
                        'Enter your username or your email address and we will send you a link with which '
79
                        . 'you can reset your password'
80
                    ),
81
                'Form' => $this->LostPasswordForm(),
82
            ]);
83
        } else {
84
            $customisedController = $controller->customise([
85
                'Content' =>
86
                    _t(
87
                        'LDAPSecurityController.NOTERESETPASSWORDUSERNAME',
88
                        'Enter your username and we will send you a link with which you can reset your password'
89
                    ),
90
                'Form' => $this->LostPasswordForm(),
91
            ]);
92
        }
93
94
        //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...
95
        return $customisedController->renderWith($this->getTemplatesFor('lostpassword'));
96
    }
97
98
    /**
99
     * Factory method for the lost password form
100
     *
101
     * @return Form Returns the lost password form
102
     */
103
    public function LostPasswordForm()
104
    {
105
        $email = EmailField::create('Email', _t('Member.EMAIL', 'Email'));
106
        $action = FormAction::create('forgotPassword', _t('Security.BUTTONSEND', 'Send me the password reset link'));
107
        return LDAPLoginForm::create(
108
            $this,
109
            'LostPasswordForm',
110
            FieldList::create([$email]),
111
            FieldList::create([$action]),
112
            false
113
        );
114
    }
115
116
    /**
117
     * @param null $action
118
     * @return String
119
     */
120
    public function Link($action = null)
121
    {
122
        return Controller::join_links(Director::baseURL(), 'LDAPSecurity', $action);
123
    }
124
125
    /**
126
     * Show the "password sent" page, after a user has requested
127
     * to reset their password.
128
     *
129
     * @param SS_HTTPRequest $request The SS_HTTPRequest for this action.
130
     * @return string Returns the "password sent" page as HTML code.
131
     */
132
    public function passwordsent($request)
133
    {
134
        $controller = $this->getResponseController(_t('Security.LOSTPASSWORDHEADER', 'Lost Password'));
135
136
        // if the controller calls Director::redirect(), this will break early
137
        if (($response = $controller->getResponse()) && $response->isFinished()) {
138
            return $response;
139
        }
140
141
        $username = Convert::raw2xml(rawurldecode($request->param('ID')));
142
143
        $customisedController = $controller->customise([
144
            'Title' => _t(
145
                'LDAPSecurity.PASSWORDSENTHEADER',
146
                "Password reset link sent to '{username}'",
147
                ['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...
148
            ),
149
            'Content' =>
150
                _t(
151
                    'LDAPSecurity.PASSWORDSENTTEXT',
152
                    "Thank you! A reset link has been sent to '{username}', provided an account exists.",
153
                    ['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...
154
                ),
155
            'Username' => $username
156
        ]);
157
        return $customisedController->renderWith($this->getTemplatesFor('passwordsent'));
158
    }
159
}
160