AdminLoginForm::sendPasswordResetLinkEmail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
/**
4
 * Class AdminLoginForm.
5
 */
6
class AdminLoginForm extends MemberLoginForm
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
9
    {
10
        parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);
11
12
        if ($this->Actions()->fieldByName('forgotPassword')) {
13
            // replaceField won't work, since it's a dataless field
14
            $this->Actions()->removeByName('forgotPassword');
15
            $this->Actions()->push(new LiteralField(
16
                'forgotPassword',
17
                '<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">'
18
                ._t('Member.BUTTONLOSTPASSWORD', "I've lost my password").'</a></p>'
19
            ));
20
        }
21
22
        Requirements::customScript(<<<'JS'
23
			(function() {
24
				var el = document.getElementById("AdminLoginForm_LoginForm_Email");
25
				if(el && el.focus) el.focus();
26
			})();
27
JS
28
        );
29
    }
30
31
    /**
32
     * @param array $data
33
     *
34
     * @return SS_HTTPResponse
35
     */
36
    public function forgotPassword($data)
37
    {
38
        if ($data['Email']) {
39
            /* @var $member Member */
40
            if ($member = Member::get()->where("Email = '".Convert::raw2sql($data['Email'])."'")->first()) {
41
                $token = $member->generateAutologinTokenAndStoreHash();
42
                $this->sendPasswordResetLinkEmail($member, $token);
43
            }
44
45
            return $this->controller->redirect('AdminSecurity/passwordsent/'.urlencode($data['Email']));
46
        }
47
48
        $this->sessionMessage(
49
            _t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'),
50
            'bad'
51
        );
52
53
        return $this->controller->redirect('AdminSecurity/lostpassword');
54
    }
55
56
    /**
57
     * @param Member $member
58
     * @param string $token
59
     */
60
    protected function sendPasswordResetLinkEmail($member, $token)
61
    {
62
        /* @var $email Member_ForgotPasswordEmail */
63
        $email = Member_ForgotPasswordEmail::create();
64
        $email->populateTemplate($member);
65
        $email->populateTemplate([
66
            'PasswordResetLink' => AdminSecurity::getPasswordResetLink($member, $token),
67
        ]);
68
        $email->setTo($member->Email);
69
        $email->send();
70
    }
71
}
72