diff --git a/src/Symfony/Bridge/Monolog/Processor/TokenProcessor.php b/src/Symfony/Bridge/Monolog/Processor/TokenProcessor.php new file mode 100644 index 0000000000000..11547be22b2ee --- /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 0000000000000..e78acf47b54c4 --- /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 b627abad0a79a..fcebd789f2bc0 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": { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy