1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
6
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
7
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Adds the user information to the context of the record which will be logged |
11
|
|
|
*/ |
12
|
|
|
class UserProcessor |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Use container else we have a continous loop in our dependency |
16
|
|
|
* |
17
|
|
|
* @var ContainerInterface |
18
|
|
|
*/ |
19
|
|
|
private $container; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var UserInterface |
23
|
|
|
*/ |
24
|
|
|
private $user; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $record = array(); |
30
|
|
|
|
31
|
|
|
private $tokenStorage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ContainerInterface|TokenStorageInterface $tokenStorage |
35
|
|
|
*/ |
36
|
1 |
View Code Duplication |
public function __construct(/*TokenStorageInterface */ $tokenStorage) |
|
|
|
|
37
|
|
|
{ |
38
|
1 |
|
if ($tokenStorage instanceof ContainerInterface) { |
39
|
1 |
|
@trigger_error(sprintf('Passing the container as the first argument of "%s" is deprecated in KunstmaanAdminBundle 5.4 and will be removed in KunstmaanAdminBundle 6.0. Inject the "security.token_storage" service instead.', __CLASS__), E_USER_DEPRECATED); |
|
|
|
|
40
|
|
|
|
41
|
1 |
|
$this->container = $tokenStorage; |
42
|
1 |
|
$this->tokenStorage = $this->container->get('security.token_storage'); |
43
|
|
|
|
44
|
1 |
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->tokenStorage = $tokenStorage; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $record |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function processRecord(array $record) |
56
|
|
|
{ |
57
|
|
|
if (\is_null($this->user)) { |
58
|
|
|
if (($this->tokenStorage !== null) && ($this->tokenStorage->getToken() !== null) && ($this->tokenStorage->getToken()->getUser() instanceof \Symfony\Component\Security\Core\User\AdvancedUserInterface)) { |
59
|
|
|
$this->user = $this->tokenStorage->getToken()->getUser(); |
60
|
|
|
$this->record['extra']['user']['username'] = $this->user->getUsername(); |
61
|
|
|
$this->record['extra']['user']['roles'] = $this->user->getRoles(); |
62
|
|
|
$this->record['extra']['user']['is_account_non_expired'] = $this->user->isAccountNonExpired(); |
63
|
|
|
$this->record['extra']['user']['is_account_non_locked'] = $this->user->isAccountNonLocked(); |
64
|
|
|
$this->record['extra']['user']['is_credentials_non_expired'] = $this->user->isCredentialsNonExpired(); |
65
|
|
|
$this->record['extra']['user']['is_enabled'] = $this->user->isEnabled(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return array_merge($record, $this->record); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.