silverstripe-framework/src/Security/MemberAuthenticator/CMSMemberLoginForm.php

131 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Security\MemberAuthenticator;
2016-06-23 01:37:22 +02:00
use SilverStripe\Control\Controller;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\PasswordField;
use SilverStripe\Security\RememberLoginHash;
use SilverStripe\Security\Security;
2016-06-23 01:37:22 +02:00
/**
* Provides the in-cms session re-authentication form for the "member" authenticator
*/
class CMSMemberLoginForm extends MemberLoginForm
2016-11-29 00:31:16 +01:00
{
/**
* CMSMemberLoginForm constructor.
* @param RequestHandler $controller
* @param string $authenticatorClass
* @param FieldList $name
*/
public function __construct(RequestHandler $controller, $authenticatorClass, $name)
{
$this->controller = $controller;
$this->authenticator_class = $authenticatorClass;
$fields = $this->getFormFields();
$actions = $this->getFormActions();
parent::__construct($controller, $authenticatorClass, $name, $fields, $actions);
$this->addExtraClass('form--no-dividers');
}
/**
* @return FieldList
*/
public function getFormFields()
2016-11-29 00:31:16 +01:00
{
// Set default fields
$fields = FieldList::create([
2016-11-29 00:31:16 +01:00
HiddenField::create("AuthenticationMethod", null, $this->authenticator_class, $this),
HiddenField::create('tempid', null, $this->controller->getRequest()->requestVar('tempid')),
PasswordField::create("Password", _t('SilverStripe\\Security\\Member.PASSWORD', 'Password'))
]);
2016-11-29 00:31:16 +01:00
if (Security::config()->get('autologin_enabled')) {
$fields->insertAfter(
'Password',
CheckboxField::create(
"Remember",
_t('SilverStripe\\Security\\Member.KEEPMESIGNEDIN', "Keep me signed in")
)->setAttribute(
'title',
_t(
'SilverStripe\\Security\\Member.REMEMBERME',
"Remember me next time? (for {count} days on this device)",
[ 'count' => RememberLoginHash::config()->uninherited('token_expiry_days') ]
)
)
);
2016-11-29 00:31:16 +01:00
}
return $fields;
}
/**
* @return FieldList
*/
public function getFormActions()
{
2016-11-29 00:31:16 +01:00
// Determine returnurl to redirect to parent page
$logoutLink = $this->getExternalLink('logout');
if ($returnURL = $this->controller->getRequest()->requestVar('BackURL')) {
$logoutLink = Controller::join_links($logoutLink, '?BackURL=' . urlencode($returnURL));
2016-11-29 00:31:16 +01:00
}
// Make actions
$actions = FieldList::create([
FormAction::create('doLogin', _t(__CLASS__.'.BUTTONLOGIN', "Let me back in"))
->addExtraClass('btn-primary'),
2016-11-29 00:31:16 +01:00
LiteralField::create(
'doLogout',
sprintf(
'<a class="btn btn-secondary" href="%s" target="_top">%s</a>',
Convert::raw2att($logoutLink),
_t(__CLASS__.'.BUTTONLOGOUT', "Log out")
)
),
LiteralField::create(
'forgotPassword',
sprintf(
'<p class="cms-security__container__form__forgotPassword"><a href="%s" target="_top">%s</a></p>',
$this->getExternalLink('lostpassword'),
_t(__CLASS__.'.BUTTONFORGOTPASSWORD', "Forgot password")
2016-11-29 00:31:16 +01:00
)
)
]);
2016-11-29 00:31:16 +01:00
return $actions;
2016-11-29 00:31:16 +01:00
}
/**
* Get link to use for external security actions
*
* @param string $action Action
* @return string
*/
public function getExternalLink($action = null)
2016-11-29 00:31:16 +01:00
{
return Security::singleton()->Link($action);
2016-11-29 00:31:16 +01:00
}
/**
* @return string
*/
public function getAuthenticatorName()
{
return _t(__CLASS__.'.AUTHENTICATORNAME', 'CMS Member Login Form');
}
}