Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
10 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RoleController | |
50.00% |
10 / 20 |
|
75.00% |
3 / 4 |
10.50 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| edit | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| update | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\admin; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Services\Permission\PermissionService; |
| 7 | use App\Services\Permission\RoleService; |
| 8 | use Illuminate\Http\RedirectResponse; |
| 9 | use Illuminate\Http\Request; |
| 10 | use Illuminate\View\View; |
| 11 | |
| 12 | /** |
| 13 | * People → Roles. Tick the things a role is allowed to do — in plain |
| 14 | * language. What each role can do is shown live as you go. |
| 15 | */ |
| 16 | class RoleController extends Controller |
| 17 | { |
| 18 | public function __construct( |
| 19 | private RoleService $roles, |
| 20 | private PermissionService $permissions, |
| 21 | ) {} |
| 22 | |
| 23 | public function index(): View |
| 24 | { |
| 25 | return view('admin.roles.index', [ |
| 26 | 'roles' => $this->roles->all(), |
| 27 | 'rbacOn' => $this->permissions->enforced(), |
| 28 | ]); |
| 29 | } |
| 30 | |
| 31 | public function edit(string $role): View |
| 32 | { |
| 33 | $groups = $this->permissions->groups(); |
| 34 | |
| 35 | $abilityLabels = []; |
| 36 | foreach ($groups as $group => $abilities) { |
| 37 | foreach ($abilities as $key => $def) { |
| 38 | $abilityLabels[$key] = ['group' => $group, 'label' => $def['label']]; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return view('admin.roles.edit', [ |
| 43 | 'role' => $this->roles->present($this->roles->findByName($role)), |
| 44 | 'groups' => $groups, |
| 45 | 'abilityLabels' => $abilityLabels, |
| 46 | ]); |
| 47 | } |
| 48 | |
| 49 | public function update(Request $request, string $role): RedirectResponse |
| 50 | { |
| 51 | $model = $this->roles->findByName($role); |
| 52 | |
| 53 | $this->roles->update($model, (array) $request->input('abilities', [])); |
| 54 | |
| 55 | return redirect() |
| 56 | ->route('admin.roles.index') |
| 57 | ->with('success', "“{$model->name}” permissions saved."); |
| 58 | } |
| 59 | } |