Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

src/Kunstmaan/AdminBundle/Helper/UserProcessor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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