Skip to content

Commit 21b7fd4

Browse files
feature symfony#21086 [MonologBridge] Add TokenProcessor (maidmaid)
This PR was merged into the 3.4 branch. Discussion ---------- [MonologBridge] Add TokenProcessor | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a ``TokenProcessor`` is an out-of-the-box class that adds token in extra info log, like this: ``` [2016-12-29 14:48:09] app.INFO: admin connected! [] {"token":{ "username":"admin", "authenticated":true, "roles":["ROLE_ADMIN"]} } ``` Commits ------- 3763515 Add TokenProcessor
2 parents 4324804 + 3763515 commit 21b7fd4

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
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 <[email protected]>
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+
16+
/**
17+
* Adds the current security token to the log entry.
18+
*
19+
* @author Dany Maillard <[email protected]>
20+
*/
21+
class TokenProcessor
22+
{
23+
private $tokenStorage;
24+
25+
public function __construct(TokenStorageInterface $tokenStorage)
26+
{
27+
$this->tokenStorage = $tokenStorage;
28+
}
29+
30+
public function __invoke(array $records)
31+
{
32+
$records['extra']['token'] = null;
33+
if (null !== $token = $this->tokenStorage->getToken()) {
34+
$records['extra']['token'] = array(
35+
'username' => $token->getUsername(),
36+
'authenticated' => $token->isAuthenticated(),
37+
'roles' => array_map(function ($role) { return $role->getRole(); }, $token->getRoles()),
38+
);
39+
}
40+
41+
return $records;
42+
}
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
19+
/**
20+
* Tests the TokenProcessor.
21+
*
22+
* @author Dany Maillard <[email protected]>
23+
*/
24+
class TokenProcessorTest extends TestCase
25+
{
26+
public function testProcessor()
27+
{
28+
$token = new UsernamePasswordToken('user', 'password', 'provider', array('ROLE_USER'));
29+
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
30+
$tokenStorage->method('getToken')->willReturn($token);
31+
32+
$processor = new TokenProcessor($tokenStorage);
33+
$record = array('extra' => array());
34+
$record = $processor($record);
35+
36+
$this->assertArrayHasKey('token', $record['extra']);
37+
$this->assertEquals($token->getUsername(), $record['extra']['token']['username']);
38+
$this->assertEquals($token->isAuthenticated(), $record['extra']['token']['authenticated']);
39+
$roles = array_map(function ($role) { return $role->getRole(); }, $token->getRoles());
40+
$this->assertEquals($roles, $record['extra']['token']['roles']);
41+
}
42+
}

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|~4.0",
2627
"symfony/var-dumper": "~3.3|~4.0"
2728
},
2829
"conflict": {

0 commit comments

Comments
 (0)