2017-05-07 21:11:00 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Security\MemberAuthenticator;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-05-20 06:32:25 +02:00
|
|
|
use SilverStripe\Control\Cookie;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2017-05-07 21:11:00 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\AuthenticationHandler;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2017-05-07 21:11:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate a member pased on a session cookie
|
|
|
|
*/
|
2017-06-09 05:07:35 +02:00
|
|
|
class SessionAuthenticationHandler implements AuthenticationHandler
|
2017-05-07 21:11:00 +02:00
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-05-07 21:11:00 +02:00
|
|
|
private $sessionVariable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the session variable name used to track member ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSessionVariable()
|
|
|
|
{
|
|
|
|
return $this->sessionVariable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the session variable name used to track member ID
|
|
|
|
*
|
|
|
|
* @param string $sessionVariable
|
|
|
|
*/
|
|
|
|
public function setSessionVariable($sessionVariable)
|
|
|
|
{
|
|
|
|
$this->sessionVariable = $sessionVariable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param HTTPRequest $request
|
2017-06-09 05:07:35 +02:00
|
|
|
* @return Member
|
2017-05-07 21:11:00 +02:00
|
|
|
*/
|
|
|
|
public function authenticateRequest(HTTPRequest $request)
|
|
|
|
{
|
2017-06-09 05:07:35 +02:00
|
|
|
// If ID is a bad ID it will be treated as if the user is not logged in, rather than throwing a
|
|
|
|
// ValidationException
|
2017-06-22 12:50:45 +02:00
|
|
|
$id = $request->getSession()->get($this->getSessionVariable());
|
2017-06-09 05:07:35 +02:00
|
|
|
if (!$id) {
|
|
|
|
return null;
|
2017-05-07 21:11:00 +02:00
|
|
|
}
|
2017-06-09 05:07:35 +02:00
|
|
|
/** @var Member $member */
|
|
|
|
$member = Member::get()->byID($id);
|
|
|
|
return $member;
|
2017-05-07 21:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-30 09:42:00 +02:00
|
|
|
* @param Member $member
|
|
|
|
* @param bool $persistent
|
2017-06-09 05:07:35 +02:00
|
|
|
* @param HTTPRequest $request
|
2017-05-07 21:11:00 +02:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
public function logIn(Member $member, $persistent = false, HTTPRequest $request = null)
|
2017-05-07 21:11:00 +02:00
|
|
|
{
|
2017-05-20 06:32:25 +02:00
|
|
|
static::regenerateSessionId();
|
2017-06-22 12:50:45 +02:00
|
|
|
$request = $request ?: Controller::curr()->getRequest();
|
|
|
|
$request->getSession()->set($this->getSessionVariable(), $member->ID);
|
2017-05-07 21:11:00 +02:00
|
|
|
|
|
|
|
// This lets apache rules detect whether the user has logged in
|
2017-05-30 09:42:00 +02:00
|
|
|
// @todo make this a setting on the authentication handler
|
2017-05-20 06:32:25 +02:00
|
|
|
if (Member::config()->get('login_marker_cookie')) {
|
|
|
|
Cookie::set(Member::config()->get('login_marker_cookie'), 1, 0);
|
2017-05-07 21:11:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate the session_id.
|
|
|
|
*/
|
|
|
|
protected static function regenerateSessionId()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
if (!Member::config()->get('session_regenerate_id')) {
|
2017-05-07 21:11:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This can be called via CLI during testing.
|
|
|
|
if (Director::is_cli()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = '';
|
|
|
|
$line = '';
|
|
|
|
|
|
|
|
// @ is to supress win32 warnings/notices when session wasn't cleaned up properly
|
|
|
|
// There's nothing we can do about this, because it's an operating system function!
|
|
|
|
if (!headers_sent($file, $line)) {
|
|
|
|
@session_regenerate_id(true);
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 09:42:00 +02:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
/**
|
2017-06-09 05:07:35 +02:00
|
|
|
* @param HTTPRequest $request
|
2017-05-07 21:11:00 +02:00
|
|
|
*/
|
2017-05-30 09:42:00 +02:00
|
|
|
public function logOut(HTTPRequest $request = null)
|
2017-05-07 21:11:00 +02:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$request = $request ?: Controller::curr()->getRequest();
|
2017-11-02 22:45:46 +01:00
|
|
|
$request->getSession()->restart($request);
|
2017-05-07 21:11:00 +02:00
|
|
|
}
|
|
|
|
}
|