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

136 lines
4.4 KiB
PHP
Raw Permalink 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->setAuthenticatorClass($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([
HiddenField::create("AuthenticationMethod", null, $this->getAuthenticatorClass(), $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.KEEP_ME_SIGNED_IN',
'Keep me signed in for {count} days',
[ 'count' => RememberLoginHash::config()->uninherited('token_expiry_days') ]
)
)
2021-04-08 02:32:12 +02:00
->setAttribute(
'title',
_t(
'SilverStripe\\Security\\Member.KEEP_ME_SIGNED_IN_TOOLTIP',
'You will remain authenticated on this device for {count} days. Only use this feature if you trust the device you are using.',
2021-04-08 02:32:12 +02:00
['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')) {
2022-04-14 03:12:59 +02:00
$logoutLink = Controller::join_links($logoutLink, '?BackURL=' . urlencode($returnURL ?? ''));
2016-11-29 00:31:16 +01:00
}
// Make actions
$actions = FieldList::create([
2018-01-16 19:39:30 +01:00
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),
2018-01-16 19:39:30 +01:00
_t(__CLASS__ . '.BUTTONLOGOUT', "Log out")
)
),
LiteralField::create(
'forgotPassword',
sprintf(
'<a href="%s" class="cms-security__container__form__forgotPassword btn btn-secondary" target="_top">%s</a>',
$this->getExternalLink('lostpassword'),
2018-01-16 19:39:30 +01:00
_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()
{
2018-01-16 19:39:30 +01:00
return _t(__CLASS__ . '.AUTHENTICATORNAME', 'CMS Member Login Form');
}
}