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-08-25 05:07:30 +02:00
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-03-02 03:24:38 +01:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Control\RequestHandler;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2017-08-25 05:07:30 +02:00
|
|
|
use SilverStripe\ORM\ValidationException;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\Authenticator;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Security\IdentityStore;
|
2017-04-22 06:30:10 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
2017-03-02 03:24:38 +01:00
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
class ChangePasswordHandler extends RequestHandler
|
2017-03-02 03:24:38 +01:00
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
/**
|
|
|
|
* @var Authenticator
|
|
|
|
*/
|
|
|
|
protected $authenticator;
|
|
|
|
|
|
|
|
/**
|
2018-03-23 03:28:00 +01:00
|
|
|
* Link to this handler
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2018-03-23 03:28:00 +01:00
|
|
|
protected $link = null;
|
2017-05-30 09:42:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Allowed Actions
|
|
|
|
*/
|
|
|
|
private static $allowed_actions = [
|
|
|
|
'changepassword',
|
|
|
|
'changePasswordForm',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array URL Handlers. All should point to changepassword
|
|
|
|
*/
|
|
|
|
private static $url_handlers = [
|
|
|
|
'' => 'changepassword',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $link The URL to recreate this request handler
|
|
|
|
* @param MemberAuthenticator $authenticator
|
|
|
|
*/
|
|
|
|
public function __construct($link, MemberAuthenticator $authenticator)
|
|
|
|
{
|
|
|
|
$this->link = $link;
|
|
|
|
$this->authenticator = $authenticator;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the change password request
|
|
|
|
* @todo this could use some spring cleaning
|
|
|
|
*
|
2017-06-08 09:12:28 +02:00
|
|
|
* @return array|HTTPResponse
|
2017-05-30 09:42:00 +02:00
|
|
|
*/
|
|
|
|
public function changepassword()
|
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
|
|
|
// Extract the member from the URL.
|
|
|
|
/** @var Member $member */
|
|
|
|
$member = null;
|
|
|
|
if ($request->getVar('m') !== null) {
|
|
|
|
$member = Member::get()->filter(['ID' => (int)$request->getVar('m')])->first();
|
|
|
|
}
|
|
|
|
$token = $request->getVar('t');
|
|
|
|
|
|
|
|
// Check whether we are merely changin password, or resetting.
|
|
|
|
if ($token !== null && $member && $member->validateAutoLoginToken($token)) {
|
|
|
|
$this->setSessionToken($member, $token);
|
|
|
|
|
|
|
|
// Redirect to myself, but without the hash in the URL
|
|
|
|
return $this->redirect($this->link);
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$session = $this->getRequest()->getSession();
|
|
|
|
if ($session->get('AutoLoginHash')) {
|
2017-05-30 09:42:00 +02:00
|
|
|
$message = DBField::create_field(
|
|
|
|
'HTMLFragment',
|
|
|
|
'<p>' . _t(
|
|
|
|
'SilverStripe\\Security\\Security.ENTERNEWPASSWORD',
|
|
|
|
'Please enter a new password.'
|
|
|
|
) . '</p>'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Subsequent request after the "first load with hash" (see previous if clause).
|
2017-06-08 09:12:28 +02:00
|
|
|
return [
|
|
|
|
'Content' => $message,
|
|
|
|
'Form' => $this->changePasswordForm()
|
|
|
|
];
|
2017-05-30 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
// Logged in user requested a password change form.
|
|
|
|
$message = DBField::create_field(
|
|
|
|
'HTMLFragment',
|
|
|
|
'<p>' . _t(
|
|
|
|
'SilverStripe\\Security\\Security.CHANGEPASSWORDBELOW',
|
|
|
|
'You can change your password below.'
|
|
|
|
) . '</p>'
|
|
|
|
);
|
|
|
|
|
2017-06-08 09:12:28 +02:00
|
|
|
return [
|
|
|
|
'Content' => $message,
|
|
|
|
'Form' => $this->changePasswordForm()
|
|
|
|
];
|
2017-05-30 09:42:00 +02:00
|
|
|
}
|
|
|
|
// Show a friendly message saying the login token has expired
|
|
|
|
if ($token !== null && $member && !$member->validateAutoLoginToken($token)) {
|
2018-03-07 13:59:17 +01:00
|
|
|
$message = DBField::create_field(
|
|
|
|
'HTMLFragment',
|
|
|
|
_t(
|
|
|
|
'SilverStripe\\Security\\Security.NOTERESETLINKINVALID',
|
|
|
|
'<p>The password reset link is invalid or expired.</p>'
|
|
|
|
. '<p>You can request a new one <a href="{link1}">here</a> or change your password after'
|
|
|
|
. ' you <a href="{link2}">logged in</a>.</p>',
|
|
|
|
[
|
2018-03-23 03:28:00 +01:00
|
|
|
'link1' => $this->Link('lostpassword'),
|
|
|
|
'link2' => $this->Link('login')
|
2018-03-07 13:59:17 +01:00
|
|
|
]
|
2017-05-30 09:42:00 +02:00
|
|
|
)
|
2018-03-07 13:59:17 +01:00
|
|
|
);
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-06-08 09:12:28 +02:00
|
|
|
return [
|
|
|
|
'Content' => $message,
|
|
|
|
];
|
2017-05-30 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Someone attempted to go to changepassword without token or being logged in
|
|
|
|
return Security::permissionFailure(
|
|
|
|
Controller::curr(),
|
|
|
|
_t(
|
|
|
|
'SilverStripe\\Security\\Security.ERRORPASSWORDPERMISSION',
|
|
|
|
'You must be logged in in order to change your password!'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @param string $token
|
|
|
|
*/
|
|
|
|
protected function setSessionToken($member, $token)
|
|
|
|
{
|
|
|
|
// if there is a current member, they should be logged out
|
|
|
|
if ($curMember = Security::getCurrentUser()) {
|
|
|
|
/** @var LogoutHandler $handler */
|
|
|
|
Injector::inst()->get(IdentityStore::class)->logOut();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the hash for the change password form. Will be unset after reload within the ChangePasswordForm.
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->getRequest()->getSession()->set('AutoLoginHash', $member->encryptWithUserSettings($token));
|
2017-05-30 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a link to this request handler.
|
|
|
|
* The link returned is supplied in the constructor
|
2018-03-23 03:28:00 +01:00
|
|
|
*
|
|
|
|
* @param string|null $action
|
2017-05-30 09:42:00 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-03-23 03:28:00 +01:00
|
|
|
public function Link($action = null)
|
2017-05-30 09:42:00 +02:00
|
|
|
{
|
2018-03-23 03:28:00 +01:00
|
|
|
$link = Controller::join_links($this->link, $action);
|
|
|
|
$this->extend('updateLink', $link, $action);
|
|
|
|
return $link;
|
2017-05-30 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method for the lost password form
|
|
|
|
*
|
|
|
|
* @skipUpgrade
|
|
|
|
* @return ChangePasswordForm Returns the lost password form
|
|
|
|
*/
|
|
|
|
public function changePasswordForm()
|
|
|
|
{
|
|
|
|
return ChangePasswordForm::create(
|
|
|
|
$this,
|
|
|
|
'ChangePasswordForm'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
/**
|
|
|
|
* Change the password
|
|
|
|
*
|
|
|
|
* @param array $data The user submitted data
|
2017-06-08 09:12:28 +02:00
|
|
|
* @param ChangePasswordForm $form
|
2017-03-02 03:24:38 +01:00
|
|
|
* @return HTTPResponse
|
2017-08-25 05:07:30 +02:00
|
|
|
* @throws ValidationException
|
|
|
|
* @throws NotFoundExceptionInterface
|
2017-03-02 03:24:38 +01:00
|
|
|
*/
|
2017-06-08 09:12:28 +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
|
2017-06-15 04:20:12 +02:00
|
|
|
$oldPassword = isset($data['OldPassword']) ? $data['OldPassword'] : null;
|
|
|
|
if ($member && !$this->checkPassword($member, $oldPassword)) {
|
2017-06-08 09:12:28 +02:00
|
|
|
$form->sessionMessage(
|
2017-05-30 09:42:00 +02:00
|
|
|
_t(
|
|
|
|
'SilverStripe\\Security\\Member.ERRORPASSWORDNOTMATCH',
|
2017-06-08 09:12:28 +02:00
|
|
|
'Your current password does not match, please try again'
|
2017-05-30 09:42:00 +02:00
|
|
|
),
|
2017-06-08 09:12:28 +02:00
|
|
|
'bad'
|
2017-03-02 03:24:38 +01:00
|
|
|
);
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
// redirect back to the form, instead of using redirectBack() which could send the user elsewhere.
|
|
|
|
return $this->redirectBackToForm();
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$session = $this->getRequest()->getSession();
|
2017-03-02 03:24:38 +01:00
|
|
|
if (!$member) {
|
2017-06-22 12:50:45 +02:00
|
|
|
if ($session->get('AutoLoginHash')) {
|
|
|
|
$member = Member::member_from_autologinhash($session->get('AutoLoginHash'));
|
2017-03-02 03:24:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The user is not logged in and no valid auto login hash is available
|
|
|
|
if (!$member) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$session->clear('AutoLoginHash');
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the new password
|
|
|
|
if (empty($data['NewPassword1'])) {
|
2017-06-08 09:12:28 +02:00
|
|
|
$form->sessionMessage(
|
2017-05-30 09:42:00 +02:00
|
|
|
_t(
|
|
|
|
'SilverStripe\\Security\\Member.EMPTYNEWPASSWORD',
|
|
|
|
"The new password can't be empty, please try again"
|
|
|
|
),
|
2017-06-08 09:12:28 +02:00
|
|
|
'bad'
|
2017-03-02 03:24:38 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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']) {
|
2017-06-08 09:12:28 +02:00
|
|
|
$form->sessionMessage(
|
2017-05-30 09:42:00 +02:00
|
|
|
_t(
|
|
|
|
'SilverStripe\\Security\\Member.ERRORNEWPASSWORD',
|
2017-06-08 09:12:28 +02:00
|
|
|
'You have entered your new password differently, try again'
|
2017-05-30 09:42:00 +02:00
|
|
|
),
|
2017-06-08 09:12:28 +02:00
|
|
|
'bad'
|
2017-03-02 03:24:38 +01:00
|
|
|
);
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
// 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()) {
|
2017-06-08 09:12:28 +02:00
|
|
|
$form->setSessionValidationResult($validationResult);
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
return $this->redirectBackToForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear locked out status
|
|
|
|
$member->LockedOutUntil = null;
|
|
|
|
$member->FailedLoginCount = null;
|
2017-05-30 09:42:00 +02:00
|
|
|
// Clear the members login hashes
|
|
|
|
$member->AutoLoginHash = null;
|
|
|
|
$member->AutoLoginExpired = DBDatetime::create()->now();
|
2017-03-02 03:24:38 +01:00
|
|
|
$member->write();
|
|
|
|
|
2017-08-25 05:07:30 +02:00
|
|
|
if ($member->canLogin()) {
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var IdentityStore $identityStore */
|
|
|
|
$identityStore = Injector::inst()->get(IdentityStore::class);
|
|
|
|
$identityStore->logIn($member, false, $this->getRequest());
|
2017-03-02 03:24:38 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$session->clear('AutoLoginHash');
|
2017-03-02 03:24:38 +01:00
|
|
|
|
|
|
|
// Redirect to backurl
|
|
|
|
$backURL = $this->getBackURL();
|
2017-10-04 01:31:36 +02:00
|
|
|
if ($backURL
|
|
|
|
// Don't redirect back to itself
|
|
|
|
&& $backURL !== Security::singleton()->Link('changepassword')
|
|
|
|
) {
|
2017-03-02 03:24:38 +01:00
|
|
|
return $this->redirect($backURL);
|
|
|
|
}
|
|
|
|
|
2017-08-25 05:07:30 +02:00
|
|
|
$backURL = Security::config()->get('default_reset_password_dest');
|
|
|
|
if ($backURL) {
|
|
|
|
return $this->redirect($backURL);
|
|
|
|
}
|
2017-03-02 03:24:38 +01:00
|
|
|
// Redirect to default location - the login form saying "You are logged in as..."
|
|
|
|
$url = Security::singleton()->Link('login');
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
return $this->redirect($url);
|
|
|
|
}
|
|
|
|
|
2017-06-08 09:12:28 +02:00
|
|
|
/**
|
|
|
|
* Something went wrong, go back to the changepassword
|
|
|
|
*
|
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
2017-03-02 03:24:38 +01:00
|
|
|
public function redirectBackToForm()
|
|
|
|
{
|
|
|
|
// Redirect back to form
|
2017-06-08 09:12:28 +02:00
|
|
|
$url = $this->addBackURLParam(Security::singleton()->Link('changepassword'));
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
return $this->redirect($url);
|
|
|
|
}
|
2017-06-15 04:20:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if password is ok
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @param string $password
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function checkPassword($member, $password)
|
|
|
|
{
|
|
|
|
if (empty($password)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// With a valid user and password, check the password is correct
|
|
|
|
$authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::CHECK_PASSWORD);
|
|
|
|
foreach ($authenticators as $authenticator) {
|
|
|
|
if (!$authenticator->checkPassword($member, $password)->isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-02 03:24:38 +01:00
|
|
|
}
|