LDAPLoginForm::getAuthenticatorName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\LDAP\Forms;
4
5
use SilverStripe\LDAP\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
class LDAPLoginForm extends MemberLoginForm
21
{
22
    /**
23
     * This field is used in the "You are logged in as %s" message
24
     * @var string
25
     */
26
    public $loggedInAsField = 'FirstName';
27
28
    /**
29
     * @var string
30
     */
31
    protected $authenticator_class = LDAPAuthenticator::class;
0 ignored issues
show
Bug introduced by
The type SilverStripe\LDAP\Forms\LDAPAuthenticator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param RequestHandler $controller
37
     * @param string $authenticatorClass
38
     * @param string $name method on the $controller
39
     */
40
    public function __construct(RequestHandler $controller, $authenticatorClass, $name)
41
    {
42
43
        parent::__construct($controller, 'LDAPAuthenticator', $name);
44
45
        if (Config::inst()->get(LDAPAuthenticator::class, 'allow_email_login') === 'yes') {
46
            $loginField = TextField::create(
47
                'Login',
48
                _t(__CLASS__ . '.USERNAMEOREMAIL', 'Username or email'),
49
                null,
50
                null,
51
                $this
52
            );
53
        } else {
54
            $loginField = TextField::create('Login', _t(__CLASS__ . '.USERNAME', 'Username'), null, null, $this);
55
        }
56
57
        $this->Fields()->replaceField('Email', $loginField);
58
        $this->setValidator(new RequiredFields('Login', 'Password'));
59
        if (Security::config()->remember_username) {
60
            $loginField->setValue($this->getSession()->get('SessionForms.MemberLoginForm.Email'));
61
        } else {
62
            // Some browsers won't respect this attribute unless it's added to the form
63
            $this->setAttribute('autocomplete', 'off');
64
            $loginField->setAttribute('autocomplete', 'off');
65
        }
66
67
        // Users can't change passwords unless appropriate a LDAP user with write permissions is
68
        // configured the LDAP connection binding
69
        $this->Actions()->remove($this->Actions()->fieldByName('forgotPassword'));
70
        $allowPasswordChange = Config::inst()->get(LDAPService::class, 'allow_password_change');
71
        if ($allowPasswordChange && $name != 'LostPasswordForm' && !Security::getCurrentUser()) {
72
            $forgotPasswordLink = sprintf(
73
                '<p id="ForgotPassword"><a href="%s">%s</a></p>',
74
                Security::singleton()->Link('lostpassword'),
75
                _t('SilverStripe\\Security\\Member.BUTTONLOSTPASSWORD', "I've lost my password")
76
            );
77
            $forgotPassword = LiteralField::create('forgotPassword', $forgotPasswordLink);
78
            $this->Actions()->add($forgotPassword);
79
        }
80
81
        // Focus on the Username field when the page is loaded
82
        Requirements::block('MemberLoginFormFieldFocus');
83
        $js = <<<JS
84
			(function() {
85
				var el = document.getElementById("Login");
86
				if(el && el.focus && (typeof jQuery == 'undefined' || jQuery(el).is(':visible'))) el.focus();
87
			})();
88
JS;
89
        Requirements::customScript($js, 'LDAPLoginFormFieldFocus');
90
    }
91
92
    /**
93
     * The name of this login form, to display in the frontend
94
     * Replaces Authenticator::get_name()
95
     *
96
     * @return string
97
     */
98
    public function getAuthenticatorName()
99
    {
100
        return _t(__CLASS__ . '.AUTHENTICATORNAME', 'LDAP');
101
    }
102
}
103