Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #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
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <danymaillard93b@gmail.com> | ||
*/ | ||
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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Bridge/Monolog/Tests/Processor/TokenProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <danymaillard93b@gmail.com> | ||
*/ | ||
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters