Skip to content

Commit

Permalink
Merge pull request #16 from louispaulet/fix-security-context into 0.1…
Browse files Browse the repository at this point in the history
…-dev

Make bundle compatible with SF2.5
  • Loading branch information
Deamon authored Apr 22, 2017
2 parents d1b6284 + 14a7c33 commit 08ae8e1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Processors/Monolog/DeamonLoggerExtraWebProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function addRequestInfo()
private function addUserInfo()
{
if ($this->configShowExtraInfo('user')) {
$token = $this->container->get('security.token_storage')->getToken();
$token = $this->container->get('security.context')->getToken();
if (($token instanceof TokenInterface) && ($token->getUser() instanceof UserInterface) && null !== $user = $token->getUser()) {
if ($this->configShowExtraInfo('user_id') && method_exists($user, 'getId')) {
$this->record['extra']['user_id'] = $user->getId();
Expand Down
79 changes: 75 additions & 4 deletions Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\User\UserInterface;

class DeamonLoggerExtraWebProcessorTest extends TestCase
Expand Down Expand Up @@ -182,9 +186,10 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
$stack->push($request);

return $stack;
case 'security.token_storage':
$storage = new TokenStorage();
$storage->setToken(new MyToken($this->getParameter('user')));
case 'security.context':
$token = new MyToken($this->getParameter('user'));
$storage = new SecurityContext(new MyAuthenticationManager($token), new MyAccessDecisionManager());
$storage->setToken($token);

return $storage;
default:
Expand Down Expand Up @@ -259,3 +264,69 @@ public function getCredentials()
{
}
}
class MyAuthenticationManager implements AuthenticationManagerInterface
{

private $token;

public function __construct(TokenInterface $token = null)
{
$this->token = $token;
}

/**
* Attempts to authenticate a TokenInterface object.
*
* @param TokenInterface $token The TokenInterface instance to authenticate
*
* @return TokenInterface An authenticated TokenInterface instance, never null
*
* @throws AuthenticationException if the authentication fails
*/
public function authenticate(TokenInterface $token)
{
return $this->token;
}
}

class MyAccessDecisionManager implements AccessDecisionManagerInterface
{

/**
* Decides whether the access is possible or not.
*
* @param TokenInterface $token A TokenInterface instance
* @param array $attributes An array of attributes associated with the method being invoked
* @param object $object The object to secure
*
* @return bool true if the access is granted, false otherwise
*/
public function decide(TokenInterface $token, array $attributes, $object = null)
{
return true;
}

/**
* Checks if the access decision manager supports the given attribute.
*
* @param string $attribute An attribute
*
* @return bool true if this decision manager supports the attribute, false otherwise
*/
public function supportsAttribute($attribute)
{
return true;
}

/**
* Checks if the access decision manager supports the given class.
*
* @param string $class A class name
*
* @return true if this decision manager can process the class
*/
public function supportsClass($class)
{
return true;
}
}
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
"gelf"
],
"require" : {

"php": ">=5.5",
"symfony/security": "^2.7||^3.0",
"symfony/dependency-injection": "^2.7||^3.0",
"symfony/monolog-bridge": "^2.7||^3.0",
"symfony/http-foundation": "^2.7||^3.0",
"symfony/http-kernel": "^2.7||^3.0",
"symfony/config": "^2.7||^3.0"
"symfony/security": "^2.5",
"symfony/dependency-injection": "^2.5",
"symfony/monolog-bridge": "^2.5",
"symfony/http-foundation": "^2.5",
"symfony/http-kernel": "^2.5",
"symfony/config": "^2.5"
},
"require-dev" : {
"phpunit/phpunit": "^4.8||^5.4.3",
Expand Down

0 comments on commit 08ae8e1

Please sign in to comment.