1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
6
|
|
|
use Microboard\Http\Requests\Role\UpdateFormRequest; |
7
|
|
|
use Microboard\Http\Requests\Role\StoreFormRequest; |
8
|
|
|
use Microboard\DataTables\RoleDataTable; |
9
|
|
|
use Illuminate\Http\RedirectResponse; |
10
|
|
|
use Illuminate\Http\Response; |
11
|
|
|
use Illuminate\View\View; |
12
|
|
|
use Exception; |
13
|
|
|
use Microboard\Models\Role; |
14
|
|
|
|
15
|
|
|
class RoleController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Display a listing of the resource. |
19
|
|
|
* |
20
|
|
|
* @param RoleDataTable $table |
21
|
|
|
* @return Response |
22
|
|
|
* @throws AuthorizationException |
23
|
|
|
*/ |
24
|
|
|
public function index(RoleDataTable $table) |
25
|
|
|
{ |
26
|
|
|
$this->authorize('viewAny', new Role); |
27
|
|
|
return $table->render('microboard::roles.index'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Show the form for creating a new resource. |
32
|
|
|
* |
33
|
|
|
* @return View |
34
|
|
|
* @throws AuthorizationException |
35
|
|
|
*/ |
36
|
|
|
public function create() |
37
|
|
|
{ |
38
|
|
|
$this->authorize('create', new Role); |
39
|
|
|
return view('microboard::roles.create'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Store a newly created resource in storage. |
44
|
|
|
* |
45
|
|
|
* @param StoreFormRequest $request |
46
|
|
|
* @return RedirectResponse |
47
|
|
|
* @throws AuthorizationException |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function store(StoreFormRequest $request) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->authorize('create', new Role); |
52
|
|
|
$role = Role::create($request->only(['name', 'display_name'])); |
53
|
|
|
$role->permissions()->attach($request->get('permissions')); |
54
|
|
|
return redirect()->route('microboard.roles.show', $role); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Display the specified resource. |
59
|
|
|
* |
60
|
|
|
* @param Role $role |
61
|
|
|
* @return View |
62
|
|
|
* @throws AuthorizationException |
63
|
|
|
*/ |
64
|
|
|
public function show(Role $role) |
65
|
|
|
{ |
66
|
|
|
$this->authorize('view', $role); |
67
|
|
|
return view('microboard::roles.show', compact('role')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Show the form for editing the specified resource. |
72
|
|
|
* |
73
|
|
|
* @param Role $role |
74
|
|
|
* @return View |
75
|
|
|
* @throws AuthorizationException |
76
|
|
|
*/ |
77
|
|
|
public function edit(Role $role) |
78
|
|
|
{ |
79
|
|
|
$this->authorize('update', $role); |
80
|
|
|
return view('microboard::roles.edit', compact('role')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Update the specified resource in storage. |
85
|
|
|
* |
86
|
|
|
* @param UpdateFormRequest $request |
87
|
|
|
* @param Role $role |
88
|
|
|
* @return RedirectResponse |
89
|
|
|
* @throws AuthorizationException |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function update(UpdateFormRequest $request, Role $role) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$this->authorize('update', $role); |
94
|
|
|
$role->update($request->only(['name', 'display_name'])); |
95
|
|
|
$role->permissions()->sync($request->get('permissions')); |
96
|
|
|
return redirect()->route('microboard.roles.show', $role); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Remove the specified resource from storage. |
101
|
|
|
* |
102
|
|
|
* @param Role $role |
103
|
|
|
* @return RedirectResponse |
104
|
|
|
* @throws AuthorizationException |
105
|
|
|
* @throws Exception |
106
|
|
|
*/ |
107
|
|
|
public function destroy(Role $role) |
108
|
|
|
{ |
109
|
|
|
$this->authorize('update', $role); |
110
|
|
|
$role->delete(); |
111
|
|
|
return redirect()->route('microboard.roles.index'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.