2017-05-20 06:32:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Security\MemberAuthenticator;
|
|
|
|
|
2017-11-27 17:15:28 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-06-15 12:54:41 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2017-07-13 23:20:58 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-05-20 06:32:25 +02:00
|
|
|
use SilverStripe\Control\RequestHandler;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-06-15 12:54:41 +02:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2017-05-20 06:32:25 +02:00
|
|
|
use SilverStripe\Security\IdentityStore;
|
2017-06-15 12:54:41 +02:00
|
|
|
use SilverStripe\Security\LogoutForm;
|
2017-05-20 06:32:25 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
2017-06-15 12:54:41 +02:00
|
|
|
use SilverStripe\Security\SecurityToken;
|
2017-05-20 06:32:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class LogoutHandler handles logging out Members from their session and/or cookie.
|
|
|
|
* The logout process destroys all traces of the member on the server (not the actual computer user
|
|
|
|
* at the other end of the line, don't worry)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class LogoutHandler extends RequestHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $url_handlers = [
|
|
|
|
'' => 'logout'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $allowed_actions = [
|
2017-06-15 12:54:41 +02:00
|
|
|
'logout',
|
|
|
|
'LogoutForm'
|
2017-05-20 06:32:25 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log out form handler method
|
|
|
|
*
|
|
|
|
* This method is called when the user clicks on "logout" on the form
|
|
|
|
* created when the parameter <i>$checkCurrentUser</i> of the
|
|
|
|
* {@link __construct constructor} was set to TRUE and the user was
|
|
|
|
* currently logged in.
|
|
|
|
*
|
2017-07-13 23:20:58 +02:00
|
|
|
* @return array|HTTPResponse
|
2017-05-20 06:32:25 +02:00
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
$member = Security::getCurrentUser();
|
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
// If the user doesn't have a security token, show them a form where they can get one.
|
|
|
|
// This protects against nuisance CSRF attacks to log out users.
|
|
|
|
if ($member && !SecurityToken::inst()->checkRequest($this->getRequest())) {
|
|
|
|
Security::singleton()->setSessionMessage(
|
|
|
|
_t(
|
|
|
|
'SilverStripe\\Security\\Security.CONFIRMLOGOUT',
|
|
|
|
"Please click the button below to confirm that you wish to log out."
|
|
|
|
),
|
|
|
|
ValidationResult::TYPE_WARNING
|
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'Form' => $this->logoutForm()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-05-20 06:32:25 +02:00
|
|
|
return $this->doLogOut($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-15 12:54:41 +02:00
|
|
|
* @return LogoutForm
|
|
|
|
*/
|
|
|
|
public function logoutForm()
|
|
|
|
{
|
|
|
|
return LogoutForm::create($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-20 06:32:25 +02:00
|
|
|
* @param Member $member
|
2017-06-15 12:54:41 +02:00
|
|
|
* @return HTTPResponse
|
2017-05-20 06:32:25 +02:00
|
|
|
*/
|
|
|
|
public function doLogOut($member)
|
|
|
|
{
|
2017-06-15 12:54:41 +02:00
|
|
|
$this->extend('beforeLogout');
|
|
|
|
|
2017-05-20 06:32:25 +02:00
|
|
|
if ($member instanceof Member) {
|
|
|
|
Injector::inst()->get(IdentityStore::class)->logOut($this->getRequest());
|
|
|
|
}
|
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
$this->extend('failedLogout');
|
|
|
|
} else {
|
|
|
|
$this->extend('afterLogout');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirectAfterLogout();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
|
|
|
protected function redirectAfterLogout()
|
|
|
|
{
|
|
|
|
$backURL = $this->getBackURL();
|
|
|
|
if ($backURL) {
|
|
|
|
return $this->redirect($backURL);
|
|
|
|
}
|
|
|
|
|
2017-11-27 17:15:28 +01:00
|
|
|
$link = Security::config()->get('login_url');
|
|
|
|
$referer = $this->getReturnReferer();
|
|
|
|
if ($referer) {
|
|
|
|
$link = Controller::join_links($link, '?' . http_build_query([
|
|
|
|
'BackURL' => Director::makeRelative($referer)
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirect($link);
|
2017-05-20 06:32:25 +02:00
|
|
|
}
|
|
|
|
}
|