NEW HTTPRequest now has hasSession() to determine whether a session exists for it

This commit is contained in:
Robbie Averill 2019-08-02 11:29:23 +12:00
parent 3224c9971b
commit 0672f8b76b
3 changed files with 28 additions and 11 deletions

View File

@ -882,12 +882,22 @@ class HTTPRequest implements ArrayAccess
} }
} }
/**
* Determines whether the request has a session
*
* @return bool
*/
public function hasSession(): bool
{
return !empty($this->session);
}
/** /**
* @return Session * @return Session
*/ */
public function getSession() public function getSession()
{ {
if (empty($this->session)) { if (!$this->hasSession()) {
throw new BadMethodCallException("No session available for this HTTPRequest"); throw new BadMethodCallException("No session available for this HTTPRequest");
} }
return $this->session; return $this->session;

View File

@ -2,7 +2,6 @@
namespace SilverStripe\Security; namespace SilverStripe\Security;
use BadMethodCallException;
use LogicException; use LogicException;
use Page; use Page;
use ReflectionClass; use ReflectionClass;
@ -417,17 +416,15 @@ class Security extends Controller implements TemplateGlobalProvider
$controller->extend('permissionDenied', $member); $controller->extend('permissionDenied', $member);
return $response; return $response;
} else {
$message = $messageSet['default'];
} }
$message = $messageSet['default'];
try { $request = $controller->getRequest();
if ($request->hasSession()) {
list($messageText, $messageCast) = $parseMessage($message); list($messageText, $messageCast) = $parseMessage($message);
static::singleton()->setSessionMessage($messageText, ValidationResult::TYPE_WARNING, $messageCast); static::singleton()->setSessionMessage($messageText, ValidationResult::TYPE_WARNING, $messageCast);
$controller->getRequest()->getSession()->set("BackURL", $_SERVER['REQUEST_URI']); $request->getSession()->set("BackURL", $_SERVER['REQUEST_URI']);
} catch (BadMethodCallException $ex) {
// noop, if session was not set yet
} }
// TODO AccessLogEntry needs an extension to handle permission denied errors // TODO AccessLogEntry needs an extension to handle permission denied errors

View File

@ -2,10 +2,11 @@
namespace SilverStripe\Control\Tests; namespace SilverStripe\Control\Tests;
use SilverStripe\Control\Middleware\TrustedProxyMiddleware;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\HTTPRequest;
use ReflectionMethod; use ReflectionMethod;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\TrustedProxyMiddleware;
use SilverStripe\Control\Session;
use SilverStripe\Dev\SapphireTest;
class HTTPRequestTest extends SapphireTest class HTTPRequestTest extends SapphireTest
{ {
@ -286,4 +287,13 @@ class HTTPRequestTest extends SapphireTest
$this->assertEquals($ip, $reflectionMethod->invoke($req, $header)); $this->assertEquals($ip, $reflectionMethod->invoke($req, $header));
} }
} }
public function testHasSession()
{
$request = new HTTPRequest('GET', '/');
$this->assertFalse($request->hasSession());
$request->setSession($this->createMock(Session::class));
$this->assertTrue($request->hasSession());
}
} }