Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3763515

Browse files
committed
Add TokenProcessor
1 parent9b8d96b commit3763515

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 <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+
namespaceSymfony\Bridge\Monolog\Processor;
13+
14+
useSymfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15+
16+
/**
17+
* Adds the current security token to the log entry.
18+
*
19+
* @author Dany Maillard <danymaillard93b@gmail.com>
20+
*/
21+
class TokenProcessor
22+
{
23+
private$tokenStorage;
24+
25+
publicfunction__construct(TokenStorageInterface$tokenStorage)
26+
{
27+
$this->tokenStorage =$tokenStorage;
28+
}
29+
30+
publicfunction__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 <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+
namespaceSymfony\Bridge\Monolog\Tests\Processor;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Bridge\Monolog\Processor\TokenProcessor;
16+
useSymfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17+
useSymfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
18+
19+
/**
20+
* Tests the TokenProcessor.
21+
*
22+
* @author Dany Maillard <danymaillard93b@gmail.com>
23+
*/
24+
class TokenProcessorTestextends TestCase
25+
{
26+
publicfunctiontestProcessor()
27+
{
28+
$token =newUsernamePasswordToken('user','password','provider',array('ROLE_USER'));
29+
$tokenStorage =$this->getMockBuilder(TokenStorageInterface::class)->getMock();
30+
$tokenStorage->method('getToken')->willReturn($token);
31+
32+
$processor =newTokenProcessor($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)

[8]ページ先頭

©2009-2025 Movatter.jp