RoleController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A index() 0 6 1
A edit() 0 8 1
A update() 0 17 1
A destroy() 0 9 1
A store() 0 15 1
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Back\Authorization;
4
5
use Illuminate\Contracts\View\Factory;
6
use Illuminate\Contracts\View\View;
7
use Illuminate\Http\Request;
8
use Thinktomorrow\Chief\Admin\Authorization\Permission;
9
use Thinktomorrow\Chief\Admin\Authorization\Role;
10
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
11
use Thinktomorrow\Chief\Forms\Fields\Concerns\Select\PairOptions;
12
13
class RoleController extends Controller
14
{
15
    /**
16
     * @return Factory|View
17
     */
18
    public function index()
19
    {
20
        $this->authorize('view-role');
21
22
        return view('chief::admin.authorization.roles.index', [
23
            'roles' => Role::all(),
24
        ]);
25
    }
26
27
    public function store(Request $request)
28
    {
29
        $this->authorize('create-role');
30
31
        $this->validate($request, [
32
            'name' => 'required|unique:roles',
33
            'permission_names' => 'required|array',
34
            'permission_names.*' => 'required', // Avoid null array entry
35
        ]);
36
37
        $role = Role::create($request->only('name'));
38
        $role->givePermissionTo($request->permission_names);
39
40
        return redirect()->route('chief.back.roles.index')
41
            ->with('messages.success', 'Rol ' . $role->name . ' is toegevoegd.');
42
    }
43
44
    /**
45
     * @return Factory|View
46
     */
47
    public function create()
48
    {
49
        $this->authorize('create-role');
50
51
        return view('chief::admin.authorization.roles.create', [
52
            'role' => new Role(),
53
            'permission_names' => PairOptions::toMultiSelectPairs(Permission::all()->pluck('name')->toArray()),
54
        ]);
55
    }
56
57
    /**
58
     * @return Factory|View
59
     */
60
    public function edit($id)
61
    {
62
        $this->authorize('update-role');
63
64
        $role = Role::findOrFail($id);
65
        $permission_names = PairOptions::toMultiSelectPairs(Permission::all()->pluck('name')->toArray());
66
67
        return view('chief::admin.authorization.roles.edit', compact('role', 'permission_names'));
68
    }
69
70
    public function update(Request $request, $id)
71
    {
72
        $this->authorize('update-role');
73
74
        $this->validate($request, [
75
            'name' => 'required|alpha_dash|unique:roles,name,' . $id,
76
            'permission_names' => 'required|array',
77
            'permission_names.*' => 'required', // Avoid null array entry
78
        ]);
79
80
        $role = Role::findOrFail($id);
81
        $role->name = $request->name;
82
        $role->save();
83
        $role->syncPermissions($request->permission_names);
84
85
        return redirect()->route('chief.back.roles.index')
86
            ->with('messages.success', 'Rol ' . $role->name . ' is aangepast.');
87
    }
88
89
    public function destroy($id)
90
    {
91
        $this->authorize('delete-role');
92
93
        $role = Role::findOrFail($id);
94
        $role->delete();
95
96
        return redirect()->route('chief.back.roles.index')
97
            ->with('messages.success', 'Rol ' . $role->name . ' is verwijderd.');
98
    }
99
}
100