|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\AssignRoles\Http\Controllers\ParticipantApi; |
|
4
|
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\DataRole; |
|
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Role; |
|
7
|
|
|
use BristolSU\Module\AssignRoles\Events\RoleCreated; |
|
8
|
|
|
use BristolSU\Module\AssignRoles\Http\Controllers\Controller; |
|
9
|
|
|
use BristolSU\Module\AssignRoles\Http\Requests\ParticipantApi\RoleController\DestroyRequest; |
|
10
|
|
|
use BristolSU\Module\AssignRoles\Http\Requests\ParticipantApi\RoleController\StoreRequest; |
|
11
|
|
|
use BristolSU\Module\AssignRoles\Http\Requests\ParticipantApi\RoleController\UpdateRequest; |
|
12
|
|
|
use BristolSU\Support\Activity\Activity; |
|
13
|
|
|
use BristolSU\Support\Authentication\Contracts\Authentication; |
|
14
|
|
|
use BristolSU\Support\Logic\Contracts\LogicRepository; |
|
15
|
|
|
use BristolSU\Support\Logic\Contracts\LogicTester; |
|
16
|
|
|
use BristolSU\Support\ModuleInstance\ModuleInstance; |
|
17
|
|
|
|
|
18
|
|
|
class RoleController extends Controller |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
|
|
|
|
|
22
|
|
|
* @var LogicTester |
|
23
|
|
|
*/ |
|
24
|
|
|
private $logicTester; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
25 |
|
public function __construct(LogicTester $logicTester) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
25 |
|
$this->logicTester = $logicTester; |
|
29
|
25 |
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
public function index(Activity $activity, ModuleInstance $moduleInstance, Role $role, Authentication $authentication, LogicRepository $logicRepository) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
3 |
|
$this->authorize('role.index'); |
|
34
|
2 |
|
$roles = $role->allThroughGroup( |
|
35
|
2 |
|
$authentication->getGroup() |
|
|
|
|
|
|
36
|
|
|
); |
|
37
|
2 |
|
if(settings('logic_group', null) !== null) { |
|
|
|
|
|
|
38
|
1 |
|
$logic = $logicRepository->getById(settings('logic_group')); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
$roles = $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) use ($logic) { |
|
|
|
|
|
|
41
|
1 |
|
return $this->logicTester->evaluate($logic, null, $role->group(), $role); |
|
42
|
1 |
|
})->values(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $roles->map(function(\BristolSU\ControlDB\Contracts\Models\Role $role) { |
|
|
|
|
|
|
46
|
2 |
|
$roleArray = $role->toArray(); |
|
47
|
2 |
|
$roleArray['position'] = $role->position(); |
|
48
|
2 |
|
$roleArray['users'] = $role->users(); |
|
49
|
2 |
|
return $roleArray; |
|
50
|
2 |
|
}); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
7 |
|
public function store(Activity $activity, ModuleInstance $moduleInstance, StoreRequest $request, Role $roleRepository, DataRole $dataRoleRepository, Authentication $authentication) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
7 |
|
$this->authorize('role.store'); |
|
56
|
|
|
|
|
57
|
6 |
|
$dataRole = $dataRoleRepository->create( |
|
58
|
6 |
|
$request->input('role_name'), |
|
59
|
6 |
|
$request->input('email') |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
6 |
|
$role = $roleRepository->create( |
|
63
|
6 |
|
$request->input('position_id'), |
|
64
|
6 |
|
$authentication->getGroup()->id(), |
|
65
|
6 |
|
$dataRole->id() |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
6 |
|
event(new RoleCreated($role)); |
|
69
|
|
|
|
|
70
|
6 |
|
return $role; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
public function destroy(Activity $activity, ModuleInstance $moduleInstance, int $roleId, DestroyRequest $request, Role $roleRepository) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
2 |
|
$this->authorize('role.delete'); |
|
76
|
|
|
|
|
77
|
1 |
|
$roleRepository->delete($roleId); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
5 |
|
public function update(Activity $activity, ModuleInstance $moduleInstance, int $roleId, UpdateRequest $request, Role $roleRepository, DataRole $dataRoleRepository) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
5 |
|
$this->authorize('role.update'); |
|
83
|
|
|
|
|
84
|
5 |
|
$role = $roleRepository->getById($roleId); |
|
85
|
5 |
|
$dataRole = $role->data(); |
|
86
|
5 |
|
$dataRole->setEmail($request->input('email', $dataRole->email())); |
|
87
|
5 |
|
$dataRole->setRoleName($request->input('role_name', $dataRole->roleName())); |
|
88
|
5 |
|
return $dataRole; |
|
89
|
|
|
} |
|
90
|
|
|
} |