Completed
Push — master ( 232009...3c190c )
by Robbie
14s
created

LDAPChangePasswordForm::doChangePassword()   D

Complexity

Conditions 13
Paths 54

Size

Total Lines 110
Code Lines 64

Duplication

Lines 29
Ratio 26.36 %

Importance

Changes 0
Metric Value
dl 29
loc 110
rs 4.9922
c 0
b 0
f 0
cc 13
eloc 64
nc 54
nop 1

How to fix   Long Method    Complexity   

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 Exception;
6
use SilverStripe\ActiveDirectory\Authenticators\LDAPAuthenticator;
7
use SilverStripe\ActiveDirectory\Services\LDAPService;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Control\HTTP;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Forms\TextField;
15
use SilverStripe\ORM\ValidationResult;
16
use SilverStripe\Security\Member;
17
use SilverStripe\Security\MemberAuthenticator\ChangePasswordForm;
18
use SilverStripe\Security\Security;
19
20
/**
21
 * @package activedirectory
22
 */
23
class LDAPChangePasswordForm extends ChangePasswordForm
24
{
25
    /**
26
     * The sole purpose for overriding the constructor is surfacing the username to the user.
27
     * @param \SilverStripe\Control\RequestHandler $controller
28
     * @param string $name
29
     * @param FieldList $fields
30
     * @param FieldList $actions
31
     */
32
    public function __construct($controller, $name, $fields = null, $actions = null)
33
    {
34
        parent::__construct($controller, $name, $fields, $actions);
35
36
        // Obtain the Member object. If the user got this far, they must have already been synced.
37
        $member = Security::getCurrentUser();
38
        if (!$member) {
39
            if ($this->getSession()->get('AutoLoginHash')) {
40
                $member = Member::member_from_autologinhash($this->getSession()->get('AutoLoginHash'));
41
            }
42
43
            // The user is not logged in and no valid auto login hash is available
44
            if (!$member) {
45
                $this->getSession()->clear('AutoLoginHash');
46
                return $this->controller->redirect($this->controller->Link('login'));
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
47
            }
48
        }
49
50
        $data = Injector::inst()
51
            ->get(LDAPService::class)
52
            ->getUserByGUID($member->GUID, ['samaccountname']);
53
54
        $emailField = null;
55
        $usernameField = null;
56
        if (Config::inst()->get(
57
            LDAPAuthenticator::class,
58
            'allow_email_login'
59
        ) === 'yes'
60
            && !empty($member->Email)
61
        ) {
62
            $emailField = TextField::create(
63
                'Email',
64
                _t(__CLASS__ . '.USERNAMEOREMAIL', 'Email'),
65
                $member->Email,
66
                null,
67
                $this
68
            );
69
        }
70
        if (!empty($data['samaccountname'])) {
71
            $usernameField = TextField::create(
72
                'Username',
73
                _t(__CLASS__ . '.USERNAME', 'Username'),
74
                $data['samaccountname'],
75
                null,
76
                $this
77
            );
78
        }
79
80
        if ($emailField) {
81
            $emailFieldReadonly = $emailField->performDisabledTransformation();
82
            $this->Fields()->unshift($emailFieldReadonly);
83
        }
84
        if ($usernameField) {
85
            $usernameFieldReadonly = $usernameField->performDisabledTransformation();
86
            $this->Fields()->unshift($usernameFieldReadonly);
87
        }
88
    }
89
}
90