|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CultuurNet\UDB3\Place; |
|
7
|
|
|
|
|
8
|
|
|
use CultuurNet\SymfonySecurityOAuthUitid\User; |
|
9
|
|
|
use CultuurNet\UDB3\Event\SecurityInterface; |
|
10
|
|
|
use CultuurNet\UDB3\Place\ReadModel\Permission\PermissionQueryInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
12
|
|
|
use ValueObjects\String\String; |
|
13
|
|
|
|
|
14
|
|
View Code Duplication |
class Security implements SecurityInterface |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var PermissionQueryInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $permissionRepository; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var TokenStorageInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $tokenStorage; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
TokenStorageInterface $tokenStorage, |
|
28
|
|
|
PermissionQueryInterface $permissionRepository |
|
29
|
|
|
) { |
|
30
|
|
|
$this->tokenStorage = $tokenStorage; |
|
31
|
|
|
$this->permissionRepository = $permissionRepository; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function allowsUpdateWithCdbXml(String $eventId) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->currentUiTIDUserCanEditPlace($eventId); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function allowsUpdates(String $eventId) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->currentUiTIDUserCanEditPlace($eventId); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param String $placeId |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
private function currentUiTIDUserCanEditPlace(String $placeId) |
|
55
|
|
|
{ |
|
56
|
|
|
$token = $this->tokenStorage->getToken(); |
|
57
|
|
|
|
|
58
|
|
|
if (!$token) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$user = $token->getUser(); |
|
63
|
|
|
|
|
64
|
|
|
if ($user instanceof User) { |
|
|
|
|
|
|
65
|
|
|
$userId = new String($user->getUid()); |
|
66
|
|
|
} else if ($user instanceof \CultuurNet\UiTIDProvider\User\User) { |
|
|
|
|
|
|
67
|
|
|
$userId = new String($user->id); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (!isset($userId)) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$editableEvents = $this->permissionRepository->getEditablePlaces( |
|
75
|
|
|
$userId |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
return in_array($placeId, $editableEvents); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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.