2008-09-26 04:22:51 +02:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
|
|
|
use Director;
|
|
|
|
use Requirements;
|
|
|
|
use Session;
|
|
|
|
use FieldList;
|
|
|
|
use HiddenField;
|
|
|
|
use FormAction;
|
|
|
|
use SS_HTTPResponse;
|
|
|
|
use TextField;
|
|
|
|
use PasswordField;
|
|
|
|
use CheckboxField;
|
|
|
|
use Config;
|
|
|
|
use LiteralField;
|
|
|
|
use RequiredFields;
|
|
|
|
use Controller;
|
|
|
|
use Convert;
|
|
|
|
use Email;
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
/**
|
2014-02-24 06:45:18 +01:00
|
|
|
* Log-in form for the "member" authentication method.
|
|
|
|
*
|
|
|
|
* Available extension points:
|
2014-04-29 23:12:23 +02:00
|
|
|
* - "authenticationFailed": Called when login was not successful.
|
2014-02-24 06:45:18 +01:00
|
|
|
* Arguments: $data containing the form submission
|
2014-04-29 23:12:23 +02:00
|
|
|
* - "forgotPassword": Called before forgot password logic kicks in,
|
|
|
|
* allowing extensions to "veto" execution by returning FALSE.
|
2014-02-24 06:45:18 +01:00
|
|
|
* Arguments: $member containing the detected Member record
|
2014-04-29 23:12:23 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-09-26 04:22:51 +02:00
|
|
|
* @subpackage security
|
|
|
|
*/
|
|
|
|
class MemberLoginForm extends LoginForm {
|
|
|
|
|
2010-10-19 07:05:23 +02:00
|
|
|
/**
|
|
|
|
* This field is used in the "You are logged in as %s" message
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $loggedInAsField = 'FirstName';
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
protected $authenticator_class = 'SilverStripe\\Security\\MemberAuthenticator';
|
2014-04-29 23:12:23 +02:00
|
|
|
|
2013-10-03 06:10:56 +02:00
|
|
|
/**
|
|
|
|
* Since the logout and dologin actions may be conditionally removed, it's necessary to ensure these
|
|
|
|
* remain valid actions regardless of the member login state.
|
|
|
|
*
|
|
|
|
* @var array
|
2014-04-29 23:12:23 +02:00
|
|
|
* @config
|
2013-10-03 06:10:56 +02:00
|
|
|
*/
|
|
|
|
private static $allowed_actions = array('dologin', 'logout');
|
2014-04-29 23:12:23 +02:00
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param Controller $controller The parent controller, necessary to
|
|
|
|
* create the appropriate form action tag.
|
|
|
|
* @param string $name The method on the controller that will return this
|
|
|
|
* form object.
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param FieldList $fields All of the fields in the form - a
|
2011-10-28 03:37:27 +02:00
|
|
|
* {@link FieldList} of {@link FormField}
|
2008-09-26 04:22:51 +02:00
|
|
|
* objects.
|
2011-10-28 03:37:27 +02:00
|
|
|
* @param FieldList|FormAction $actions All of the action buttons in the
|
|
|
|
* form - a {@link FieldList} of
|
2008-09-26 04:22:51 +02:00
|
|
|
* {@link FormAction} objects
|
|
|
|
* @param bool $checkCurrentUser If set to TRUE, it will be checked if a
|
|
|
|
* the user is currently logged in, and if
|
|
|
|
* so, only a logout button will be rendered
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($controller, $name, $fields = null, $actions = null,
|
2012-12-08 12:20:20 +01:00
|
|
|
$checkCurrentUser = true) {
|
2008-09-26 04:22:51 +02:00
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
// This is now set on the class directly to make it easier to create subclasses
|
|
|
|
// $this->authenticator_class = $authenticatorClassName;
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
$customCSS = project() . '/css/member_login.css';
|
|
|
|
if(Director::fileExists($customCSS)) {
|
|
|
|
Requirements::css($customCSS);
|
|
|
|
}
|
2014-04-29 23:12:23 +02:00
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
if(isset($_REQUEST['BackURL'])) {
|
|
|
|
$backURL = $_REQUEST['BackURL'];
|
|
|
|
} else {
|
|
|
|
$backURL = Session::get('BackURL');
|
|
|
|
}
|
|
|
|
|
2010-04-13 05:18:26 +02:00
|
|
|
if($checkCurrentUser && Member::currentUser() && Member::logged_in_session_exists()) {
|
2016-06-10 23:50:38 +02:00
|
|
|
$fields = FieldList::create(
|
|
|
|
HiddenField::create("AuthenticationMethod", null, $this->authenticator_class, $this)
|
2009-04-29 02:07:39 +02:00
|
|
|
);
|
2016-06-10 23:50:38 +02:00
|
|
|
$actions = FieldList::create(
|
|
|
|
FormAction::create("logout", _t('Member.BUTTONLOGINOTHER', "Log in as someone else"))
|
2009-04-29 02:07:39 +02:00
|
|
|
);
|
2008-09-26 04:22:51 +02:00
|
|
|
} else {
|
|
|
|
if(!$fields) {
|
2016-06-23 01:37:22 +02:00
|
|
|
$label=singleton('SilverStripe\\Security\\Member')->fieldLabel(Member::config()->unique_identifier_field);
|
2016-06-10 23:50:38 +02:00
|
|
|
$fields = FieldList::create(
|
|
|
|
HiddenField::create("AuthenticationMethod", null, $this->authenticator_class, $this),
|
2012-09-26 23:34:00 +02:00
|
|
|
// Regardless of what the unique identifer field is (usually 'Email'), it will be held in the
|
|
|
|
// 'Email' value, below:
|
2016-06-10 23:50:38 +02:00
|
|
|
$emailField = TextField::create("Email", $label, null, null, $this),
|
|
|
|
PasswordField::create("Password", _t('Member.PASSWORD', 'Password'))
|
2008-09-26 04:22:51 +02:00
|
|
|
);
|
2014-04-09 01:41:25 +02:00
|
|
|
if(Security::config()->remember_username) {
|
|
|
|
$emailField->setValue(Session::get('SessionForms.MemberLoginForm.Email'));
|
|
|
|
} else {
|
|
|
|
// Some browsers won't respect this attribute unless it's added to the form
|
|
|
|
$this->setAttribute('autocomplete', 'off');
|
|
|
|
$emailField->setAttribute('autocomplete', 'off');
|
|
|
|
}
|
2013-03-21 19:48:54 +01:00
|
|
|
if(Security::config()->autologin_enabled) {
|
2016-02-04 23:21:06 +01:00
|
|
|
$fields->push(
|
|
|
|
CheckboxField::create(
|
|
|
|
"Remember",
|
|
|
|
_t('Member.KEEPMESIGNEDIN', "Keep me signed in")
|
|
|
|
)->setAttribute(
|
|
|
|
'title',
|
|
|
|
sprintf(
|
|
|
|
_t('Member.REMEMBERME', "Remember me next time? (for %d days on this device)"),
|
2016-06-23 01:37:22 +02:00
|
|
|
Config::inst()->get('SilverStripe\\Security\\RememberLoginHash', 'token_expiry_days')
|
2016-02-04 23:21:06 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
if(!$actions) {
|
2016-06-10 23:50:38 +02:00
|
|
|
$actions = FieldList::create(
|
|
|
|
FormAction::create('dologin', _t('Member.BUTTONLOGIN', "Log in")),
|
|
|
|
LiteralField::create(
|
2009-01-10 12:35:50 +01:00
|
|
|
'forgotPassword',
|
2012-09-26 23:34:00 +02:00
|
|
|
'<p id="ForgotPassword"><a href="Security/lostpassword">'
|
|
|
|
. _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>'
|
2009-01-10 12:35:50 +01:00
|
|
|
)
|
2008-09-26 04:22:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($backURL)) {
|
2016-06-10 23:50:38 +02:00
|
|
|
$fields->push(HiddenField::create('BackURL', 'BackURL', $backURL));
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
|
2013-05-08 10:22:52 +02:00
|
|
|
// Reduce attack surface by enforcing POST requests
|
|
|
|
$this->setFormMethod('POST', true);
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
parent::__construct($controller, $name, $fields, $actions);
|
2009-03-10 23:08:52 +01:00
|
|
|
|
2016-06-10 23:50:38 +02:00
|
|
|
$this->setValidator(RequiredFields::create('Email', 'Password'));
|
2013-08-09 02:59:52 +02:00
|
|
|
|
2009-03-10 23:08:52 +01:00
|
|
|
// Focus on the email input when the page is loaded
|
2014-04-08 01:25:27 +02:00
|
|
|
$js = <<<JS
|
2012-03-09 00:12:33 +01:00
|
|
|
(function() {
|
|
|
|
var el = document.getElementById("MemberLoginForm_LoginForm_Email");
|
2014-04-29 23:12:23 +02:00
|
|
|
if(el && el.focus && (typeof jQuery == 'undefined' || jQuery(el).is(':visible'))) el.focus();
|
2012-03-09 00:12:33 +01:00
|
|
|
})();
|
2014-04-08 01:25:27 +02:00
|
|
|
JS;
|
|
|
|
Requirements::customScript($js, 'MemberLoginFormFieldFocus');
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get message from session
|
|
|
|
*/
|
|
|
|
protected function getMessageFromSession() {
|
2014-06-03 13:20:54 +02:00
|
|
|
|
|
|
|
$forceMessage = Session::get('MemberLoginForm.force_message');
|
|
|
|
if(($member = Member::currentUser()) && !$forceMessage) {
|
2012-05-01 21:44:54 +02:00
|
|
|
$this->message = _t(
|
2014-04-29 23:12:23 +02:00
|
|
|
'Member.LOGGEDINAS',
|
|
|
|
"You're logged in as {name}.",
|
2012-05-01 21:44:54 +02:00
|
|
|
array('name' => $member->{$this->loggedInAsField})
|
|
|
|
);
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
2014-06-03 13:20:54 +02:00
|
|
|
|
|
|
|
// Reset forced message
|
|
|
|
if($forceMessage) {
|
|
|
|
Session::set('MemberLoginForm.force_message', false);
|
|
|
|
}
|
2014-01-30 22:38:34 +01:00
|
|
|
|
2014-11-18 00:45:54 +01:00
|
|
|
return parent::getMessageFromSession();
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Login form handler method
|
|
|
|
*
|
|
|
|
* This method is called when the user clicks on "Log in"
|
|
|
|
*
|
|
|
|
* @param array $data Submitted data
|
|
|
|
*/
|
|
|
|
public function dologin($data) {
|
|
|
|
if($this->performLogin($data)) {
|
2011-10-27 22:38:29 +02:00
|
|
|
$this->logInUserAndRedirect($data);
|
2008-09-26 04:22:51 +02:00
|
|
|
} else {
|
2011-10-27 22:38:29 +02:00
|
|
|
if(array_key_exists('Email', $data)){
|
|
|
|
Session::set('SessionForms.MemberLoginForm.Email', $data['Email']);
|
|
|
|
Session::set('SessionForms.MemberLoginForm.Remember', isset($data['Remember']));
|
|
|
|
}
|
2008-11-10 04:51:35 +01:00
|
|
|
|
2014-04-29 23:12:23 +02:00
|
|
|
if(isset($_REQUEST['BackURL'])) $backURL = $_REQUEST['BackURL'];
|
|
|
|
else $backURL = null;
|
2008-11-10 04:51:35 +01:00
|
|
|
|
2012-04-12 02:09:39 +02:00
|
|
|
if($backURL) Session::set('BackURL', $backURL);
|
2014-04-29 23:12:23 +02:00
|
|
|
|
2013-02-05 22:49:06 +01:00
|
|
|
// Show the right tab on failed login
|
|
|
|
$loginLink = Director::absoluteURL($this->controller->Link('login'));
|
|
|
|
if($backURL) $loginLink .= '?BackURL=' . urlencode($backURL);
|
|
|
|
$this->controller->redirect($loginLink . '#' . $this->FormName() .'_tab');
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 22:38:29 +02:00
|
|
|
/**
|
|
|
|
* Login in the user and figure out where to redirect the browser.
|
|
|
|
*
|
|
|
|
* The $data has this format
|
|
|
|
* array(
|
|
|
|
* 'AuthenticationMethod' => 'MemberAuthenticator',
|
|
|
|
* 'Email' => 'sam@silverstripe.com',
|
|
|
|
* 'Password' => '1nitialPassword',
|
|
|
|
* 'BackURL' => 'test/link',
|
|
|
|
* [Optional: 'Remember' => 1 ]
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param array $data
|
2015-05-25 02:38:34 +02:00
|
|
|
* @return SS_HTTPResponse
|
2011-10-27 22:38:29 +02:00
|
|
|
*/
|
|
|
|
protected function logInUserAndRedirect($data) {
|
|
|
|
Session::clear('SessionForms.MemberLoginForm.Email');
|
|
|
|
Session::clear('SessionForms.MemberLoginForm.Remember');
|
|
|
|
|
|
|
|
if(Member::currentUser()->isPasswordExpired()) {
|
|
|
|
if(isset($_REQUEST['BackURL']) && $backURL = $_REQUEST['BackURL']) {
|
|
|
|
Session::set('BackURL', $backURL);
|
|
|
|
}
|
2016-06-23 01:37:22 +02:00
|
|
|
$cp = ChangePasswordForm::create($this->controller, 'SilverStripe\\Security\\ChangePasswordForm');
|
2014-10-06 05:01:33 +02:00
|
|
|
$cp->sessionMessage(
|
|
|
|
_t('Member.PASSWORDEXPIRED', 'Your password has expired. Please choose a new one.'),
|
|
|
|
'good'
|
|
|
|
);
|
2011-10-29 06:06:19 +02:00
|
|
|
return $this->controller->redirect('Security/changepassword');
|
2011-10-27 22:38:29 +02:00
|
|
|
}
|
2014-04-29 23:12:23 +02:00
|
|
|
|
2011-10-27 22:38:29 +02:00
|
|
|
// Absolute redirection URLs may cause spoofing
|
2015-05-25 02:38:34 +02:00
|
|
|
if(!empty($_REQUEST['BackURL'])) {
|
|
|
|
$url = $_REQUEST['BackURL'];
|
|
|
|
if(Director::is_site_url($url) ) {
|
|
|
|
$url = Director::absoluteURL($url);
|
|
|
|
} else {
|
|
|
|
// Spoofing attack, redirect to homepage instead of spoofing url
|
|
|
|
$url = Director::absoluteBaseURL();
|
|
|
|
}
|
|
|
|
return $this->controller->redirect($url);
|
2011-10-27 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a default login dest has been set, redirect to that.
|
2015-05-25 02:38:34 +02:00
|
|
|
if ($url = Security::config()->default_login_dest) {
|
|
|
|
$url = Controller::join_links(Director::absoluteBaseURL(), $url);
|
|
|
|
return $this->controller->redirect($url);
|
2011-10-27 22:38:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-07 13:32:04 +01:00
|
|
|
// Redirect the user to the page where they came from
|
2011-10-27 22:38:29 +02:00
|
|
|
$member = Member::currentUser();
|
|
|
|
if($member) {
|
|
|
|
$firstname = Convert::raw2xml($member->FirstName);
|
|
|
|
if(!empty($data['Remember'])) {
|
|
|
|
Session::set('SessionForms.MemberLoginForm.Remember', '1');
|
|
|
|
$member->logIn(true);
|
|
|
|
} else {
|
|
|
|
$member->logIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::set('Security.Message.message',
|
2012-05-01 21:44:54 +02:00
|
|
|
_t('Member.WELCOMEBACK', "Welcome Back, {firstname}", array('firstname' => $firstname))
|
2011-10-27 22:38:29 +02:00
|
|
|
);
|
|
|
|
Session::set("Security.Message.type", "good");
|
|
|
|
}
|
|
|
|
Controller::curr()->redirectBack();
|
|
|
|
}
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log out form handler method
|
|
|
|
*
|
|
|
|
* This method is called when the user clicks on "logout" on the form
|
|
|
|
* created when the parameter <i>$checkCurrentUser</i> of the
|
|
|
|
* {@link __construct constructor} was set to TRUE and the user was
|
|
|
|
* currently logged in.
|
|
|
|
*/
|
|
|
|
public function logout() {
|
|
|
|
$s = new Security();
|
|
|
|
$s->logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* Try to authenticate the user
|
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param array $data Submitted data
|
2012-12-08 12:20:20 +01:00
|
|
|
* @return Member Returns the member object on successful authentication
|
|
|
|
* or NULL on failure.
|
|
|
|
*/
|
2008-09-26 04:22:51 +02:00
|
|
|
public function performLogin($data) {
|
2010-10-13 06:04:32 +02:00
|
|
|
$member = call_user_func_array(array($this->authenticator_class, 'authenticate'), array($data, $this));
|
|
|
|
if($member) {
|
2008-09-26 04:22:51 +02:00
|
|
|
$member->LogIn(isset($data['Remember']));
|
|
|
|
return $member;
|
|
|
|
} else {
|
|
|
|
$this->extend('authenticationFailed', $data);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-02-24 06:45:18 +01:00
|
|
|
* Forgot password form handler method.
|
|
|
|
* Called when the user clicks on "I've lost my password".
|
|
|
|
* Extensions can use the 'forgotPassword' method to veto executing
|
|
|
|
* the logic, by returning FALSE. In this case, the user will be redirected back
|
|
|
|
* to the form without further action. It is recommended to set a message
|
|
|
|
* in the form detailing why the action was denied.
|
2008-09-26 04:22:51 +02:00
|
|
|
*
|
|
|
|
* @param array $data Submitted data
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return SS_HTTPResponse
|
2008-09-26 04:22:51 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function forgotPassword($data) {
|
2013-06-21 00:32:08 +02:00
|
|
|
// Ensure password is given
|
|
|
|
if(empty($data['Email'])) {
|
|
|
|
$this->sessionMessage(
|
|
|
|
_t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'),
|
|
|
|
'bad'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->controller->redirect('Security/lostpassword');
|
|
|
|
return;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Find existing member
|
|
|
|
$member = Member::get()->filter("Email", $data['Email'])->first();
|
2008-09-26 04:22:51 +02:00
|
|
|
|
2014-02-24 06:45:18 +01:00
|
|
|
// Allow vetoing forgot password requests
|
|
|
|
$results = $this->extend('forgotPassword', $member);
|
|
|
|
if($results && is_array($results) && in_array(false, $results, true)) {
|
|
|
|
return $this->controller->redirect('Security/lostpassword');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 13:29:43 +02:00
|
|
|
if($member) {
|
2012-11-08 04:33:19 +01:00
|
|
|
$token = $member->generateAutologinTokenAndStoreHash();
|
2008-09-26 04:22:51 +02:00
|
|
|
|
2016-04-04 11:48:56 +02:00
|
|
|
/** @var Email $e */
|
|
|
|
$e = Email::create();
|
|
|
|
$e->setSubject(_t('Member.SUBJECTPASSWORDRESET', "Your password reset link", 'Email subject'));
|
|
|
|
$e->setTemplate('ForgotPasswordEmail');
|
2012-05-05 00:03:17 +02:00
|
|
|
$e->populateTemplate($member);
|
|
|
|
$e->populateTemplate(array(
|
2012-11-08 04:33:19 +01:00
|
|
|
'PasswordResetLink' => Security::getPasswordResetLink($member, $token)
|
2012-09-19 12:07:39 +02:00
|
|
|
));
|
|
|
|
$e->setTo($member->Email);
|
2012-05-05 00:03:17 +02:00
|
|
|
$e->send();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-29 06:06:19 +02:00
|
|
|
$this->controller->redirect('Security/passwordsent/' . urlencode($data['Email']));
|
2008-10-16 13:29:43 +02:00
|
|
|
} elseif($data['Email']) {
|
2013-06-21 00:32:08 +02:00
|
|
|
// Avoid information disclosure by displaying the same status,
|
|
|
|
// regardless wether the email address actually exists
|
2013-04-05 11:14:45 +02:00
|
|
|
$this->controller->redirect('Security/passwordsent/' . rawurlencode($data['Email']));
|
2008-09-26 04:22:51 +02:00
|
|
|
} else {
|
2008-10-16 13:29:43 +02:00
|
|
|
$this->sessionMessage(
|
|
|
|
_t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'),
|
|
|
|
'bad'
|
|
|
|
);
|
2014-04-29 23:12:23 +02:00
|
|
|
|
2011-10-29 06:06:19 +02:00
|
|
|
$this->controller->redirect('Security/lostpassword');
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|