2008-09-26 04:22:51 +02:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
namespace SilverStripe\Security\MemberAuthenticator;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2017-04-30 05:17:26 +02:00
|
|
|
use SilverStripe\Control\RequestHandler;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Forms\CheckboxField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Forms\HiddenField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Forms\PasswordField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Forms\TextField;
|
2016-11-23 06:09:10 +01:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Security\LoginForm as BaseLoginForm;
|
2017-04-22 06:30:10 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\RememberLoginHash;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
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
|
2008-09-26 04:22:51 +02:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
class MemberLoginForm extends BaseLoginForm
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This field is used in the "You are logged in as %s" message
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $loggedInAsField = 'FirstName';
|
|
|
|
|
2017-04-14 06:21:38 +02:00
|
|
|
/**
|
|
|
|
* Required fields for validation
|
2017-06-09 05:07:35 +02:00
|
|
|
*
|
|
|
|
* @config
|
2017-04-14 06:21:38 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2017-06-09 05:07:35 +02:00
|
|
|
private static $required_fields = [
|
|
|
|
'Email',
|
|
|
|
'Password',
|
|
|
|
];
|
2017-04-14 06:21:38 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @skipUpgrade
|
2017-04-30 05:17:26 +02:00
|
|
|
* @param RequestHandler $controller The parent controller, necessary to
|
2016-11-29 00:31:16 +01:00
|
|
|
* create the appropriate form action tag.
|
2017-04-14 05:30:55 +02:00
|
|
|
* @param string $authenticatorClass Authenticator for this LoginForm
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $name The method on the controller that will return this
|
|
|
|
* form object.
|
|
|
|
* @param FieldList $fields All of the fields in the form - a
|
|
|
|
* {@link FieldList} of {@link FormField}
|
|
|
|
* objects.
|
|
|
|
* @param FieldList|FormAction $actions All of the action buttons in the
|
|
|
|
* form - a {@link FieldList} of
|
|
|
|
* {@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
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$controller,
|
2017-04-14 05:30:55 +02:00
|
|
|
$authenticatorClass,
|
2016-11-29 00:31:16 +01:00
|
|
|
$name,
|
|
|
|
$fields = null,
|
|
|
|
$actions = null,
|
|
|
|
$checkCurrentUser = true
|
|
|
|
) {
|
2017-06-15 07:25:23 +02:00
|
|
|
$this->setController($controller);
|
2017-04-14 05:30:55 +02:00
|
|
|
$this->authenticator_class = $authenticatorClass;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
$customCSS = project() . '/css/member_login.css';
|
|
|
|
if (Director::fileExists($customCSS)) {
|
|
|
|
Requirements::css($customCSS);
|
|
|
|
}
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
if ($checkCurrentUser && Security::getCurrentUser()) {
|
2017-05-20 06:32:25 +02:00
|
|
|
// @todo find a more elegant way to handle this
|
|
|
|
$logoutAction = Security::logout_url();
|
2016-11-29 00:31:16 +01:00
|
|
|
$fields = FieldList::create(
|
2017-06-08 09:12:28 +02:00
|
|
|
HiddenField::create('AuthenticationMethod', null, $this->authenticator_class, $this)
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
|
|
|
$actions = FieldList::create(
|
2017-06-08 09:12:28 +02:00
|
|
|
FormAction::create('logout', _t(
|
|
|
|
'SilverStripe\\Security\\Member.BUTTONLOGINOTHER',
|
|
|
|
'Log in as someone else'
|
|
|
|
))
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (!$fields) {
|
2017-04-14 06:21:38 +02:00
|
|
|
$fields = $this->getFormFields();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
if (!$actions) {
|
2017-04-14 06:21:38 +02:00
|
|
|
$actions = $this->getFormActions();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce attack surface by enforcing POST requests
|
|
|
|
$this->setFormMethod('POST', true);
|
|
|
|
|
|
|
|
parent::__construct($controller, $name, $fields, $actions);
|
|
|
|
|
2017-05-20 06:32:25 +02:00
|
|
|
if (isset($logoutAction)) {
|
|
|
|
$this->setFormAction($logoutAction);
|
|
|
|
}
|
2017-04-14 06:21:38 +02:00
|
|
|
$this->setValidator(RequiredFields::create(self::config()->get('required_fields')));
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-14 06:21:38 +02:00
|
|
|
/**
|
|
|
|
* Build the FieldList for the login form
|
|
|
|
*
|
2017-07-03 02:21:27 +02:00
|
|
|
* @skipUpgrade
|
2017-04-14 06:21:38 +02:00
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
protected function getFormFields()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$request = $this->getRequest();
|
2017-06-15 07:25:23 +02:00
|
|
|
if ($request->getVar('BackURL')) {
|
|
|
|
$backURL = $request->getVar('BackURL');
|
2017-06-08 09:12:28 +02:00
|
|
|
} else {
|
2017-06-22 12:50:45 +02:00
|
|
|
$backURL = $request->getSession()->get('BackURL');
|
2017-06-08 09:12:28 +02:00
|
|
|
}
|
|
|
|
|
2017-06-15 07:25:23 +02:00
|
|
|
$label = Member::singleton()->fieldLabel(Member::config()->get('unique_identifier_field'));
|
2017-04-14 06:21:38 +02:00
|
|
|
$fields = FieldList::create(
|
|
|
|
HiddenField::create("AuthenticationMethod", null, $this->authenticator_class, $this),
|
|
|
|
// Regardless of what the unique identifer field is (usually 'Email'), it will be held in the
|
|
|
|
// 'Email' value, below:
|
|
|
|
// @todo Rename the field to a more generic covering name
|
|
|
|
$emailField = TextField::create("Email", $label, null, null, $this),
|
2017-04-20 03:15:24 +02:00
|
|
|
PasswordField::create("Password", _t('SilverStripe\\Security\\Member.PASSWORD', 'Password'))
|
2017-04-14 06:21:38 +02:00
|
|
|
);
|
|
|
|
$emailField->setAttribute('autofocus', 'true');
|
|
|
|
|
2017-06-08 09:12:28 +02:00
|
|
|
if (Security::config()->get('remember_username')) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$emailField->setValue($this->getSession()->get('SessionForms.MemberLoginForm.Email'));
|
2017-04-14 06:21:38 +02:00
|
|
|
} else {
|
|
|
|
// Some browsers won't respect this attribute unless it's added to the form
|
|
|
|
$this->setAttribute('autocomplete', 'off');
|
|
|
|
$emailField->setAttribute('autocomplete', 'off');
|
|
|
|
}
|
2017-05-30 09:42:00 +02:00
|
|
|
if (Security::config()->get('autologin_enabled')) {
|
2017-04-14 06:21:38 +02:00
|
|
|
$fields->push(
|
|
|
|
CheckboxField::create(
|
|
|
|
"Remember",
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Security\\Member.KEEPMESIGNEDIN', "Keep me signed in")
|
2017-04-14 06:21:38 +02:00
|
|
|
)->setAttribute(
|
|
|
|
'title',
|
2017-05-31 07:48:16 +02:00
|
|
|
_t(
|
|
|
|
'SilverStripe\\Security\\Member.REMEMBERME',
|
|
|
|
"Remember me next time? (for {count} days on this device)",
|
|
|
|
[ 'count' => RememberLoginHash::config()->uninherited('token_expiry_days') ]
|
2017-04-14 06:21:38 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-08 09:12:28 +02:00
|
|
|
if (isset($backURL)) {
|
|
|
|
$fields->push(HiddenField::create('BackURL', 'BackURL', $backURL));
|
|
|
|
}
|
|
|
|
|
2017-04-14 06:21:38 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build default login form action FieldList
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
protected function getFormActions()
|
|
|
|
{
|
|
|
|
$actions = FieldList::create(
|
2017-04-22 06:30:10 +02:00
|
|
|
FormAction::create('doLogin', _t('SilverStripe\\Security\\Member.BUTTONLOGIN', "Log in")),
|
2017-04-14 06:21:38 +02:00
|
|
|
LiteralField::create(
|
|
|
|
'forgotPassword',
|
|
|
|
'<p id="ForgotPassword"><a href="' . Security::lost_password_url() . '">'
|
2017-04-20 03:15:24 +02:00
|
|
|
. _t('SilverStripe\\Security\\Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>'
|
2017-04-14 06:21:38 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $actions;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
public function restoreFormState()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
parent::restoreFormState();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$session = $this->getSession();
|
|
|
|
$forceMessage = $session->get('MemberLoginForm.force_message');
|
2017-05-20 06:32:25 +02:00
|
|
|
if (($member = Security::getCurrentUser()) && !$forceMessage) {
|
2016-11-23 06:09:10 +01:00
|
|
|
$message = _t(
|
2017-04-20 03:15:24 +02:00
|
|
|
'SilverStripe\\Security\\Member.LOGGEDINAS',
|
2016-11-29 00:31:16 +01:00
|
|
|
"You're logged in as {name}.",
|
|
|
|
array('name' => $member->{$this->loggedInAsField})
|
|
|
|
);
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->setMessage($message, ValidationResult::TYPE_INFO);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset forced message
|
|
|
|
if ($forceMessage) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$session->set('MemberLoginForm.force_message', false);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $this;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-14 05:30:55 +02:00
|
|
|
/**
|
|
|
|
* The name of this login form, to display in the frontend
|
|
|
|
* Replaces Authenticator::get_name()
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAuthenticatorName()
|
|
|
|
{
|
2017-02-04 20:41:31 +01:00
|
|
|
return _t(self::class . '.AUTHENTICATORNAME', "E-mail & Password");
|
2017-04-14 05:30:55 +02:00
|
|
|
}
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|