Completed
Pull Request — master (#116)
by Franco
01:32
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\TextField;
14
use SilverStripe\ORM\ValidationResult;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Security\MemberAuthenticator\ChangePasswordForm;
17
use SilverStripe\Security\Security;
18
19
/**
20
 * @package activedirectory
21
 */
22
class LDAPChangePasswordForm extends ChangePasswordForm
23
{
24
    /**
25
     * The sole purpose for overriding the constructor is surfacing the username to the user.
26
     * @param \SilverStripe\Control\RequestHandler $controller
27
     * @param string $name
28
     * @param null $fields
29
     * @param null $actions
30
     */
31
    public function __construct($controller, $name, $fields = null, $actions = null)
32
    {
33
        parent::__construct($controller, $name, $fields, $actions);
34
35
        // Obtain the Member object. If the user got this far, they must have already been synced.
36
        $member = Security::getCurrentUser();
37
        if (!$member) {
38
            if ($this->getSession()->get('AutoLoginHash')) {
39
                $member = Member::member_from_autologinhash($this->getSession()->get('AutoLoginHash'));
40
            }
41
42
            // The user is not logged in and no valid auto login hash is available
43
            if (!$member) {
44
                $this->getSession()->clear('AutoLoginHash');
45
                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...
46
            }
47
        }
48
49
        $data = Injector::inst()
50
            ->get(LDAPService::class)
51
            ->getUserByGUID($member->GUID, ['samaccountname']);
52
53
        $emailField = null;
54
        $usernameField = null;
55
        if (Config::inst()->get(
56
            LDAPAuthenticator::class,
57
            'allow_email_login'
58
        ) === 'yes'
59
            && !empty($member->Email)
60
        ) {
61
            $emailField = TextField::create(
62
                'Email',
63
                _t(__CLASS__ . '.USERNAMEOREMAIL', 'Email'),
64
                $member->Email,
65
                null,
66
                $this
67
            );
68
        }
69
        if (!empty($data['samaccountname'])) {
70
            $usernameField = TextField::create(
71
                'Username',
72
                _t(__CLASS__ . '.USERNAME', 'Username'),
73
                $data['samaccountname'],
74
                null,
75
                $this
76
            );
77
        }
78
79
        if ($emailField) {
80
            $emailFieldReadonly = $emailField->performDisabledTransformation();
81
            $this->Fields()->unshift($emailFieldReadonly);
82
        }
83
        if ($usernameField) {
84
            $usernameFieldReadonly = $usernameField->performDisabledTransformation();
85
            $this->Fields()->unshift($usernameFieldReadonly);
86
        }
87
    }
88
}
89