1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager\Subscriber\Behavior\Audit; |
13
|
|
|
|
14
|
|
|
use Sulu\Component\DocumentManager\Behavior\Audit\BlameBehavior; |
15
|
|
|
use Sulu\Component\DocumentManager\Event\ConfigureOptionsEvent; |
16
|
|
|
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent; |
17
|
|
|
use Sulu\Component\DocumentManager\Event\PersistEvent; |
18
|
|
|
use Sulu\Component\DocumentManager\Events; |
19
|
|
|
use Sulu\Component\Security\Authentication\UserInterface; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
22
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Manages user blame (log who creator the document and who updated it last). |
26
|
|
|
*/ |
27
|
|
|
class BlameSubscriber implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
const CREATOR = 'creator'; |
30
|
|
|
const CHANGER = 'changer'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TokenStorage |
34
|
|
|
*/ |
35
|
|
|
private $tokenStorage; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param TokenStorage $tokenStorage |
39
|
|
|
*/ |
40
|
|
|
public function __construct(TokenStorage $tokenStorage = null) |
41
|
|
|
{ |
42
|
|
|
$this->tokenStorage = $tokenStorage; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public static function getSubscribedEvents() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
Events::CONFIGURE_OPTIONS => 'configureOptions', |
52
|
|
|
Events::PERSIST => 'handlePersist', |
53
|
|
|
Events::METADATA_LOAD => 'handleMetadataLoad', |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ConfigureOptionsEvent $event |
59
|
|
|
*/ |
60
|
|
|
public function configureOptions(ConfigureOptionsEvent $event) |
61
|
|
|
{ |
62
|
|
|
$event->getOptions()->setDefaults([ |
63
|
|
|
'user' => null, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function handleMetadataLoad(MetadataLoadEvent $event) |
68
|
|
|
{ |
69
|
|
|
$metadata = $event->getMetadata(); |
70
|
|
|
|
71
|
|
|
if (!$metadata->getReflectionClass()->isSubclassOf(BlameBehavior::class)) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$metadata->addFieldMapping('creator', [ |
76
|
|
|
'encoding' => 'system_localized', |
77
|
|
|
'type' => 'date', |
78
|
|
|
]); |
79
|
|
|
$metadata->addFieldMapping('changer', [ |
80
|
|
|
'encoding' => 'system_localized', |
81
|
|
|
'type' => 'date', |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param PersistEvent $event |
87
|
|
|
*/ |
88
|
|
|
public function handlePersist(PersistEvent $event) |
89
|
|
|
{ |
90
|
|
|
$document = $event->getDocument(); |
91
|
|
|
|
92
|
|
|
if (!$document instanceof BlameBehavior) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$userId = $this->getUserId($event->getOptions()); |
97
|
|
|
|
98
|
|
|
if (null === $userId) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!$event->getLocale()) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!$document->getCreator()) { |
107
|
|
|
$event->getAccessor()->set(self::CREATOR, $userId); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$event->getAccessor()->set(self::CHANGER, $userId); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function getUserId($options) |
114
|
|
|
{ |
115
|
|
|
if ($options['user']) { |
116
|
|
|
return $options['user']; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (null === $this->tokenStorage) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$token = $this->tokenStorage->getToken(); |
124
|
|
|
|
125
|
|
|
if (null === $token || $token instanceof AnonymousToken) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$user = $token->getUser(); |
130
|
|
|
|
131
|
|
View Code Duplication |
if (!$user instanceof UserInterface) { |
|
|
|
|
132
|
|
|
throw new \InvalidArgumentException(sprintf( |
133
|
|
|
'User must implement the Sulu UserInterface, got "%s"', |
134
|
|
|
is_object($user) ? get_class($user) : gettype($user) |
135
|
|
|
)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $user->getId(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.