Skip to content

Commit 26b9d1f

Browse files
committed
Add TokenProcessor
1 parent 7140b52 commit 26b9d1f

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Processor;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15+
use Symfony\Component\Security\Core\Role\RoleInterface;
16+
17+
/**
18+
* Adds the current security token to the log entry.
19+
*
20+
* @author Dany Maillard <danymaillard93b@gmail.com>
21+
*/
22+
class TokenProcessor
23+
{
24+
private $tokenStorage;
25+
26+
public function __construct(TokenStorageInterface $tokenStorage)
27+
{
28+
$this->tokenStorage = $tokenStorage;
29+
}
30+
31+
public function __invoke(array $records)
32+
{
33+
$records['extra']['token'] = null;
34+
if (null !== $token = $this->tokenStorage->getToken()) {
35+
$records['extra']['token'] = array(
36+
'username' => $token->getUsername(),
37+
'authenticated' => $token->isAuthenticated(),
38+
'roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $token->getRoles()),
39+
);
40+
}
41+
42+
return $records;
43+
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Tests\Processor;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Monolog\Processor\TokenProcessor;
16+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
18+
use Symfony\Component\Security\Core\Role\RoleInterface;
19+
20+
/**
21+
* Tests the TokenProcessor.
22+
*
23+
* @author Dany Maillard <danymaillard93b@gmail.com>
24+
*/
25+
class TokenProcessorTest extends TestCase
26+
{
27+
public function testProcessor()
28+
{
29+
$token = new UsernamePasswordToken('user', 'password', 'provider', array('ROLE_USER'));
30+
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
31+
$tokenStorage->method('getToken')->willReturn($token);
32+
33+
$processor = new TokenProcessor($tokenStorage);
34+
$record = array('extra' => array());
35+
$record = $processor($record);
36+
37+
$this->assertArrayHasKey('token', $record['extra']);
38+
$this->assertEquals($token->getUsername(), $record['extra']['token']['username']);
39+
$this->assertEquals($token->isAuthenticated(), $record['extra']['token']['authenticated']);
40+
$roles = array_map(function (RoleInterface $role) { return $role->getRole(); }, $token->getRoles());
41+
$this->assertEquals($roles, $record['extra']['token']['roles']);
42+
}
43+
}

src/Symfony/Bridge/Monolog/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"require-dev": {
2424
"symfony/console": "~2.8|~3.0|~4.0",
2525
"symfony/event-dispatcher": "~2.8|~3.0|~4.0",
26+
"symfony/security-core": "~2.8|~3.0",
2627
"symfony/var-dumper": "~3.3|~4.0"
2728
},
2829
"conflict": {

0 commit comments

Comments
 (0)