1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SilverStripe\Security\MemberAuthenticator; |
5
|
|
|
|
6
|
|
|
use SilverStripe\Control\HTTPResponse; |
7
|
|
|
use SilverStripe\Control\Session; |
8
|
|
|
use SilverStripe\Forms\FormRequestHandler; |
9
|
|
|
use SilverStripe\Security\Member; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class ChangePasswordHandler extends FormRequestHandler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Change the password |
16
|
|
|
* |
17
|
|
|
* @param array $data The user submitted data |
18
|
|
|
* @return HTTPResponse |
19
|
|
|
*/ |
20
|
|
|
public function doChangePassword(array $data) |
21
|
|
|
{ |
22
|
|
|
$member = Member::currentUser(); |
23
|
|
|
// The user was logged in, check the current password |
24
|
|
|
if ($member && ( |
25
|
|
|
empty($data['OldPassword']) || |
26
|
|
|
!$member->checkPassword($data['OldPassword'])->isValid() |
27
|
|
|
)) { |
28
|
|
|
$this->form->sessionMessage( |
29
|
|
|
_t('Member.ERRORPASSWORDNOTMATCH', "Your current password does not match, please try again"), |
30
|
|
|
"bad" |
31
|
|
|
); |
32
|
|
|
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
33
|
|
|
return $this->redirectBackToForm(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!$member) { |
37
|
|
|
if (Session::get('AutoLoginHash')) { |
38
|
|
|
$member = Member::member_from_autologinhash(Session::get('AutoLoginHash')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// The user is not logged in and no valid auto login hash is available |
42
|
|
|
if (!$member) { |
43
|
|
|
Session::clear('AutoLoginHash'); |
44
|
|
|
return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login'))); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Check the new password |
49
|
|
|
if (empty($data['NewPassword1'])) { |
50
|
|
|
$this->form->sessionMessage( |
51
|
|
|
_t('Member.EMPTYNEWPASSWORD', "The new password can't be empty, please try again"), |
52
|
|
|
"bad" |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
56
|
|
|
return $this->redirectBackToForm(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Fail if passwords do not match |
60
|
|
|
if ($data['NewPassword1'] !== $data['NewPassword2']) { |
61
|
|
|
$this->form->sessionMessage( |
62
|
|
|
_t('Member.ERRORNEWPASSWORD', "You have entered your new password differently, try again"), |
63
|
|
|
"bad" |
64
|
|
|
); |
65
|
|
|
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
66
|
|
|
return $this->redirectBackToForm(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Check if the new password is accepted |
70
|
|
|
$validationResult = $member->changePassword($data['NewPassword1']); |
71
|
|
|
if (!$validationResult->isValid()) { |
72
|
|
|
$this->form->setSessionValidationResult($validationResult); |
73
|
|
|
return $this->redirectBackToForm(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Clear locked out status |
77
|
|
|
$member->LockedOutUntil = null; |
78
|
|
|
$member->FailedLoginCount = null; |
79
|
|
|
$member->write(); |
80
|
|
|
|
81
|
|
|
if ($member->canLogIn()->isValid()) { |
82
|
|
|
$member->logIn(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// TODO Add confirmation message to login redirect |
86
|
|
|
Session::clear('AutoLoginHash'); |
87
|
|
|
|
88
|
|
|
// Redirect to backurl |
89
|
|
|
$backURL = $this->getBackURL(); |
90
|
|
|
if ($backURL) { |
91
|
|
|
return $this->redirect($backURL); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Redirect to default location - the login form saying "You are logged in as..." |
95
|
|
|
$url = Security::singleton()->Link('login'); |
96
|
|
|
return $this->redirect($url); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function redirectBackToForm() |
100
|
|
|
{ |
101
|
|
|
// Redirect back to form |
102
|
|
|
$url = $this->addBackURLParam(CMSSecurity::singleton()->Link('changepassword')); |
103
|
|
|
return $this->redirect($url); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|