1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Http\Controllers\Role; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Http\Controllers\Controller; |
6
|
|
|
use BristolSU\ControlDB\Http\Requests\Api\Role\StoreRoleRequest; |
7
|
|
|
use BristolSU\ControlDB\Http\Requests\Api\Role\UpdateRoleRequest; |
8
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\DataRole as DataRoleRepository; |
9
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Role as RoleRepository; |
10
|
|
|
use BristolSU\ControlDB\Contracts\Models\Role; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Handle roles |
14
|
|
|
*/ |
|
|
|
|
15
|
|
|
class RoleController extends Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get all roles |
20
|
|
|
* |
21
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
22
|
|
|
* @return Role[]|\Illuminate\Support\Collection |
|
|
|
|
23
|
|
|
*/ |
24
|
1 |
|
public function index(RoleRepository $roleRepository) |
25
|
|
|
{ |
26
|
1 |
|
return $roleRepository->all(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get information about a specific role |
31
|
|
|
* |
32
|
|
|
* @param Role $role |
|
|
|
|
33
|
|
|
* @return Role |
|
|
|
|
34
|
|
|
*/ |
35
|
1 |
|
public function show(Role $role) |
36
|
|
|
{ |
37
|
1 |
|
return $role; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new role |
42
|
|
|
* |
43
|
|
|
* @param StoreRoleRequest $request |
|
|
|
|
44
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
45
|
|
|
* @param DataRoleRepository $dataRoleRepository |
|
|
|
|
46
|
|
|
* @return Role |
|
|
|
|
47
|
|
|
*/ |
48
|
2 |
|
public function store(StoreRoleRequest $request, RoleRepository $roleRepository, DataRoleRepository $dataRoleRepository) |
49
|
|
|
{ |
50
|
2 |
|
$dataRole = $dataRoleRepository->create( |
51
|
2 |
|
$request->input('role_name'), |
52
|
2 |
|
$request->input('email') |
53
|
|
|
); |
54
|
|
|
|
55
|
2 |
|
foreach($dataRole->getAdditionalAttributes() as $additionalAttribute) { |
|
|
|
|
56
|
1 |
|
if($request->has($additionalAttribute)) { |
|
|
|
|
57
|
1 |
|
$dataRole->saveAdditionalAttribute($additionalAttribute, $request->input($additionalAttribute)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return $roleRepository->create($request->input('position_id'), $request->input('group_id'), $dataRole->id()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Update a role |
66
|
|
|
* |
67
|
|
|
* @param Role $role |
|
|
|
|
68
|
|
|
* @param UpdateRoleRequest $request |
|
|
|
|
69
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
70
|
|
|
* @param DataRoleRepository $dataRoleRepository |
|
|
|
|
71
|
|
|
* @return Role |
|
|
|
|
72
|
|
|
*/ |
73
|
2 |
|
public function update(Role $role, UpdateRoleRequest $request, RoleRepository $roleRepository, DataRoleRepository $dataRoleRepository) |
|
|
|
|
74
|
|
|
{ |
75
|
2 |
|
$dataRole = $role->data(); |
76
|
|
|
|
77
|
2 |
|
if($request->input('role_name') !== null) { |
|
|
|
|
78
|
2 |
|
$dataRole->setRoleName($request->input('role_name')); |
79
|
|
|
} |
80
|
2 |
|
if($request->input('email') !== null) { |
|
|
|
|
81
|
2 |
|
$dataRole->setEmail($request->input('email')); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
if($request->input('position_id') !== null) { |
|
|
|
|
85
|
1 |
|
$role->setPositionId($request->input('position_id')); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
if($request->input('group_id') !== null) { |
|
|
|
|
89
|
1 |
|
$role->setGroupId($request->input('group_id')); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
foreach($dataRole->getAdditionalAttributes() as $additionalAttribute) { |
|
|
|
|
93
|
1 |
|
if($request->has($additionalAttribute)) { |
|
|
|
|
94
|
1 |
|
$dataRole->saveAdditionalAttribute($additionalAttribute, $request->input($additionalAttribute)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return $role; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete a role |
103
|
|
|
* |
104
|
|
|
* @param Role $role |
|
|
|
|
105
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
106
|
|
|
*/ |
|
|
|
|
107
|
1 |
|
public function destroy(Role $role, RoleRepository $roleRepository) |
108
|
|
|
{ |
109
|
1 |
|
$roleRepository->delete((int) $role->id()); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|