1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\AssignRoles\Support; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\Role as RoleModel; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Role; |
7
|
|
|
use BristolSU\Support\Logic\Contracts\LogicRepository; |
8
|
|
|
use BristolSU\Support\Logic\Facade\LogicTester; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
|
11
|
|
|
class LogicRoleRepository implements Role |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* @var Role |
16
|
|
|
*/ |
17
|
|
|
private $roleRepository; |
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @var \BristolSU\Support\Logic\Contracts\LogicTester |
20
|
|
|
*/ |
21
|
|
|
private $logicTester; |
|
|
|
|
22
|
|
|
|
23
|
38 |
|
public function __construct(Role $roleRepository, \BristolSU\Support\Logic\Contracts\LogicTester $logicTester) |
|
|
|
|
24
|
|
|
{ |
25
|
38 |
|
$this->roleRepository = $roleRepository; |
26
|
38 |
|
$this->logicTester = $logicTester; |
27
|
38 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
|
|
|
|
32
|
22 |
|
public function getById(int $id): RoleModel |
33
|
|
|
{ |
34
|
22 |
|
return $this->roleRepository->getById($id); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
|
|
|
|
40
|
|
|
public function all(): Collection |
41
|
|
|
{ |
42
|
|
|
return $this->filter($this->roleRepository->all()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
|
|
|
|
48
|
|
|
public function getByDataProviderId(int $dataProviderId): RoleModel |
49
|
|
|
{ |
50
|
|
|
return $this->roleRepository->getByDataProviderId($dataProviderId); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
|
|
|
|
56
|
6 |
|
public function create(int $positionId, int $groupId, int $dataProviderId): RoleModel |
57
|
|
|
{ |
58
|
6 |
|
return $this->roleRepository->create($positionId, $groupId, $dataProviderId); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
|
|
|
|
64
|
1 |
|
public function delete(int $id): void |
65
|
|
|
{ |
66
|
1 |
|
$this->roleRepository->delete($id); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
|
|
|
|
72
|
5 |
|
public function allThroughGroup(\BristolSU\ControlDB\Contracts\Models\Group $group): Collection |
73
|
|
|
{ |
74
|
5 |
|
return $this->filter($this->roleRepository->allThroughGroup($group)); |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
|
|
|
|
81
|
|
|
public function allThroughPosition(\BristolSU\ControlDB\Contracts\Models\Position $position): Collection |
82
|
|
|
{ |
83
|
|
|
return $this->filter($this->roleRepository->allThroughPosition($position)); |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
private function hasLogicGroup() |
|
|
|
|
87
|
|
|
{ |
88
|
5 |
|
return $this->logicGroupId() !== null; |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
private function logicGroupId() |
|
|
|
|
92
|
|
|
{ |
93
|
5 |
|
$id = settings('logic_group', null); |
94
|
5 |
|
if($id === null) { |
|
|
|
|
95
|
4 |
|
return null; |
96
|
|
|
} |
97
|
1 |
|
return (int) $id; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
private function logicGroup() |
|
|
|
|
101
|
|
|
{ |
102
|
1 |
|
return app(LogicRepository::class)->getById( |
103
|
1 |
|
$this->logicGroupId() |
|
|
|
|
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
private function isInLogicGroup(\BristolSU\ControlDB\Contracts\Models\Role $role) |
|
|
|
|
108
|
|
|
{ |
109
|
1 |
|
return $this->logicTester->evaluate($this->logicGroup(), null, $role->group(), $role); |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
private function filter(Collection $roles) |
|
|
|
|
113
|
|
|
{ |
114
|
5 |
|
if($this->hasLogicGroup()) { |
|
|
|
|
115
|
|
|
return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) { |
|
|
|
|
116
|
1 |
|
return $this->isInLogicGroup($role); |
117
|
1 |
|
})->values(); |
|
|
|
|
118
|
|
|
} |
119
|
4 |
|
return $roles; |
120
|
|
|
} |
121
|
|
|
} |