2017-03-02 03:24:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
namespace SilverStripe\Security\MemberAuthenticator;
|
2017-03-02 03:24:38 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-03-02 03:24:38 +01:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Forms\FormRequestHandler;
|
2017-04-22 06:30:10 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
2017-05-07 21:11:00 +02:00
|
|
|
use SilverStripe\Security\IdentityStore;
|
2017-03-02 03:24:38 +01:00
|
|
|
|
|
|
|
class ChangePasswordHandler extends FormRequestHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Change the password
|
|
|
|
*
|
|
|
|
* @param array $data The user submitted data
|
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
2017-05-07 21:11:00 +02:00
|
|
|
public function doChangePassword(array $data, $form)
|
2017-03-02 03:24:38 +01:00
|
|
|
{
|
2017-05-20 06:32:25 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2017-03-02 03:24:38 +01:00
|
|
|
// The user was logged in, check the current password
|
|
|
|
if ($member && (
|
|
|
|
empty($data['OldPassword']) ||
|
|
|
|
!$member->checkPassword($data['OldPassword'])->isValid()
|
|
|
|
)) {
|
|
|
|
$this->form->sessionMessage(
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Security\\Member.ERRORPASSWORDNOTMATCH', "Your current password does not match, please try again"),
|
2017-03-02 03:24:38 +01:00
|
|
|
"bad"
|
|
|
|
);
|
|
|
|
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere.
|
|
|
|
return $this->redirectBackToForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$member) {
|
|
|
|
if (Session::get('AutoLoginHash')) {
|
|
|
|
$member = Member::member_from_autologinhash(Session::get('AutoLoginHash'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user is not logged in and no valid auto login hash is available
|
|
|
|
if (!$member) {
|
|
|
|
Session::clear('AutoLoginHash');
|
|
|
|
return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the new password
|
|
|
|
if (empty($data['NewPassword1'])) {
|
|
|
|
$this->form->sessionMessage(
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Security\\Member.EMPTYNEWPASSWORD', "The new password can't be empty, please try again"),
|
2017-03-02 03:24:38 +01:00
|
|
|
"bad"
|
|
|
|
);
|
|
|
|
|
|
|
|
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere.
|
|
|
|
return $this->redirectBackToForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if passwords do not match
|
|
|
|
if ($data['NewPassword1'] !== $data['NewPassword2']) {
|
|
|
|
$this->form->sessionMessage(
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Security\\Member.ERRORNEWPASSWORD', "You have entered your new password differently, try again"),
|
2017-03-02 03:24:38 +01:00
|
|
|
"bad"
|
|
|
|
);
|
|
|
|
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere.
|
|
|
|
return $this->redirectBackToForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the new password is accepted
|
|
|
|
$validationResult = $member->changePassword($data['NewPassword1']);
|
|
|
|
if (!$validationResult->isValid()) {
|
|
|
|
$this->form->setSessionValidationResult($validationResult);
|
|
|
|
return $this->redirectBackToForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear locked out status
|
|
|
|
$member->LockedOutUntil = null;
|
|
|
|
$member->FailedLoginCount = null;
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
if ($member->canLogIn()->isValid()) {
|
2017-05-07 21:11:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)
|
|
|
|
->logIn($member, false, $form->getRequestHandler()->getRequest());
|
2017-03-02 03:24:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Add confirmation message to login redirect
|
|
|
|
Session::clear('AutoLoginHash');
|
|
|
|
|
|
|
|
// Redirect to backurl
|
|
|
|
$backURL = $this->getBackURL();
|
|
|
|
if ($backURL) {
|
|
|
|
return $this->redirect($backURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to default location - the login form saying "You are logged in as..."
|
|
|
|
$url = Security::singleton()->Link('login');
|
|
|
|
return $this->redirect($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function redirectBackToForm()
|
|
|
|
{
|
|
|
|
// Redirect back to form
|
|
|
|
$url = $this->addBackURLParam(CMSSecurity::singleton()->Link('changepassword'));
|
|
|
|
return $this->redirect($url);
|
|
|
|
}
|
|
|
|
}
|