2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
2017-04-14 05:30:55 +02:00
|
|
|
use LogicException;
|
2017-06-09 05:07:35 +02:00
|
|
|
use Page;
|
2017-06-15 12:54:41 +02:00
|
|
|
use ReflectionClass;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\CMS\Controllers\ModelAsController;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-04-22 06:30:10 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
2017-01-03 08:37:17 +01:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2016-10-06 06:31:38 +02:00
|
|
|
use SilverStripe\Dev\TestOnly;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\Form;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2017-01-03 08:37:17 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-07-13 09:08:09 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-11-23 06:09:10 +01:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
use SilverStripe\View\TemplateGlobalProvider;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Implements a basic security model
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class Security extends Controller implements TemplateGlobalProvider
|
|
|
|
{
|
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index',
|
|
|
|
'login',
|
|
|
|
'logout',
|
|
|
|
'basicauthlogin',
|
|
|
|
'lostpassword',
|
|
|
|
'passwordsent',
|
|
|
|
'changepassword',
|
|
|
|
'ping',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If set to TRUE to prevent sharing of the session across several sites
|
|
|
|
* in the domain.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
private static $strict_path_checking = false;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The password encryption algorithm to use by default.
|
|
|
|
* This is an arbitrary code registered through {@link PasswordEncryptor}.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $password_encryption_algorithm = 'blowfish';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Showing "Remember me"-checkbox
|
|
|
|
* on loginform, and saving encrypted credentials to a cookie.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $autologin_enabled = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if login username may be remembered between login sessions
|
2017-05-30 09:42:00 +02:00
|
|
|
* If set to false this will disable auto-complete and prevent username persisting in the session
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $remember_username = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Location of word list to use for generating passwords
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $word_list = './wordlist.txt';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $template = 'BlankPage';
|
|
|
|
|
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* Template that is used to render the pages.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $template_main = 'Page';
|
|
|
|
|
2017-01-12 21:16:13 +01:00
|
|
|
/**
|
|
|
|
* Class to use for page rendering
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @config
|
|
|
|
*/
|
2017-03-02 03:24:38 +01:00
|
|
|
private static $page_class = Page::class;
|
2017-01-12 21:16:13 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Default message set used in permission failures.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array|string
|
|
|
|
*/
|
|
|
|
private static $default_message_set;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default login URL
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
private static $login_url = 'Security/login';
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default logout URL
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
private static $logout_url = 'Security/logout';
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default lost password URL
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
private static $lost_password_url = 'Security/lostpassword';
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Value of X-Frame-Options header
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $frame_options = 'SAMEORIGIN';
|
|
|
|
|
2017-01-17 13:23:41 +01:00
|
|
|
/**
|
|
|
|
* Value of the X-Robots-Tag header (for the Security section)
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $robots_tag = 'noindex, nofollow';
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Enable or disable recording of login attempts
|
|
|
|
* through the {@link LoginRecord} object.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var boolean $login_recording
|
|
|
|
*/
|
|
|
|
private static $login_recording = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean If set to TRUE or FALSE, {@link database_is_ready()}
|
|
|
|
* will always return FALSE. Used for unit testing.
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
protected static $force_database_is_ready;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When the database has once been verified as ready, it will not do the
|
|
|
|
* checks again.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2017-04-22 06:30:10 +02:00
|
|
|
protected static $database_is_ready = false;
|
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @var Authenticator[] available authenticators
|
2017-04-23 05:30:33 +02:00
|
|
|
*/
|
2017-06-08 09:12:28 +02:00
|
|
|
private $authenticators = [];
|
2017-04-22 06:30:10 +02:00
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @var Member Currently logged in user (if available)
|
2017-04-23 05:30:33 +02:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
protected static $currentUser;
|
2017-04-22 06:30:10 +02:00
|
|
|
|
2017-05-20 06:32:25 +02:00
|
|
|
/**
|
2017-06-09 05:07:35 +02:00
|
|
|
* @return Authenticator[]
|
2017-05-20 06:32:25 +02:00
|
|
|
*/
|
2017-06-08 09:12:28 +02:00
|
|
|
public function getAuthenticators()
|
2017-05-30 09:42:00 +02:00
|
|
|
{
|
2017-06-08 09:12:28 +02:00
|
|
|
return $this->authenticators;
|
2017-05-30 09:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-09 05:07:35 +02:00
|
|
|
* @param Authenticator[] $authenticators
|
2017-05-30 09:42:00 +02:00
|
|
|
*/
|
2017-06-08 09:12:28 +02:00
|
|
|
public function setAuthenticators(array $authenticators)
|
2017-05-30 09:42:00 +02:00
|
|
|
{
|
2017-06-08 09:12:28 +02:00
|
|
|
$this->authenticators = $authenticators;
|
2017-05-30 09:42:00 +02:00
|
|
|
}
|
2017-05-20 06:32:25 +02:00
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
protected function init()
|
2017-04-22 06:30:10 +02:00
|
|
|
{
|
2017-04-23 05:30:33 +02:00
|
|
|
parent::init();
|
2017-04-22 06:30:10 +02:00
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
// Prevent clickjacking, see https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options
|
2017-04-30 05:17:26 +02:00
|
|
|
$frameOptions = static::config()->get('frame_options');
|
2017-04-23 05:30:33 +02:00
|
|
|
if ($frameOptions) {
|
|
|
|
$this->getResponse()->addHeader('X-Frame-Options', $frameOptions);
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
// Prevent search engines from indexing the login page
|
2017-04-30 05:17:26 +02:00
|
|
|
$robotsTag = static::config()->get('robots_tag');
|
2017-04-23 05:30:33 +02:00
|
|
|
if ($robotsTag) {
|
|
|
|
$this->getResponse()->addHeader('X-Robots-Tag', $robotsTag);
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
2017-04-23 05:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2017-05-31 07:48:16 +02:00
|
|
|
$this->httpError(404); // no-op
|
2017-04-23 05:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the selected authenticator for this request
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param string $name The identifier of the authenticator in your config
|
2017-05-20 06:32:25 +02:00
|
|
|
* @return Authenticator Class name of Authenticator
|
2017-04-23 05:30:33 +02:00
|
|
|
* @throws LogicException
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
protected function getAuthenticator($name = 'default')
|
2017-04-23 05:30:33 +02:00
|
|
|
{
|
2017-06-08 09:12:28 +02:00
|
|
|
$authenticators = $this->authenticators;
|
2017-04-23 05:30:33 +02:00
|
|
|
|
|
|
|
if (isset($authenticators[$name])) {
|
2017-05-30 09:42:00 +02:00
|
|
|
return $authenticators[$name];
|
2017-04-23 05:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new LogicException('No valid authenticator found');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all registered authenticators
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param int $service The type of service that is requested
|
2017-06-09 05:07:35 +02:00
|
|
|
* @return Authenticator[] Return an array of Authenticator objects
|
2017-04-23 05:30:33 +02:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
public function getApplicableAuthenticators($service = Authenticator::LOGIN)
|
2017-04-23 05:30:33 +02:00
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
$authenticators = $this->getAuthenticators();
|
2017-04-22 06:30:10 +02:00
|
|
|
|
2017-06-09 05:07:35 +02:00
|
|
|
/** @var Authenticator $authenticator */
|
|
|
|
foreach ($authenticators as $name => $authenticator) {
|
|
|
|
if (!($authenticator->supportedServices() & $service)) {
|
2017-04-30 05:17:26 +02:00
|
|
|
unset($authenticators[$name]);
|
|
|
|
}
|
|
|
|
}
|
2017-05-20 06:32:25 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
if (empty($authenticators)) {
|
|
|
|
throw new LogicException('No applicable authenticators found');
|
|
|
|
}
|
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
return $authenticators;
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a given authenticator is registered
|
|
|
|
*
|
2017-04-23 05:30:33 +02:00
|
|
|
* @param string $authenticator The configured identifier of the authenicator
|
2017-04-22 06:30:10 +02:00
|
|
|
* @return bool Returns TRUE if the authenticator is registered, FALSE
|
|
|
|
* otherwise.
|
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
public function hasAuthenticator($authenticator)
|
2017-04-22 06:30:10 +02:00
|
|
|
{
|
2017-06-08 09:12:28 +02:00
|
|
|
$authenticators = $this->authenticators;
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
return !empty($authenticators[$authenticator]);
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register that we've had a permission failure trying to view the given page
|
|
|
|
*
|
|
|
|
* This will redirect to a login page.
|
|
|
|
* If you don't provide a messageSet, a default will be used.
|
|
|
|
*
|
|
|
|
* @param Controller $controller The controller that you were on to cause the permission
|
|
|
|
* failure.
|
|
|
|
* @param string|array $messageSet The message to show to the user. This
|
|
|
|
* can be a string, or a map of different
|
|
|
|
* messages for different contexts.
|
|
|
|
* If you pass an array, you can use the
|
|
|
|
* following keys:
|
|
|
|
* - default: The default message
|
|
|
|
* - alreadyLoggedIn: The message to
|
|
|
|
* show if the user
|
|
|
|
* is already logged
|
|
|
|
* in and lacks the
|
|
|
|
* permission to
|
|
|
|
* access the item.
|
|
|
|
*
|
|
|
|
* The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link
|
|
|
|
* to log in.
|
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
|
|
|
public static function permissionFailure($controller = null, $messageSet = null)
|
|
|
|
{
|
|
|
|
self::set_ignore_disallowed_actions(true);
|
2017-12-14 02:49:58 +01:00
|
|
|
|
|
|
|
// Parse raw message / escape type
|
|
|
|
$parseMessage = function ($message) {
|
2017-12-14 01:50:52 +01:00
|
|
|
if ($message instanceof DBField) {
|
2017-12-14 02:49:58 +01:00
|
|
|
return [
|
|
|
|
$message->getValue(),
|
|
|
|
$message->config()->get('escape_type') === 'raw'
|
|
|
|
? ValidationResult::CAST_TEXT
|
|
|
|
: ValidationResult::CAST_HTML,
|
|
|
|
];
|
2017-12-13 05:10:16 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 02:49:58 +01:00
|
|
|
// Default to escaped value
|
|
|
|
return [
|
|
|
|
$message,
|
|
|
|
ValidationResult::CAST_TEXT,
|
|
|
|
];
|
2017-12-13 05:10:16 +01:00
|
|
|
};
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
if (!$controller && Controller::has_curr()) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$controller = Controller::curr();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Director::is_ajax()) {
|
|
|
|
$response = ($controller) ? $controller->getResponse() : new HTTPResponse();
|
|
|
|
$response->setStatusCode(403);
|
2017-05-20 06:32:25 +02:00
|
|
|
if (!static::getCurrentUser()) {
|
2017-05-30 09:42:00 +02:00
|
|
|
$response->setBody(
|
|
|
|
_t('SilverStripe\\CMS\\Controllers\\ContentController.NOTLOGGEDIN', 'Not logged in')
|
|
|
|
);
|
|
|
|
$response->setStatusDescription(
|
|
|
|
_t('SilverStripe\\CMS\\Controllers\\ContentController.NOTLOGGEDIN', 'Not logged in')
|
|
|
|
);
|
|
|
|
// Tell the CMS to allow re-authentication
|
2017-06-10 04:46:01 +02:00
|
|
|
if (CMSSecurity::singleton()->enabled()) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$response->addHeader('X-Reauthenticate', '1');
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the messageSet provided
|
|
|
|
if (!$messageSet) {
|
|
|
|
if ($configMessageSet = static::config()->get('default_message_set')) {
|
|
|
|
$messageSet = $configMessageSet;
|
|
|
|
} else {
|
|
|
|
$messageSet = array(
|
2017-02-04 20:41:31 +01:00
|
|
|
'default' => _t(
|
|
|
|
__CLASS__ . '.NOTEPAGESECURED',
|
2016-11-29 00:31:16 +01:00
|
|
|
"That page is secured. Enter your credentials below and we will send "
|
2017-02-04 20:41:31 +01:00
|
|
|
. "you right along."
|
2016-11-29 00:31:16 +01:00
|
|
|
),
|
|
|
|
'alreadyLoggedIn' => _t(
|
2017-02-04 20:41:31 +01:00
|
|
|
__CLASS__ . '.ALREADYLOGGEDIN',
|
2016-11-29 00:31:16 +01:00
|
|
|
"You don't have access to this page. If you have another account that "
|
2017-02-04 20:41:31 +01:00
|
|
|
. "can access that page, you can log in again below."
|
2016-11-29 00:31:16 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($messageSet)) {
|
|
|
|
$messageSet = array('default' => $messageSet);
|
|
|
|
}
|
|
|
|
|
2017-05-20 06:32:25 +02:00
|
|
|
$member = static::getCurrentUser();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// Work out the right message to show
|
|
|
|
if ($member && $member->exists()) {
|
|
|
|
$response = ($controller) ? $controller->getResponse() : new HTTPResponse();
|
|
|
|
$response->setStatusCode(403);
|
|
|
|
|
|
|
|
//If 'alreadyLoggedIn' is not specified in the array, then use the default
|
|
|
|
//which should have been specified in the lines above
|
|
|
|
if (isset($messageSet['alreadyLoggedIn'])) {
|
|
|
|
$message = $messageSet['alreadyLoggedIn'];
|
|
|
|
} else {
|
|
|
|
$message = $messageSet['default'];
|
|
|
|
}
|
|
|
|
|
2017-12-14 02:49:58 +01:00
|
|
|
list($messageText, $messageCast) = $parseMessage($message);
|
|
|
|
static::singleton()->setSessionMessage($messageText, ValidationResult::TYPE_WARNING, $messageCast);
|
2017-06-22 12:50:45 +02:00
|
|
|
$request = new HTTPRequest('GET', '/');
|
|
|
|
if ($controller) {
|
|
|
|
$request->setSession($controller->getRequest()->getSession());
|
|
|
|
}
|
|
|
|
$loginResponse = static::singleton()->login($request);
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($loginResponse instanceof HTTPResponse) {
|
|
|
|
return $loginResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->setBody((string)$loginResponse);
|
|
|
|
|
|
|
|
$controller->extend('permissionDenied', $member);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
} else {
|
|
|
|
$message = $messageSet['default'];
|
|
|
|
}
|
|
|
|
|
2017-12-14 02:49:58 +01:00
|
|
|
list($messageText, $messageCast) = $parseMessage($message);
|
|
|
|
static::singleton()->setSessionMessage($messageText, ValidationResult::TYPE_WARNING, $messageCast);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$controller->getRequest()->getSession()->set("BackURL", $_SERVER['REQUEST_URI']);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// TODO AccessLogEntry needs an extension to handle permission denied errors
|
|
|
|
// Audit logging hook
|
|
|
|
$controller->extend('permissionDenied', $member);
|
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return $controller->redirect(Controller::join_links(
|
2017-02-22 04:14:53 +01:00
|
|
|
Security::config()->uninherited('login_url'),
|
2016-11-23 06:09:10 +01:00
|
|
|
"?BackURL=" . urlencode($_SERVER['REQUEST_URI'])
|
|
|
|
));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
/**
|
|
|
|
* @param null|Member $currentUser
|
|
|
|
*/
|
|
|
|
public static function setCurrentUser($currentUser = null)
|
2017-05-07 21:11:00 +02:00
|
|
|
{
|
|
|
|
self::$currentUser = $currentUser;
|
|
|
|
}
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
/**
|
|
|
|
* @return null|Member
|
|
|
|
*/
|
2017-05-07 21:11:00 +02:00
|
|
|
public static function getCurrentUser()
|
|
|
|
{
|
|
|
|
return self::$currentUser;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Get the login forms for all available authentication methods
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @deprecated 5.0.0 Now handled by {@link static::delegateToMultipleHandlers}
|
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return array Returns an array of available login forms (array of Form
|
|
|
|
* objects).
|
|
|
|
*
|
|
|
|
*/
|
2017-04-22 06:30:10 +02:00
|
|
|
public function getLoginForms()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Now handled by delegateToMultipleHandlers');
|
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
return array_map(
|
2017-06-09 05:07:35 +02:00
|
|
|
function (Authenticator $authenticator) {
|
|
|
|
return [
|
|
|
|
$authenticator->getLoginHandler($this->Link())->loginForm()
|
|
|
|
];
|
2017-04-22 06:30:10 +02:00
|
|
|
},
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->getApplicableAuthenticators()
|
2017-04-22 06:30:10 +02:00
|
|
|
);
|
2017-08-02 04:08:59 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a link to a security action
|
|
|
|
*
|
|
|
|
* @param string $action Name of the action
|
|
|
|
* @return string Returns the link to the given action
|
|
|
|
*/
|
|
|
|
public function Link($action = null)
|
|
|
|
{
|
|
|
|
/** @skipUpgrade */
|
|
|
|
return Controller::join_links(Director::baseURL(), "Security", $action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This action is available as a keep alive, so user
|
|
|
|
* sessions don't timeout. A common use is in the admin.
|
|
|
|
*/
|
|
|
|
public function ping()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform pre-login checking and prepare a response if available prior to login
|
|
|
|
*
|
|
|
|
* @return HTTPResponse Substitute response object if the login process should be curcumvented.
|
|
|
|
* Returns null if should proceed as normal.
|
|
|
|
*/
|
|
|
|
protected function preLogin()
|
|
|
|
{
|
|
|
|
// Event handler for pre-login, with an option to let it break you out of the login form
|
|
|
|
$eventResults = $this->extend('onBeforeSecurityLogin');
|
|
|
|
// If there was a redirection, return
|
|
|
|
if ($this->redirectedTo()) {
|
|
|
|
return $this->getResponse();
|
|
|
|
}
|
|
|
|
// If there was an HTTPResponse object returned, then return that
|
|
|
|
if ($eventResults) {
|
|
|
|
foreach ($eventResults as $result) {
|
|
|
|
if ($result instanceof HTTPResponse) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If arriving on the login page already logged in, with no security error, and a ReturnURL then redirect
|
|
|
|
// back. The login message check is neccesary to prevent infinite loops where BackURL links to
|
|
|
|
// an action that triggers Security::permissionFailure.
|
|
|
|
// This step is necessary in cases such as automatic redirection where a user is authenticated
|
|
|
|
// upon landing on an SSL secured site and is automatically logged in, or some other case
|
|
|
|
// where the user has permissions to continue but is not given the option.
|
2017-06-15 12:54:41 +02:00
|
|
|
if (!$this->getSessionMessage()
|
2017-05-20 06:32:25 +02:00
|
|
|
&& ($member = static::getCurrentUser())
|
2016-11-29 00:31:16 +01:00
|
|
|
&& $member->exists()
|
2017-05-30 09:42:00 +02:00
|
|
|
&& $this->getRequest()->requestVar('BackURL')
|
2016-11-29 00:31:16 +01:00
|
|
|
) {
|
|
|
|
return $this->redirectBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public function getRequest()
|
|
|
|
{
|
|
|
|
// Support Security::singleton() where a request isn't always injected
|
|
|
|
$request = parent::getRequest();
|
|
|
|
if ($request) {
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Controller::has_curr() && Controller::curr() !== $this) {
|
|
|
|
return Controller::curr()->getRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Prepare the controller for handling the response to this request
|
|
|
|
*
|
|
|
|
* @param string $title Title to use
|
|
|
|
* @return Controller
|
|
|
|
*/
|
|
|
|
protected function getResponseController($title)
|
|
|
|
{
|
2017-03-02 03:24:38 +01:00
|
|
|
// Use the default setting for which Page to use to render the security page
|
2017-08-22 23:42:10 +02:00
|
|
|
$pageClass = $this->config()->get('page_class');
|
2017-03-02 03:24:38 +01:00
|
|
|
if (!$pageClass || !class_exists($pageClass)) {
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
// Create new instance of page holder
|
|
|
|
/** @var Page $holderPage */
|
2017-06-09 05:07:35 +02:00
|
|
|
$holderPage = Injector::inst()->create($pageClass);
|
2017-03-02 03:24:38 +01:00
|
|
|
$holderPage->Title = $title;
|
2016-11-29 00:31:16 +01:00
|
|
|
/** @skipUpgrade */
|
2017-03-02 03:24:38 +01:00
|
|
|
$holderPage->URLSegment = 'Security';
|
2016-11-29 00:31:16 +01:00
|
|
|
// Disable ID-based caching of the log-in page by making it a random number
|
2017-05-30 09:42:00 +02:00
|
|
|
$holderPage->ID = -1 * random_int(1, 10000000);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-09 05:07:35 +02:00
|
|
|
$controller = ModelAsController::controller_for($holderPage);
|
2016-11-29 00:31:16 +01:00
|
|
|
$controller->doInit();
|
2017-08-03 05:52:31 +02:00
|
|
|
$controller->setRequest($this->getRequest());
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combine the given forms into a formset with a tabbed interface
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param array|Form[] $forms
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-06-15 12:54:41 +02:00
|
|
|
protected function generateTabbedFormSet($forms)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-31 07:48:16 +02:00
|
|
|
if (count($forms) === 1) {
|
|
|
|
return $forms;
|
|
|
|
}
|
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
$viewData = new ArrayData([
|
2016-11-29 00:31:16 +01:00
|
|
|
'Forms' => new ArrayList($forms),
|
2017-06-15 12:54:41 +02:00
|
|
|
]);
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return $viewData->renderWith(
|
2017-06-15 12:54:41 +02:00
|
|
|
$this->getTemplatesFor('MultiAuthenticatorTabbedForms')
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the HTML Content for the $Content area during login
|
|
|
|
*
|
|
|
|
* @param string &$messageType Type of message, if available, passed back to caller
|
|
|
|
* @return string Message in HTML format
|
|
|
|
*/
|
2017-06-15 12:54:41 +02:00
|
|
|
protected function getSessionMessage(&$messageType = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$session = $this->getRequest()->getSession();
|
|
|
|
$message = $session->get('Security.Message.message');
|
2016-11-29 00:31:16 +01:00
|
|
|
$messageType = null;
|
|
|
|
if (empty($message)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$messageType = $session->get('Security.Message.type');
|
|
|
|
$messageCast = $session->get('Security.Message.cast');
|
2016-11-23 06:09:10 +01:00
|
|
|
if ($messageCast !== ValidationResult::CAST_HTML) {
|
|
|
|
$message = Convert::raw2xml($message);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
return sprintf('<p class="message %s">%s</p>', Convert::raw2att($messageType), $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the next message to display for the security login page. Defaults to warning
|
|
|
|
*
|
|
|
|
* @param string $message Message
|
|
|
|
* @param string $messageType Message type. One of ValidationResult::TYPE_*
|
|
|
|
* @param string $messageCast Message cast. One of ValidationResult::CAST_*
|
|
|
|
*/
|
2017-06-15 12:54:41 +02:00
|
|
|
public function setSessionMessage(
|
2016-11-23 06:09:10 +01:00
|
|
|
$message,
|
|
|
|
$messageType = ValidationResult::TYPE_WARNING,
|
|
|
|
$messageCast = ValidationResult::CAST_TEXT
|
|
|
|
) {
|
2017-06-22 12:50:45 +02:00
|
|
|
Controller::curr()
|
|
|
|
->getRequest()
|
|
|
|
->getSession()
|
|
|
|
->set("Security.Message.message", $message)
|
|
|
|
->set("Security.Message.type", $messageType)
|
|
|
|
->set("Security.Message.cast", $messageCast);
|
2016-11-23 06:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear login message
|
|
|
|
*/
|
2017-06-15 12:54:41 +02:00
|
|
|
public static function clearSessionMessage()
|
2016-11-23 06:09:10 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
Controller::curr()
|
|
|
|
->getRequest()
|
|
|
|
->getSession()
|
|
|
|
->clear("Security.Message");
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the "login" page
|
|
|
|
*
|
|
|
|
* For multiple authenticators, Security_MultiAuthenticatorLogin is used.
|
|
|
|
* See getTemplatesFor and getIncludeTemplate for how to override template logic
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param null|HTTPRequest $request
|
|
|
|
* @param int $service
|
2017-04-23 05:30:33 +02:00
|
|
|
* @return HTTPResponse|string Returns the "login" page as HTML code.
|
2017-07-04 04:18:47 +02:00
|
|
|
* @throws HTTPResponse_Exception
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
public function login($request = null, $service = Authenticator::LOGIN)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
if ($request) {
|
|
|
|
$this->setRequest($request);
|
|
|
|
} elseif ($request) {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
} else {
|
|
|
|
throw new HTTPResponse_Exception("No request available", 500);
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Check pre-login process
|
|
|
|
if ($response = $this->preLogin()) {
|
|
|
|
return $response;
|
|
|
|
}
|
2017-05-30 09:42:00 +02:00
|
|
|
$authName = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
$handlers = $this->getServiceAuthenticatorsFromRequest($service, $request);
|
2017-05-30 09:42:00 +02:00
|
|
|
|
|
|
|
$link = $this->Link('login');
|
2017-06-15 12:54:41 +02:00
|
|
|
array_walk(
|
|
|
|
$handlers,
|
|
|
|
function (Authenticator &$auth, $name) use ($link) {
|
|
|
|
$auth = $auth->getLoginHandler(Controller::join_links($link, $name));
|
2017-08-02 04:08:59 +02:00
|
|
|
}
|
2017-06-15 12:54:41 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return $this->delegateToMultipleHandlers(
|
|
|
|
$handlers,
|
2017-07-04 04:18:47 +02:00
|
|
|
_t(__CLASS__.'.LOGIN', 'Log in'),
|
2017-06-15 12:54:41 +02:00
|
|
|
$this->getTemplatesFor('login'),
|
|
|
|
[$this, 'aggregateTabbedForms']
|
2017-08-02 04:08:59 +02:00
|
|
|
);
|
|
|
|
}
|
2017-06-15 12:54:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the currently logged in user out
|
|
|
|
*
|
|
|
|
* Logging out without ID-parameter in the URL, will log the user out of all applicable Authenticators.
|
|
|
|
*
|
|
|
|
* Adding an ID will only log the user out of that Authentication method.
|
|
|
|
*
|
|
|
|
* @param null|HTTPRequest $request
|
|
|
|
* @param int $service
|
|
|
|
* @return HTTPResponse|string
|
|
|
|
*/
|
|
|
|
public function logout($request = null, $service = Authenticator::LOGOUT)
|
|
|
|
{
|
|
|
|
$authName = null;
|
|
|
|
|
|
|
|
if (!$request) {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
$handlers = $this->getServiceAuthenticatorsFromRequest($service, $request);
|
|
|
|
|
|
|
|
$link = $this->Link('logout');
|
|
|
|
array_walk(
|
|
|
|
$handlers,
|
|
|
|
function (Authenticator &$auth, $name) use ($link) {
|
|
|
|
$auth = $auth->getLogoutHandler(Controller::join_links($link, $name));
|
|
|
|
}
|
|
|
|
);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
return $this->delegateToMultipleHandlers(
|
|
|
|
$handlers,
|
2017-07-04 04:18:47 +02:00
|
|
|
_t(__CLASS__.'.LOGOUT', 'Log out'),
|
2017-06-15 12:54:41 +02:00
|
|
|
$this->getTemplatesFor('logout'),
|
2017-07-13 23:20:58 +02:00
|
|
|
[$this, 'aggregateAuthenticatorResponses']
|
2017-06-15 12:54:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get authenticators for the given service, optionally filtered by the ID parameter
|
|
|
|
* of the current request
|
|
|
|
*
|
|
|
|
* @param int $service
|
|
|
|
* @param HTTPRequest $request
|
2017-07-13 23:20:58 +02:00
|
|
|
* @return array|Authenticator[]
|
2017-06-15 12:54:41 +02:00
|
|
|
* @throws HTTPResponse_Exception
|
|
|
|
*/
|
|
|
|
protected function getServiceAuthenticatorsFromRequest($service, HTTPRequest $request)
|
|
|
|
{
|
|
|
|
$authName = null;
|
|
|
|
|
|
|
|
if ($request->param('ID')) {
|
|
|
|
$authName = $request->param('ID');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delegate to a single named handler - e.g. Security/login/<authname>/
|
2017-06-09 05:07:35 +02:00
|
|
|
if ($authName && $this->hasAuthenticator($authName)) {
|
2017-05-30 09:42:00 +02:00
|
|
|
if ($request) {
|
|
|
|
$request->shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
$authenticator = $this->getAuthenticator($authName);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
if (!$authenticator->supportedServices() & $service) {
|
2017-06-15 12:54:41 +02:00
|
|
|
// Try to be helpful and show the service constant name, e.g. Authenticator::LOGIN
|
|
|
|
$constants = array_flip((new ReflectionClass(Authenticator::class))->getConstants());
|
|
|
|
|
|
|
|
$message = 'Invalid Authenticator "' . $authName . '" for ';
|
|
|
|
if (array_key_exists($service, $constants)) {
|
|
|
|
$message .= 'service: Authenticator::' . $constants[$service];
|
|
|
|
} else {
|
|
|
|
$message .= 'unknown authenticator service';
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new HTTPResponse_Exception($message, 400);
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
$handlers = [$authName => $authenticator];
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2017-06-15 12:54:41 +02:00
|
|
|
// Delegate to all of them, building a tabbed view - e.g. Security/login/
|
2017-05-30 09:42:00 +02:00
|
|
|
$handlers = $this->getApplicableAuthenticators($service);
|
2017-04-23 05:30:33 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
return $handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Aggregate tabbed forms from each handler to fragments ready to be rendered.
|
|
|
|
*
|
2017-07-03 02:21:27 +02:00
|
|
|
* @skipUpgrade
|
2017-06-15 12:54:41 +02:00
|
|
|
* @param array $results
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function aggregateTabbedForms(array $results)
|
|
|
|
{
|
|
|
|
$forms = [];
|
|
|
|
foreach ($results as $authName => $singleResult) {
|
|
|
|
// The result *must* be an array with a Form key
|
|
|
|
if (!is_array($singleResult) || !isset($singleResult['Form'])) {
|
|
|
|
user_error('Authenticator "' . $authName . '" doesn\'t support tabbed forms', E_USER_WARNING);
|
|
|
|
continue;
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
$forms[] = $singleResult['Form'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$forms) {
|
|
|
|
throw new \LogicException('No authenticators found compatible with tabbed forms');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'Forms' => ArrayList::create($forms),
|
|
|
|
'Form' => $this->generateTabbedFormSet($forms)
|
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-07-13 23:20:58 +02:00
|
|
|
/**
|
|
|
|
* We have three possible scenarios.
|
|
|
|
* We get back Content (e.g. Password Reset)
|
|
|
|
* We get back a Form (no token set for logout)
|
|
|
|
* We get back a HTTPResponse, telling us to redirect.
|
|
|
|
* Return the first one, which is the default response, as that covers all required scenarios
|
|
|
|
*
|
|
|
|
* @param array $results
|
|
|
|
* @return array|HTTPResponse
|
|
|
|
*/
|
|
|
|
protected function aggregateAuthenticatorResponses($results)
|
|
|
|
{
|
|
|
|
$error = false;
|
|
|
|
$result = null;
|
|
|
|
foreach ($results as $authName => $singleResult) {
|
|
|
|
if (($singleResult instanceof HTTPResponse) ||
|
|
|
|
(is_array($singleResult) &&
|
|
|
|
(isset($singleResult['Content']) || isset($singleResult['Form'])))
|
|
|
|
) {
|
|
|
|
// return the first successful response
|
|
|
|
return $singleResult;
|
|
|
|
} else {
|
|
|
|
// Not a valid response
|
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
throw new \LogicException('No authenticators found compatible with logout operation');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
2017-06-15 12:54:41 +02:00
|
|
|
* Delegate to a number of handlers and aggregate the results. This is used, for example, to
|
|
|
|
* build the log-in page where there are multiple authenticators active.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-04-23 05:30:33 +02:00
|
|
|
* If a single handler is passed, delegateToHandler() will be called instead
|
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param array|RequestHandler[] $handlers
|
2017-04-22 06:30:10 +02:00
|
|
|
* @param string $title The title of the form
|
|
|
|
* @param array $templates
|
2017-06-15 12:54:41 +02:00
|
|
|
* @param callable $aggregator
|
2017-05-30 09:42:00 +02:00
|
|
|
* @return array|HTTPResponse|RequestHandler|DBHTMLText|string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-15 12:54:41 +02:00
|
|
|
protected function delegateToMultipleHandlers(array $handlers, $title, array $templates, callable $aggregator)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
// Simpler case for a single authenticator
|
|
|
|
if (count($handlers) === 1) {
|
|
|
|
return $this->delegateToHandler(array_values($handlers)[0], $title, $templates);
|
|
|
|
}
|
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
// Process each of the handlers
|
|
|
|
$results = array_map(
|
2017-06-09 05:07:35 +02:00
|
|
|
function (RequestHandler $handler) {
|
2017-06-22 12:50:45 +02:00
|
|
|
return $handler->handleRequest($this->getRequest());
|
2017-04-22 06:30:10 +02:00
|
|
|
},
|
|
|
|
$handlers
|
|
|
|
);
|
|
|
|
|
2017-07-13 23:20:58 +02:00
|
|
|
$response = call_user_func_array($aggregator, [$results]);
|
|
|
|
// The return could be a HTTPResponse, in which we don't want to call the render
|
|
|
|
if (is_array($response)) {
|
|
|
|
return $this->renderWrappedController($title, $response, $templates);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-22 06:30:10 +02:00
|
|
|
* Delegate to another RequestHandler, rendering any fragment arrays into an appropriate.
|
|
|
|
* controller.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param RequestHandler $handler
|
2017-04-22 06:30:10 +02:00
|
|
|
* @param string $title The title of the form
|
|
|
|
* @param array $templates
|
2017-05-30 09:42:00 +02:00
|
|
|
* @return array|HTTPResponse|RequestHandler|DBHTMLText|string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
protected function delegateToHandler(RequestHandler $handler, $title, array $templates = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$result = $handler->handleRequest($this->getRequest());
|
2017-04-22 06:30:10 +02:00
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
// Return the customised controller - may be used to render a Form (e.g. login form)
|
2017-04-22 06:30:10 +02:00
|
|
|
if (is_array($result)) {
|
|
|
|
$result = $this->renderWrappedController($title, $result, $templates);
|
2017-08-02 04:08:59 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-22 06:30:10 +02:00
|
|
|
* Render the given fragments into a security page controller with the given title.
|
2017-06-15 12:54:41 +02:00
|
|
|
*
|
2017-04-23 05:30:33 +02:00
|
|
|
* @param string $title string The title to give the security page
|
|
|
|
* @param array $fragments A map of objects to render into the page, e.g. "Form"
|
|
|
|
* @param array $templates An array of templates to use for the render
|
2017-05-30 09:42:00 +02:00
|
|
|
* @return HTTPResponse|DBHTMLText
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-04-22 06:30:10 +02:00
|
|
|
protected function renderWrappedController($title, array $fragments, array $templates)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-04-22 06:30:10 +02:00
|
|
|
$controller = $this->getResponseController($title);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// if the controller calls Director::redirect(), this will break early
|
|
|
|
if (($response = $controller->getResponse()) && $response->isFinished()) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
// Handle any form messages from validation, etc.
|
|
|
|
$messageType = '';
|
2017-06-15 12:54:41 +02:00
|
|
|
$message = $this->getSessionMessage($messageType);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
// We've displayed the message in the form output, so reset it for the next run.
|
2017-06-15 12:54:41 +02:00
|
|
|
static::clearSessionMessage();
|
2017-04-22 06:30:10 +02:00
|
|
|
|
|
|
|
if ($message) {
|
|
|
|
$messageResult = [
|
|
|
|
'Content' => DBField::create_field('HTMLFragment', $message),
|
|
|
|
'Message' => DBField::create_field('HTMLFragment', $message),
|
|
|
|
'MessageType' => $messageType
|
|
|
|
];
|
2017-04-23 05:30:33 +02:00
|
|
|
$fragments = array_merge($fragments, $messageResult);
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
return $controller->customise($fragments)->renderWith($templates);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-22 06:30:10 +02:00
|
|
|
public function basicauthlogin()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
$member = BasicAuth::requireLogin($this->getRequest(), 'SilverStripe login', 'ADMIN');
|
2017-05-20 06:32:25 +02:00
|
|
|
static::setCurrentUser($member);
|
2017-04-22 06:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the "lost password" page
|
|
|
|
*
|
|
|
|
* @return string Returns the "lost password" page as HTML code.
|
|
|
|
*/
|
|
|
|
public function lostpassword()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
$handlers = [];
|
|
|
|
$authenticators = $this->getApplicableAuthenticators(Authenticator::RESET_PASSWORD);
|
|
|
|
/** @var Authenticator $authenticator */
|
|
|
|
foreach ($authenticators as $authenticator) {
|
|
|
|
$handlers[] = $authenticator->getLostPasswordHandler(
|
|
|
|
Controller::join_links($this->Link(), 'lostpassword')
|
|
|
|
);
|
|
|
|
}
|
2017-04-22 06:30:10 +02:00
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
return $this->delegateToMultipleHandlers(
|
|
|
|
$handlers,
|
2017-04-22 06:30:10 +02:00
|
|
|
_t('SilverStripe\\Security\\Security.LOSTPASSWORDHEADER', 'Lost Password'),
|
2017-06-15 12:54:41 +02:00
|
|
|
$this->getTemplatesFor('lostpassword'),
|
2017-07-13 23:20:58 +02:00
|
|
|
[$this, 'aggregateAuthenticatorResponses']
|
2017-04-22 06:30:10 +02:00
|
|
|
);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the "change password" page.
|
|
|
|
* This page can either be called directly by logged-in users
|
|
|
|
* (in which case they need to provide their old password),
|
|
|
|
* or through a link emailed through {@link lostpassword()}.
|
|
|
|
* In this case no old password is required, authentication is ensured
|
|
|
|
* through the Member.AutoLoginHash property.
|
|
|
|
*
|
|
|
|
* @see ChangePasswordForm
|
|
|
|
*
|
|
|
|
* @return string|HTTPRequest Returns the "change password" page as HTML code, or a redirect response
|
|
|
|
*/
|
|
|
|
public function changepassword()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
/** @var array|Authenticator[] $authenticators */
|
|
|
|
$authenticators = $this->getApplicableAuthenticators(Authenticator::CHANGE_PASSWORD);
|
|
|
|
$handlers = [];
|
|
|
|
foreach ($authenticators as $authenticator) {
|
|
|
|
$handlers[] = $authenticator->getChangePasswordHandler($this->Link('changepassword'));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
return $this->delegateToMultipleHandlers(
|
|
|
|
$handlers,
|
|
|
|
_t('SilverStripe\\Security\\Security.CHANGEPASSWORDHEADER', 'Change your password'),
|
2017-06-15 12:54:41 +02:00
|
|
|
$this->getTemplatesFor('changepassword'),
|
2017-07-13 23:20:58 +02:00
|
|
|
[$this, 'aggregateAuthenticatorResponses']
|
2017-08-02 04:08:59 +02:00
|
|
|
);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
/**
|
|
|
|
* Create a link to the password reset form.
|
|
|
|
*
|
|
|
|
* GET parameters used:
|
|
|
|
* - m: member ID
|
|
|
|
* - t: plaintext token
|
|
|
|
*
|
|
|
|
* @param Member $member Member object associated with this link.
|
|
|
|
* @param string $autologinToken The auto login token.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getPasswordResetLink($member, $autologinToken)
|
|
|
|
{
|
|
|
|
$autologinToken = urldecode($autologinToken);
|
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
return static::singleton()->Link('changepassword') . "?m={$member->ID}&t=$autologinToken";
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the list of templates to use for rendering the given action.
|
|
|
|
*
|
|
|
|
* @skipUpgrade
|
|
|
|
* @param string $action
|
|
|
|
* @return array Template list
|
|
|
|
*/
|
|
|
|
public function getTemplatesFor($action)
|
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
$templates = SSViewer::get_templates_by_class(static::class, "_{$action}", __CLASS__);
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return array_merge(
|
|
|
|
$templates,
|
|
|
|
[
|
|
|
|
"Security_{$action}",
|
|
|
|
"Security",
|
2017-08-22 23:42:10 +02:00
|
|
|
$this->config()->get("template_main"),
|
2016-11-29 00:31:16 +01:00
|
|
|
"BlankPage"
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an existing member with administrator privileges, or create one of necessary.
|
|
|
|
*
|
|
|
|
* Will create a default 'Administrators' group if no group is found
|
|
|
|
* with an ADMIN permission. Will create a new 'Admin' member with administrative permissions
|
|
|
|
* if no existing Member with these permissions is found.
|
|
|
|
*
|
|
|
|
* Important: Any newly created administrator accounts will NOT have valid
|
|
|
|
* login credentials (Email/Password properties), which means they can't be used for login
|
|
|
|
* purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}.
|
|
|
|
*
|
|
|
|
* @return Member
|
2017-06-13 11:04:43 +02:00
|
|
|
*
|
2017-06-15 04:20:12 +02:00
|
|
|
* @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::findOrCreateDefaultAdmin()
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function findAnAdministrator()
|
|
|
|
{
|
2017-06-13 11:04:43 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Please use DefaultAdminService::findOrCreateDefaultAdmin()');
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$service = DefaultAdminService::singleton();
|
2017-06-13 11:04:43 +02:00
|
|
|
return $service->findOrCreateDefaultAdmin();
|
2017-08-02 04:08:59 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush the default admin credentials
|
2017-06-13 11:04:43 +02:00
|
|
|
*
|
2017-06-15 04:20:12 +02:00
|
|
|
* @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::clearDefaultAdmin()
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function clear_default_admin()
|
|
|
|
{
|
2017-06-13 11:04:43 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Please use DefaultAdminService::clearDefaultAdmin()');
|
|
|
|
|
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a default admin in dev-mode
|
|
|
|
*
|
|
|
|
* This will set a static default-admin which is not existing
|
|
|
|
* as a database-record. By this workaround we can test pages in dev-mode
|
|
|
|
* with a unified login. Submitted login-credentials are first checked
|
|
|
|
* against this static information in {@link Security::authenticate()}.
|
|
|
|
*
|
|
|
|
* @param string $username The user name
|
|
|
|
* @param string $password The password (in cleartext)
|
|
|
|
* @return bool True if successfully set
|
2017-06-13 11:04:43 +02:00
|
|
|
*
|
2017-06-15 04:20:12 +02:00
|
|
|
* @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::setDefaultAdmin($username, $password)
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function setDefaultAdmin($username, $password)
|
|
|
|
{
|
2017-06-13 11:04:43 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Please use DefaultAdminService::setDefaultAdmin($username, $password)');
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
DefaultAdminService::setDefaultAdmin($username, $password);
|
|
|
|
return true;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the passed credentials are matching the default-admin.
|
|
|
|
* Compares cleartext-password set through Security::setDefaultAdmin().
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @return bool
|
2017-06-13 11:04:43 +02:00
|
|
|
*
|
2017-06-15 04:20:12 +02:00
|
|
|
* @deprecated 4.0.0..5.0.0 Use DefaultAdminService::isDefaultAdminCredentials() instead
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function check_default_admin($username, $password)
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Please use DefaultAdminService::isDefaultAdminCredentials($username, $password)');
|
2017-06-13 11:04:43 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var DefaultAdminService $service */
|
|
|
|
return DefaultAdminService::isDefaultAdminCredentials($username, $password);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the default admin account has been set.
|
2017-06-15 04:20:12 +02:00
|
|
|
*
|
|
|
|
* @deprecated 4.0.0..5.0.0 Use DefaultAdminService::hasDefaultAdmin() instead
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function has_default_admin()
|
|
|
|
{
|
2017-06-13 11:04:43 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Please use DefaultAdminService::hasDefaultAdmin()');
|
|
|
|
|
|
|
|
return DefaultAdminService::hasDefaultAdmin();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default admin username
|
|
|
|
*
|
2017-06-15 04:20:12 +02:00
|
|
|
* @deprecated 4.0.0..5.0.0 Use DefaultAdminService::getDefaultAdminUsername()
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function default_admin_username()
|
|
|
|
{
|
2017-06-13 11:04:43 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Please use DefaultAdminService::getDefaultAdminUsername()');
|
2017-06-15 04:20:12 +02:00
|
|
|
|
2017-06-13 11:04:43 +02:00
|
|
|
return DefaultAdminService::getDefaultAdminUsername();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default admin password
|
|
|
|
*
|
2017-06-15 04:20:12 +02:00
|
|
|
* @deprecated 4.0.0..5.0.0 Use DefaultAdminService::getDefaultAdminPassword()
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function default_admin_password()
|
|
|
|
{
|
2017-06-13 11:04:43 +02:00
|
|
|
Deprecation::notice('5.0.0', 'Please use DefaultAdminService::getDefaultAdminPassword()');
|
|
|
|
|
|
|
|
return DefaultAdminService::getDefaultAdminPassword();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt a password according to the current password encryption settings.
|
|
|
|
* If the settings are so that passwords shouldn't be encrypted, the
|
|
|
|
* result is simple the clear text password with an empty salt except when
|
|
|
|
* a custom algorithm ($algorithm parameter) was passed.
|
|
|
|
*
|
|
|
|
* @param string $password The password to encrypt
|
|
|
|
* @param string $salt Optional: The salt to use. If it is not passed, but
|
|
|
|
* needed, the method will automatically create a
|
|
|
|
* random salt that will then be returned as return value.
|
|
|
|
* @param string $algorithm Optional: Use another algorithm to encrypt the
|
|
|
|
* password (so that the encryption algorithm can be changed over the time).
|
|
|
|
* @param Member $member Optional
|
|
|
|
* @return mixed Returns an associative array containing the encrypted
|
|
|
|
* password and the used salt in the form:
|
|
|
|
* <code>
|
|
|
|
* array(
|
|
|
|
* 'password' => string,
|
|
|
|
* 'salt' => string,
|
|
|
|
* 'algorithm' => string,
|
|
|
|
* 'encryptor' => PasswordEncryptor instance
|
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
* If the passed algorithm is invalid, FALSE will be returned.
|
|
|
|
*
|
2017-12-14 16:17:33 +01:00
|
|
|
* @throws PasswordEncryptor_NotFoundException
|
2016-11-29 00:31:16 +01:00
|
|
|
* @see encrypt_passwords()
|
|
|
|
*/
|
|
|
|
public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null)
|
|
|
|
{
|
|
|
|
// Fall back to the default encryption algorithm
|
|
|
|
if (!$algorithm) {
|
2017-03-02 03:24:38 +01:00
|
|
|
$algorithm = self::config()->get('password_encryption_algorithm');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$encryptor = PasswordEncryptor::create_for_algorithm($algorithm);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// New salts will only need to be generated if the password is hashed for the first time
|
2017-06-15 04:20:12 +02:00
|
|
|
$salt = ($salt) ? $salt : $encryptor->salt($password);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-05-31 07:48:16 +02:00
|
|
|
return [
|
2017-06-15 04:20:12 +02:00
|
|
|
'password' => $encryptor->encrypt($password, $salt, $member),
|
2017-02-04 20:41:31 +01:00
|
|
|
'salt' => $salt,
|
2016-11-29 00:31:16 +01:00
|
|
|
'algorithm' => $algorithm,
|
2017-06-15 04:20:12 +02:00
|
|
|
'encryptor' => $encryptor
|
2017-05-31 07:48:16 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the database is in a state to perform security checks.
|
|
|
|
* See {@link DatabaseAdmin->init()} for more information.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function database_is_ready()
|
|
|
|
{
|
|
|
|
// Used for unit tests
|
|
|
|
if (self::$force_database_is_ready !== null) {
|
|
|
|
return self::$force_database_is_ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::$database_is_ready) {
|
|
|
|
return self::$database_is_ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
$requiredClasses = ClassInfo::dataClassesFor(Member::class);
|
|
|
|
$requiredClasses[] = Group::class;
|
|
|
|
$requiredClasses[] = Permission::class;
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
foreach ($requiredClasses as $class) {
|
|
|
|
// Skip test classes, as not all test classes are scaffolded at once
|
|
|
|
if (is_a($class, TestOnly::class, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if any of the tables aren't created in the database
|
|
|
|
$table = $schema->tableName($class);
|
|
|
|
if (!ClassInfo::hasTable($table)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: DataExtensions aren't applied until a class is instantiated for
|
|
|
|
// the first time, so create an instance here.
|
|
|
|
singleton($class);
|
|
|
|
|
|
|
|
// if any of the tables don't have all fields mapped as table columns
|
|
|
|
$dbFields = DB::field_list($table);
|
|
|
|
if (!$dbFields) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$objFields = $schema->databaseFields($class, false);
|
|
|
|
$missingFields = array_diff_key($objFields, $dbFields);
|
|
|
|
|
|
|
|
if ($missingFields) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self::$database_is_ready = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
/**
|
|
|
|
* Resets the database_is_ready cache
|
|
|
|
*/
|
|
|
|
public static function clear_database_is_ready()
|
|
|
|
{
|
|
|
|
self::$database_is_ready = null;
|
|
|
|
self::$force_database_is_ready = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the database_is_ready call to return a certain value - used for testing
|
2017-06-09 05:07:35 +02:00
|
|
|
*
|
|
|
|
* @param bool $isReady
|
2017-04-23 05:30:33 +02:00
|
|
|
*/
|
|
|
|
public static function force_database_is_ready($isReady)
|
|
|
|
{
|
|
|
|
self::$force_database_is_ready = $isReady;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string Set the default login dest
|
|
|
|
* This is the URL that users will be redirected to after they log in,
|
|
|
|
* if they haven't logged in en route to access a secured page.
|
|
|
|
* By default, this is set to the homepage.
|
|
|
|
*/
|
|
|
|
private static $default_login_dest = "";
|
|
|
|
|
2017-08-25 05:07:30 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string Set the default reset password destination
|
|
|
|
* This is the URL that users will be redirected to after they change their password,
|
|
|
|
* By default, it's redirecting to {@link $login}.
|
|
|
|
*/
|
|
|
|
private static $default_reset_password_dest;
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
protected static $ignore_disallowed_actions = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to true to ignore access to disallowed actions, rather than returning permission failure
|
|
|
|
* Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions()
|
2017-06-09 05:07:35 +02:00
|
|
|
* @param bool $flag True or false
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public static function set_ignore_disallowed_actions($flag)
|
|
|
|
{
|
|
|
|
self::$ignore_disallowed_actions = $flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function ignore_disallowed_actions()
|
|
|
|
{
|
|
|
|
return self::$ignore_disallowed_actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL of the log-in page.
|
|
|
|
*
|
|
|
|
* To update the login url use the "Security.login_url" config setting.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function login_url()
|
|
|
|
{
|
2017-03-02 03:24:38 +01:00
|
|
|
return Controller::join_links(Director::baseURL(), self::config()->get('login_url'));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL of the logout page.
|
|
|
|
*
|
|
|
|
* To update the logout url use the "Security.logout_url" config setting.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function logout_url()
|
|
|
|
{
|
2017-06-15 12:54:41 +02:00
|
|
|
$logoutUrl = Controller::join_links(Director::baseURL(), self::config()->get('logout_url'));
|
|
|
|
return SecurityToken::inst()->addToUrl($logoutUrl);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL of the logout page.
|
|
|
|
*
|
|
|
|
* To update the logout url use the "Security.logout_url" config setting.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function lost_password_url()
|
|
|
|
{
|
2017-03-02 03:24:38 +01:00
|
|
|
return Controller::join_links(Director::baseURL(), self::config()->get('lost_password_url'));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines global accessible templates variables.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_template_global_variables()
|
|
|
|
{
|
2017-05-31 07:48:16 +02:00
|
|
|
return [
|
2017-02-04 20:41:31 +01:00
|
|
|
"LoginURL" => "login_url",
|
|
|
|
"LogoutURL" => "logout_url",
|
2016-11-29 00:31:16 +01:00
|
|
|
"LostPasswordURL" => "lost_password_url",
|
2017-06-22 12:50:45 +02:00
|
|
|
"CurrentMember" => "getCurrentUser",
|
|
|
|
"currentUser" => "getCurrentUser"
|
2017-05-31 07:48:16 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2011-03-25 00:01:50 +01:00
|
|
|
}
|