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

LDAPLoginForm::__construct()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 8.6868
c 0
b 0
f 0
cc 6
eloc 33
nc 8
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Forms;
4
5
use SilverStripe\ActiveDirectory\Services\LDAPService;
6
use SilverStripe\Control\RequestHandler;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\RequiredFields;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\Security\MemberAuthenticator\MemberLoginForm;
12
use SilverStripe\Security\Security;
13
use SilverStripe\View\Requirements;
14
15
/**
16
 * Class LDAPLoginForm
17
 *
18
 * This not very interesting in itself. It's pretty much boiler-plate code to access the authenticator.
19
 *
20
 * @package activedirectory
21
 */
22
class LDAPLoginForm extends MemberLoginForm
23
{
24
    /**
25
     * This field is used in the "You are logged in as %s" message
26
     * @var string
27
     */
28
    public $loggedInAsField = 'FirstName';
29
30
    /**
31
     * @var string
32
     */
33
    protected $authenticator_class = LDAPAuthenticator::class;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param RequestHandler $controller
39
     * @param string $authenticatorClass
40
     * @param string $name method on the $controller
41
     */
42
    public function __construct(RequestHandler $controller, $authenticatorClass, $name)
43
    {
44
45
        parent::__construct($controller, 'LDAPAuthenticator', $name);
46
47
        if (Config::inst()->get(LDAPAuthenticator::class, 'allow_email_login') === 'yes') {
48
            $loginField = TextField::create(
49
                'Login',
50
                _t(__CLASS__ . '.USERNAMEOREMAIL', 'Username or email'),
51
                null,
52
                null,
53
                $this
54
            );
55
        } else {
56
            $loginField = TextField::create('Login', _t(__CLASS__ . '.USERNAME', 'Username'), null, null, $this);
57
        }
58
59
        $this->Fields()->replaceField('Email', $loginField);
60
        $this->setValidator(new RequiredFields('Login', 'Password'));
61
        if (Security::config()->remember_username) {
62
            $loginField->setValue($this->getSession()->get('SessionForms.MemberLoginForm.Email'));
63
        } else {
64
            // Some browsers won't respect this attribute unless it's added to the form
65
            $this->setAttribute('autocomplete', 'off');
66
            $loginField->setAttribute('autocomplete', 'off');
67
        }
68
69
        // Users can't change passwords unless appropriate a LDAP user with write permissions is
70
        // configured the LDAP connection binding
71
        $this->Actions()->remove($this->Actions()->fieldByName('forgotPassword'));
72
        $allowPasswordChange = Config::inst()
73
            ->get(LDAPService::class, 'allow_password_change');
74
        if ($allowPasswordChange && $name != 'LostPasswordForm' && !Security::getCurrentUser()) {
75
            $forgotPasswordLink = sprintf(
76
                '<p id="ForgotPassword"><a href="%s">%s</a></p>',
77
                Security::singleton()->Link('lostpassword'),
78
                _t('SilverStripe\\Security\\Member.BUTTONLOSTPASSWORD', "I've lost my password")
79
            );
80
            $forgotPassword = LiteralField::create('forgotPassword', $forgotPasswordLink);
81
            $this->Actions()->add($forgotPassword);
82
        }
83
84
        // Focus on the Username field when the page is loaded
85
        Requirements::block('MemberLoginFormFieldFocus');
86
        $js = <<<JS
87
			(function() {
88
				var el = document.getElementById("Login");
89
				if(el && el.focus && (typeof jQuery == 'undefined' || jQuery(el).is(':visible'))) el.focus();
90
			})();
91
JS;
92
        Requirements::customScript($js, 'LDAPLoginFormFieldFocus');
93
    }
94
95
    /**
96
     * The name of this login form, to display in the frontend
97
     * Replaces Authenticator::get_name()
98
     *
99
     * @return string
100
     */
101
    public function getAuthenticatorName()
102
    {
103
        return _t(__CLASS__ . '.AUTHENTICATORNAME', 'LDAP');
104
    }
105
}
106