Passed
Push — master ( 3c190c...2857c2 )
by Robbie
03:26
created

LDAPChangePasswordForm   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 55 9
1
<?php
2
3
namespace SilverStripe\LDAP\Forms;
4
5
use Exception;
6
use SilverStripe\LDAP\Authenticators\LDAPAuthenticator;
7
use SilverStripe\LDAP\Services\LDAPService;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Control\HTTP;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Control\RequestHandler;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Core\Injector\Injector;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Forms\TextField;
16
use SilverStripe\ORM\ValidationResult;
17
use SilverStripe\Security\Member;
18
use SilverStripe\Security\MemberAuthenticator\ChangePasswordForm;
19
use SilverStripe\Security\Security;
20
21
class LDAPChangePasswordForm extends ChangePasswordForm
22
{
23
    /**
24
     * The sole purpose for overriding the constructor is surfacing the username to the user.
25
     * @param RequestHandler $controller
26
     * @param string $name
27
     * @param FieldList $fields
28
     * @param FieldList $actions
29
     */
30
    public function __construct($controller, $name, $fields = null, $actions = null)
31
    {
32
        parent::__construct($controller, $name, $fields, $actions);
33
34
        // Obtain the Member object. If the user got this far, they must have already been synced.
35
        $member = Security::getCurrentUser();
36
        if (!$member) {
37
            if ($this->getSession()->get('AutoLoginHash')) {
38
                $member = Member::member_from_autologinhash($this->getSession()->get('AutoLoginHash'));
0 ignored issues
show
Bug introduced by
It seems like $this->getSession()->get('AutoLoginHash') can also be of type array; however, parameter $hash of SilverStripe\Security\Me...er_from_autologinhash() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
                $member = Member::member_from_autologinhash(/** @scrutinizer ignore-type */ $this->getSession()->get('AutoLoginHash'));
Loading history...
39
            }
40
41
            // The user is not logged in and no valid auto login hash is available
42
            if (!$member) {
43
                $this->getSession()->clear('AutoLoginHash');
44
                return $this->controller->redirect($this->controller->Link('login'));
45
            }
46
        }
47
48
        $data = Injector::inst()
49
            ->get(LDAPService::class)
50
            ->getUserByGUID($member->GUID, ['samaccountname']);
51
52
        $emailField = null;
53
        $usernameField = null;
54
        if (Config::inst()->get(
55
            LDAPAuthenticator::class,
56
            'allow_email_login'
57
        ) === 'yes'
58
            && !empty($member->Email)
59
        ) {
60
            $emailField = TextField::create(
61
                'Email',
0 ignored issues
show
Bug introduced by
'Email' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
                /** @scrutinizer ignore-type */ 'Email',
Loading history...
62
                _t(__CLASS__ . '.USERNAMEOREMAIL', 'Email'),
63
                $member->Email,
64
                null,
65
                $this
0 ignored issues
show
Bug introduced by
$this of type SilverStripe\LDAP\Forms\LDAPChangePasswordForm is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                /** @scrutinizer ignore-type */ $this
Loading history...
66
            );
67
        }
68
        if (!empty($data['samaccountname'])) {
69
            $usernameField = TextField::create(
70
                'Username',
71
                _t(__CLASS__ . '.USERNAME', 'Username'),
72
                $data['samaccountname'],
73
                null,
74
                $this
75
            );
76
        }
77
78
        if ($emailField) {
79
            $emailFieldReadonly = $emailField->performDisabledTransformation();
80
            $this->Fields()->unshift($emailFieldReadonly);
81
        }
82
        if ($usernameField) {
83
            $usernameFieldReadonly = $usernameField->performDisabledTransformation();
84
            $this->Fields()->unshift($usernameFieldReadonly);
85
        }
86
    }
87
}
88