1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Service; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Role; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Ds\Component\Api\Api\Api; |
8
|
|
|
use Ds\Component\Api\Model\Access; |
9
|
|
|
use Ds\Component\Api\Model\Permission; |
10
|
|
|
use Ds\Component\Api\Query\AccessParameters; |
11
|
|
|
use Ds\Component\Discovery\Service\DiscoveryService; |
12
|
|
|
use Ds\Component\Entity\Service\EntityService; |
13
|
|
|
use LogicException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class RoleService |
17
|
|
|
*/ |
18
|
|
|
class RoleService extends EntityService |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Ds\Component\Api\Api\Api |
22
|
|
|
*/ |
23
|
|
|
protected $api; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Ds\Component\Discovery\Service\DiscoveryService |
27
|
|
|
*/ |
28
|
|
|
protected $discoveryService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param \Doctrine\ORM\EntityManager $manager |
34
|
|
|
* @param \Ds\Component\Api\Api\Api $api |
35
|
|
|
* @param \Ds\Component\Discovery\Service\DiscoveryService $discoveryService |
36
|
|
|
* @param string $entity |
37
|
|
|
*/ |
38
|
|
|
public function __construct(EntityManager $manager, Api $api, DiscoveryService $discoveryService, $entity = Role::class) |
39
|
|
|
{ |
40
|
|
|
parent::__construct($manager, $entity); |
41
|
|
|
$this->api = $api; |
42
|
|
|
$this->discoveryService = $discoveryService; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create access across all microservices |
47
|
|
|
* |
48
|
|
|
* @param \AppBundle\Entity\Role $role |
49
|
|
|
* @return \AppBundle\Service\RoleService |
50
|
|
|
* @throws \LogicException |
51
|
|
|
*/ |
52
|
|
|
public function createAccess(Role $role) |
53
|
|
|
{ |
54
|
|
|
if (null === $role->getUuid()) { |
|
|
|
|
55
|
|
|
throw new LogicException('Role does not have a uuid.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$services = $this->discoveryService->get(); |
59
|
|
|
|
60
|
|
|
foreach ($role->getPermissions() as $service => $scopes) { |
61
|
|
|
if ( |
62
|
|
|
property_exists($services, $service) && |
63
|
|
|
$services->$service->enabled && |
64
|
|
|
property_exists($services->$service, 'attributes') && |
65
|
|
|
property_exists($services->$service->attributes, 'access') && |
66
|
|
|
$services->$service->attributes->access |
67
|
|
|
) { |
68
|
|
|
$access = new Access; |
69
|
|
|
$access |
70
|
|
|
->setOwner($role->getOwner()) |
71
|
|
|
->setOwnerUuid($role->getOwnerUuid()) |
72
|
|
|
->setAssignee('Role') |
73
|
|
|
->setAssigneeUuid($role->getUuid()) |
74
|
|
|
->setVersion(1); |
75
|
|
|
|
76
|
|
|
foreach ($scopes as $scope) { |
77
|
|
|
foreach ($scope['permissions'] as $entry) { |
78
|
|
|
$permission = new Permission; |
79
|
|
|
$permission |
80
|
|
|
->setScope($scope['scope']) |
81
|
|
|
->setEntity($scope['entity']) |
82
|
|
|
->setEntityUuid($scope['entityUuid']) |
83
|
|
|
->setKey($entry['key']) |
84
|
|
|
->setAttributes($entry['attributes']); |
85
|
|
|
$access->addPermission($permission); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->api->get($service.'.access')->create($access); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Delete access across all microservices |
98
|
|
|
* |
99
|
|
|
* @param \AppBundle\Entity\Role $role |
100
|
|
|
* @return \AppBundle\Service\RoleService |
101
|
|
|
* @throws \LogicException |
102
|
|
|
*/ |
103
|
|
|
public function deleteAccess(Role $role) |
104
|
|
|
{ |
105
|
|
|
if (null === $role->getUuid()) { |
|
|
|
|
106
|
|
|
throw new LogicException('Role does not have a uuid.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$services = $this->discoveryService->get(); |
110
|
|
|
|
111
|
|
|
foreach ($services as $service => $config) { |
112
|
|
|
if ( |
113
|
|
|
$config->enabled && |
114
|
|
|
property_exists($config, 'attributes') && |
115
|
|
|
property_exists($config->attributes, 'access') && |
116
|
|
|
$config->attributes->access |
117
|
|
|
) { |
118
|
|
|
$service = $this->api->get($service.'.access'); |
119
|
|
|
$parameters = new AccessParameters; |
120
|
|
|
$parameters |
121
|
|
|
->setAssignee('Role') |
122
|
|
|
->setAssigneeUuid($role->getUuid()); |
123
|
|
|
$accesses = $service->getList($parameters); |
|
|
|
|
124
|
|
|
|
125
|
|
|
foreach ($accesses as $access) { |
126
|
|
|
$service->delete($access); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|