1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\AssignRoles\Support; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\Group; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Position; |
7
|
|
|
use BristolSU\Support\Authentication\Contracts\Authentication; |
8
|
|
|
use BristolSU\Support\Logic\Contracts\Audience\LogicAudience; |
9
|
|
|
use BristolSU\Support\Logic\Contracts\LogicRepository; |
10
|
|
|
use BristolSU\Support\Logic\Contracts\LogicTester; |
11
|
|
|
use BristolSU\Support\ModuleInstance\ModuleInstance; |
12
|
|
|
use Exception; |
13
|
|
|
use Illuminate\Support\Facades\Cache; |
14
|
|
|
|
15
|
|
|
class PositionSettingRetrieval |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @var LogicRepository |
20
|
|
|
*/ |
21
|
|
|
private $logicRepository; |
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @var LogicAudience |
24
|
|
|
*/ |
25
|
|
|
private $logicAudience; |
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @var Position |
28
|
|
|
*/ |
29
|
|
|
private $positionRepository; |
|
|
|
|
30
|
|
|
|
31
|
|
|
public function __construct(LogicRepository $logicRepository, LogicAudience $logicAudience, Position $positionRepository) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->logicRepository = $logicRepository; |
34
|
|
|
$this->logicAudience = $logicAudience; |
35
|
|
|
$this->positionRepository = $positionRepository; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getSettings(Group $group) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$settings = settings('position_settings', []); |
41
|
|
|
foreach ($settings as $setting) { |
42
|
|
|
if ($this->groupIsForSetting($group, $setting)) { |
43
|
|
|
return $setting; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
throw new SettingRetrievalException('Could not find a setting'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function groupIsForSetting(Group $group, array $setting): bool |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$user = app(Authentication::class)->getUser(); |
52
|
|
|
$role = app(Authentication::class)->getRole(); |
53
|
|
|
|
54
|
|
|
if (!array_key_exists('logic_id', $setting)) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
$logic = $this->logicRepository->getById($setting['logic_id']); |
58
|
|
|
return app(LogicTester::class)->evaluate($logic, $user, $group, $role); |
59
|
|
|
// $groups = collect($this->logicAudience->groupAudience($logic))->map(function (Group $group) { |
|
|
|
|
60
|
|
|
// return $group->id(); |
|
|
|
|
61
|
|
|
// }); |
|
|
|
|
62
|
|
|
// return $groups->contains($group->id()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getCacheKey(Group $group) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$moduleInstance = app(ModuleInstance::class); |
68
|
|
|
if($moduleInstance->exists) { |
|
|
|
|
69
|
|
|
throw new \Exception('Module instance was used in ' . static::class . ' but was not bound to the container'); |
70
|
|
|
} |
71
|
|
|
return PositionSettingRetrieval::class . '.' . $moduleInstance->id() . '.' . $group->id(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function parse($setting) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
foreach ($setting['allowed'] as $index => $id) { |
77
|
|
|
$setting['allowed'][$index] = $this->getPosition($id); |
78
|
|
|
} |
79
|
|
|
foreach ($setting['only_one_role'] as $index => $id) { |
80
|
|
|
$setting['only_one_role'][$index] = $this->getPosition($id); |
81
|
|
|
} |
82
|
|
|
foreach ($setting['only_one_user'] as $index => $id) { |
83
|
|
|
$setting['only_one_user'][$index] = $this->getPosition($id); |
84
|
|
|
} |
85
|
|
|
foreach ($setting['user_only_has_one_role'] as $index => $id) { |
86
|
|
|
$setting['user_only_has_one_role'][$index] = $this->getPosition($id); |
87
|
|
|
} |
88
|
|
|
return $setting; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function getPosition($id) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
return $this->positionRepository->getById($id); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |