FIX Use Injector to retrieve the current session

This commit is contained in:
Robbie Averill 2017-12-12 14:56:38 +13:00
parent 429c1e0e3b
commit 097d0697c5

View File

@ -2,11 +2,13 @@
namespace SilverStripe\Security; namespace SilverStripe\Security;
use Exception;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\HiddenField;
use SilverStripe\View\TemplateGlobalProvider; use SilverStripe\View\TemplateGlobalProvider;
@ -56,7 +58,7 @@ class SecurityToken implements TemplateGlobalProvider
protected static $enabled = true; protected static $enabled = true;
/** /**
* @var String $name * @var string $name
*/ */
protected $name = null; protected $name = null;
@ -110,7 +112,7 @@ class SecurityToken implements TemplateGlobalProvider
} }
/** /**
* @return String * @return string
*/ */
public static function get_default_name() public static function get_default_name()
{ {
@ -146,11 +148,11 @@ class SecurityToken implements TemplateGlobalProvider
} }
/** /**
* @return String * @return string
*/ */
public function getValue() public function getValue()
{ {
$session = Controller::curr()->getRequest()->getSession(); $session = $this->getSession();
$value = $session->get($this->getName()); $value = $session->get($this->getName());
// only regenerate if the token isn't already set in the session // only regenerate if the token isn't already set in the session
@ -163,12 +165,28 @@ class SecurityToken implements TemplateGlobalProvider
} }
/** /**
* @param String $val * @param string $val
* @return $this
*/ */
public function setValue($val) public function setValue($val)
{ {
$session = Controller::curr()->getRequest()->getSession(); $this->getSession()->set($this->getName(), $val);
$session->set($this->getName(), $val); return $this;
}
/**
* Returns the current session instance from the injector
*
* @return Session
* @throws Exception If the HTTPRequest class hasn't been registered as a service
*/
protected function getSession()
{
$injector = Injector::inst();
if (!$injector->has(HTTPRequest::class)) {
throw new Exception('No HTTPRequest object available yet!');
}
return $injector->get(HTTPRequest::class)->getSession();
} }
/** /**
@ -188,8 +206,8 @@ class SecurityToken implements TemplateGlobalProvider
* *
* Typically you'll want to check {@link Form->securityTokenEnabled()} before calling this method. * Typically you'll want to check {@link Form->securityTokenEnabled()} before calling this method.
* *
* @param String $compare * @param string $compare
* @return Boolean * @return boolean
*/ */
public function check($compare) public function check($compare)
{ {
@ -246,8 +264,8 @@ class SecurityToken implements TemplateGlobalProvider
} }
/** /**
* @param String $url * @param string $url
* @return String * @return string
*/ */
public function addToUrl($url) public function addToUrl($url)
{ {
@ -272,7 +290,7 @@ class SecurityToken implements TemplateGlobalProvider
/** /**
* @uses RandomGenerator * @uses RandomGenerator
* *
* @return String * @return string
*/ */
protected function generate() protected function generate()
{ {