silverstripe-framework/Security/CMSSecurity.php

235 lines
6.0 KiB
PHP
Raw Normal View History

<?php
2016-06-23 01:37:22 +02:00
namespace SilverStripe\Security;
use Requirements;
use Controller;
use Director;
use Convert;
use Session;
use AdminRootController;
use SS_HTTPResponse;
/**
* Provides a security interface functionality within the cms
2015-08-24 06:15:38 +02:00
* @package framework
* @subpackage security
*/
class CMSSecurity extends Security {
private static $casting = array(
'Title' => 'HTMLFragment'
);
private static $allowed_actions = array(
2016-06-23 01:37:22 +02:00
'SilverStripe\\Security\\LoginForm',
'success'
);
/**
* Enable in-cms reauthentication
*
* @var boolean
* @config
*/
private static $reauth_enabled = true;
protected function init() {
parent::init();
// Include CMS styles and js
Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::css(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css');
Requirements::combine_files(
'cmssecurity.js',
array(
THIRDPARTY_DIR . '/jquery/jquery.js',
THIRDPARTY_DIR . '/jquery-ui/jquery-ui.js',
THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js',
FRAMEWORK_ADMIN_DIR . '/client/dist/js/sspath.js',
FRAMEWORK_ADMIN_DIR . '/client/dist/js/CMSSecurity.js'
)
);
}
public function Link($action = null) {
2016-06-23 01:37:22 +02:00
/** @skipUpgrade */
return Controller::join_links(Director::baseURL(), "CMSSecurity", $action);
}
/**
* Get known logged out member
*
* @return Member
*/
public function getTargetMember() {
if($tempid = $this->getRequest()->requestVar('tempid')) {
return Member::member_from_tempid($tempid);
}
}
public function getResponseController($title) {
// Use $this to prevent use of Page to render underlying templates
return $this;
}
2014-11-18 23:05:07 +01:00
protected function getLoginMessage(&$messageType = null) {
return parent::getLoginMessage($messageType)
?: _t(
'CMSSecurity.LoginMessage',
'<p>If you have any unsaved work you can return to where you left off by logging back in below.</p>'
);
}
public function getTitle() {
// Check if logged in already
if(Member::currentUserID()) {
return _t('CMSSecurity.SUCCESS', 'Success');
}
// Display logged-out message
$member = $this->getTargetMember();
if($member) {
return _t(
'CMSSecurity.TimedOutTitleMember',
'Hey {name}!<br />Your session has timed out.',
'Title for CMS popup login form for a known user',
array('name' => $member->FirstName)
);
} else {
return _t(
'CMSSecurity.TimedOutTitleAnonymous',
'Your session has timed out.',
'Title for CMS popup login form without a known user'
);
}
}
/**
* Redirects the user to the external login page
*
* @return SS_HTTPResponse
*/
protected function redirectToExternalLogin() {
$loginURL = Security::create()->Link('login');
$loginURLATT = Convert::raw2att($loginURL);
$loginURLJS = Convert::raw2js($loginURL);
$message = _t(
'CMSSecurity.INVALIDUSER',
'<p>Invalid user. <a target="_top" href="{link}">Please re-authenticate here</a> to continue.</p>',
'Message displayed to user if their session cannot be restored',
array('link' => $loginURLATT)
);
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setBody(<<<PHP
<!DOCTYPE html>
<html><body>
$message
2016-01-24 06:12:45 +01:00
<script type="application/javascript">
setTimeout(function(){top.location.href = "$loginURLJS";}, 0);
</script>
</body></html>
PHP
);
$this->setResponse($response);
return $response;
}
protected function preLogin() {
// If no member has been previously logged in for this session, force a redirect to the main login page
if(!$this->getTargetMember()) {
return $this->redirectToExternalLogin();
}
return parent::preLogin();
}
public function GetLoginForms() {
$forms = array();
$authenticators = Authenticator::get_authenticators();
foreach($authenticators as $authenticator) {
// Get only CMS-supporting authenticators
if($authenticator::supports_cms()) {
$forms[] = $authenticator::get_cms_login_form($this);
}
}
return $forms;
}
/**
* Determine if CMSSecurity is enabled
*
* @return bool
*/
public static function enabled() {
// Disable shortcut
if(!static::config()->reauth_enabled) return false;
// Count all cms-supported methods
$authenticators = Authenticator::get_authenticators();
foreach($authenticators as $authenticator) {
// Supported if at least one authenticator is supported
if($authenticator::supports_cms()) return true;
}
return false;
}
public function LoginForm() {
$authenticator = $this->getAuthenticator();
if($authenticator && $authenticator::supports_cms()) {
return $authenticator::get_cms_login_form($this);
}
user_error('Passed invalid authentication method', E_USER_ERROR);
}
2014-11-18 23:05:07 +01:00
public function getTemplatesFor($action) {
2016-06-23 01:37:22 +02:00
return array("CMSSecurity_{$action}", "SilverStripe\\Security\\CMSSecurity")
+ parent::getTemplatesFor($action);
}
2014-11-18 23:05:07 +01:00
public function getIncludeTemplate($name) {
Merge remote-tracking branch 'origin/3.1' Conflicts: .travis.yml README.md admin/code/LeftAndMain.php admin/css/screen.css admin/scss/screen.scss api/RestfulService.php conf/ConfigureFromEnv.php control/injector/ServiceConfigurationLocator.php control/injector/SilverStripeServiceConfigurationLocator.php core/ClassInfo.php core/Object.php css/AssetUploadField.css css/ComplexTableField_popup.css dev/CSSContentParser.php dev/DevelopmentAdmin.php docs/en/changelogs/index.md docs/en/misc/contributing/code.md docs/en/reference/execution-pipeline.md filesystem/GD.php filesystem/ImagickBackend.php filesystem/Upload.php forms/Form.php forms/FormField.php forms/HtmlEditorConfig.php forms/gridfield/GridFieldDetailForm.php forms/gridfield/GridFieldSortableHeader.php lang/en.yml model/Aggregate.php model/DataList.php model/DataObject.php model/DataQuery.php model/Image.php model/MySQLDatabase.php model/SQLQuery.php model/fieldtypes/HTMLText.php model/fieldtypes/Text.php scss/AssetUploadField.scss search/filters/SearchFilter.php security/Authenticator.php security/LoginForm.php security/Member.php security/MemberAuthenticator.php security/MemberLoginForm.php security/Security.php tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php tests/control/HTTPTest.php tests/control/RequestHandlingTest.php tests/filesystem/UploadTest.php tests/forms/FormTest.php tests/forms/NumericFieldTest.php tests/model/DataListTest.php tests/model/DataObjectTest.php tests/model/TextTest.php tests/security/MemberAuthenticatorTest.php tests/security/SecurityDefaultAdminTest.php tests/view/SSViewerCacheBlockTest.php tests/view/SSViewerTest.php
2014-11-18 00:45:54 +01:00
return array("CMSSecurity_{$name}")
+ parent::getIncludeTemplate($name);
}
/**
* Given a successful login, tell the parent frame to close the dialog
*
* @return SS_HTTPResponse
*/
public function success() {
// Ensure member is properly logged in
if(!Member::currentUserID()) {
return $this->redirectToExternalLogin();
}
// Get redirect url
$controller = $this->getResponseController(_t('CMSSecurity.SUCCESS', 'Success'));
$backURLs = array(
$this->getRequest()->requestVar('BackURL'),
Session::get('BackURL'),
Director::absoluteURL(AdminRootController::config()->url_base, true),
);
foreach ($backURLs as $backURL) {
if ($backURL && Director::is_site_url($backURL)) {
break;
}
}
// Show login
$controller = $controller->customise(array(
'Content' => _t(
'CMSSecurity.SUCCESSCONTENT',
'<p>Login success. If you are not automatically redirected '.
'<a target="_top" href="{link}">click here</a></p>',
'Login message displayed in the cms popup once a user has re-authenticated themselves',
array('link' => $backURL)
)
));
return $controller->renderWith($this->getTemplatesFor('success'));
}
}