|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\CommandHandling; |
|
4
|
|
|
|
|
5
|
|
|
use Broadway\CommandHandling\CommandBusInterface; |
|
6
|
|
|
use Broadway\Domain\Metadata; |
|
7
|
|
|
use CultuurNet\UDB3\Offer\Commands\AuthorizableCommandInterface; |
|
8
|
|
|
use CultuurNet\UDB3\Security\CommandAuthorizationException; |
|
9
|
|
|
use CultuurNet\UDB3\Security\SecurityInterface; |
|
10
|
|
|
use CultuurNet\UDB3\Security\UserIdentificationInterface; |
|
11
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class AuthorizedCommandBus extends CommandBusDecoratorBase implements AuthorizedCommandBusInterface, LoggerAwareInterface, ContextAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Metadata |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $metadata; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var UserIdentificationInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $userIdentification; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var SecurityInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $security; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* AuthorizedCommandBus constructor. |
|
33
|
|
|
* @param CommandBusInterface $decoratee |
|
34
|
|
|
* @param UserIdentificationInterface $userIdentification |
|
35
|
|
|
* @param SecurityInterface $security |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
CommandBusInterface $decoratee, |
|
39
|
|
|
UserIdentificationInterface $userIdentification, |
|
40
|
|
|
SecurityInterface $security |
|
41
|
|
|
) { |
|
42
|
|
|
parent::__construct($decoratee); |
|
43
|
|
|
|
|
44
|
|
|
$this->userIdentification = $userIdentification; |
|
45
|
|
|
$this->security = $security; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function dispatch($command) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($command instanceof AuthorizableCommandInterface) { |
|
54
|
|
|
$authorized = $this->isAuthorized($command); |
|
55
|
|
|
} else { |
|
56
|
|
|
$authorized = true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($authorized) { |
|
60
|
|
|
parent::dispatch($command); |
|
61
|
|
|
} else { |
|
62
|
|
|
throw new CommandAuthorizationException( |
|
63
|
|
|
$this->userIdentification->getId(), |
|
|
|
|
|
|
64
|
|
|
$command |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
|
|
public function isAuthorized(AuthorizableCommandInterface $command) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->security->isAuthorized($command); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return UserIdentificationInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getUserIdentification() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->userIdentification; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Sets a logger instance on the object. |
|
87
|
|
|
* |
|
88
|
|
|
* @param LoggerInterface $logger |
|
89
|
|
|
* |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setLogger(LoggerInterface $logger) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->decoratee->setLogger($logger); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param Metadata|null $context |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setContext(Metadata $context = null) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->metadata = $context; |
|
103
|
|
|
|
|
104
|
|
|
if ($this->decoratee instanceof ContextAwareInterface) { |
|
105
|
|
|
$this->decoratee->setContext($context); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: