From 3763515caed048518d91b3c3dd194958658dde6b Mon Sep 17 00:00:00 2001 From: Dany Maillard Date: Sun, 25 Dec 2016 20:27:49 +0100 Subject: [PATCH] Add TokenProcessor --- .../Monolog/Processor/TokenProcessor.php | 43 +++++++++++++++++++ .../Tests/Processor/TokenProcessorTest.php | 42 ++++++++++++++++++ src/Symfony/Bridge/Monolog/composer.json | 1 + 3 files changed, 86 insertions(+) create mode 100644 src/Symfony/Bridge/Monolog/Processor/TokenProcessor.php create mode 100644 src/Symfony/Bridge/Monolog/Tests/Processor/TokenProcessorTest.php diff --git a/src/Symfony/Bridge/Monolog/Processor/TokenProcessor.php b/src/Symfony/Bridge/Monolog/Processor/TokenProcessor.php new file mode 100644 index 0000000000..11547be22b --- /dev/null +++ b/src/Symfony/Bridge/Monolog/Processor/TokenProcessor.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Monolog\Processor; + +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; + +/** + * Adds the current security token to the log entry. + * + * @author Dany Maillard + */ +class TokenProcessor +{ + private $tokenStorage; + + public function __construct(TokenStorageInterface $tokenStorage) + { + $this->tokenStorage = $tokenStorage; + } + + public function __invoke(array $records) + { + $records['extra']['token'] = null; + if (null !== $token = $this->tokenStorage->getToken()) { + $records['extra']['token'] = array( + 'username' => $token->getUsername(), + 'authenticated' => $token->isAuthenticated(), + 'roles' => array_map(function ($role) { return $role->getRole(); }, $token->getRoles()), + ); + } + + return $records; + } +} diff --git a/src/Symfony/Bridge/Monolog/Tests/Processor/TokenProcessorTest.php b/src/Symfony/Bridge/Monolog/Tests/Processor/TokenProcessorTest.php new file mode 100644 index 0000000000..e78acf47b5 --- /dev/null +++ b/src/Symfony/Bridge/Monolog/Tests/Processor/TokenProcessorTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Monolog\Tests\Processor; + +use PHPUnit\Framework\TestCase; +use Symfony\Bridge\Monolog\Processor\TokenProcessor; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; + +/** + * Tests the TokenProcessor. + * + * @author Dany Maillard + */ +class TokenProcessorTest extends TestCase +{ + public function testProcessor() + { + $token = new UsernamePasswordToken('user', 'password', 'provider', array('ROLE_USER')); + $tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); + $tokenStorage->method('getToken')->willReturn($token); + + $processor = new TokenProcessor($tokenStorage); + $record = array('extra' => array()); + $record = $processor($record); + + $this->assertArrayHasKey('token', $record['extra']); + $this->assertEquals($token->getUsername(), $record['extra']['token']['username']); + $this->assertEquals($token->isAuthenticated(), $record['extra']['token']['authenticated']); + $roles = array_map(function ($role) { return $role->getRole(); }, $token->getRoles()); + $this->assertEquals($roles, $record['extra']['token']['roles']); + } +} diff --git a/src/Symfony/Bridge/Monolog/composer.json b/src/Symfony/Bridge/Monolog/composer.json index b627abad0a..fcebd789f2 100644 --- a/src/Symfony/Bridge/Monolog/composer.json +++ b/src/Symfony/Bridge/Monolog/composer.json @@ -23,6 +23,7 @@ "require-dev": { "symfony/console": "~2.8|~3.0|~4.0", "symfony/event-dispatcher": "~2.8|~3.0|~4.0", + "symfony/security-core": "~2.8|~3.0|~4.0", "symfony/var-dumper": "~3.3|~4.0" }, "conflict": {