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
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
13
|
|
|
use Illuminate\Support\Collection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handle roles |
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
class RoleController extends Controller |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get all roles |
23
|
|
|
* |
24
|
|
|
* @param Request $request |
|
|
|
|
25
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
26
|
|
|
* @return LengthAwarePaginator |
|
|
|
|
27
|
|
|
*/ |
28
|
2 |
|
public function index(Request $request, RoleRepository $roleRepository) |
29
|
|
|
{ |
30
|
2 |
|
$perPage = $request->input('per_page', 10); |
31
|
2 |
|
$page = $request->input('page', 1); |
32
|
|
|
|
33
|
2 |
|
return $this->paginationResponse( |
34
|
2 |
|
$roleRepository->paginate($page, $perPage), |
35
|
2 |
|
$roleRepository->count() |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get information about a specific role |
41
|
|
|
* |
42
|
|
|
* @param Role $role |
|
|
|
|
43
|
|
|
* @return Role |
|
|
|
|
44
|
|
|
*/ |
45
|
1 |
|
public function show(Role $role) |
46
|
|
|
{ |
47
|
1 |
|
return $role; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a new role |
52
|
|
|
* |
53
|
|
|
* @param StoreRoleRequest $request |
|
|
|
|
54
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
55
|
|
|
* @param DataRoleRepository $dataRoleRepository |
|
|
|
|
56
|
|
|
* @return Role |
|
|
|
|
57
|
|
|
*/ |
58
|
2 |
|
public function store(StoreRoleRequest $request, RoleRepository $roleRepository, DataRoleRepository $dataRoleRepository) |
59
|
|
|
{ |
60
|
2 |
|
$dataRole = $dataRoleRepository->create( |
61
|
2 |
|
$request->input('role_name'), |
62
|
2 |
|
$request->input('email') |
63
|
|
|
); |
64
|
|
|
|
65
|
2 |
|
foreach($dataRole->getAdditionalAttributes() as $additionalAttribute) { |
|
|
|
|
66
|
1 |
|
if($request->has($additionalAttribute)) { |
|
|
|
|
67
|
1 |
|
$dataRole->saveAdditionalAttribute($additionalAttribute, $request->input($additionalAttribute)); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return $roleRepository->create($request->input('position_id'), $request->input('group_id'), $dataRole->id()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Update a role |
76
|
|
|
* |
77
|
|
|
* @param Role $role |
|
|
|
|
78
|
|
|
* @param UpdateRoleRequest $request |
|
|
|
|
79
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
80
|
|
|
* @param DataRoleRepository $dataRoleRepository |
|
|
|
|
81
|
|
|
* @return Role |
|
|
|
|
82
|
|
|
*/ |
83
|
2 |
|
public function update(Role $role, UpdateRoleRequest $request, RoleRepository $roleRepository, DataRoleRepository $dataRoleRepository) |
84
|
|
|
{ |
85
|
2 |
|
$dataRole = $role->data(); |
86
|
|
|
|
87
|
2 |
|
$dataRoleRepository->update( |
88
|
2 |
|
$dataRole->id(), |
89
|
2 |
|
$request->input('role_name', $dataRole->roleName()), |
90
|
2 |
|
$request->input('email', $dataRole->email()), |
91
|
|
|
); |
92
|
|
|
|
93
|
2 |
|
$roleRepository->update( |
94
|
2 |
|
$role->id(), |
95
|
2 |
|
$request->input('position_id', $role->positionId()), |
96
|
2 |
|
$request->input('group_id', $role->groupId()), |
97
|
2 |
|
$dataRole->id() |
98
|
|
|
); |
99
|
|
|
|
100
|
2 |
|
foreach($dataRole->getAdditionalAttributes() as $additionalAttribute) { |
|
|
|
|
101
|
1 |
|
if($request->has($additionalAttribute)) { |
|
|
|
|
102
|
1 |
|
$dataRole->saveAdditionalAttribute($additionalAttribute, $request->input($additionalAttribute)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
return $roleRepository->getById($role->id()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Delete a role |
111
|
|
|
* |
112
|
|
|
* @param Role $role |
|
|
|
|
113
|
|
|
* @param RoleRepository $roleRepository |
|
|
|
|
114
|
|
|
*/ |
|
|
|
|
115
|
1 |
|
public function destroy(Role $role, RoleRepository $roleRepository) |
116
|
|
|
{ |
117
|
1 |
|
$roleRepository->delete((int) $role->id()); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|