1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Dashboard |
5
|
|
|
* @author Ian Olson <[email protected]> |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2015, Laraflock |
8
|
|
|
* @link https://github.com/laraflock |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Laraflock\Dashboard\Controllers; |
12
|
|
|
|
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use Illuminate\Http\Response; |
15
|
|
|
use Laracasts\Flash\Flash; |
16
|
|
|
use Laraflock\Dashboard\Exceptions\FormValidationException; |
17
|
|
|
use Laraflock\Dashboard\Exceptions\RolesException; |
18
|
|
|
|
19
|
|
|
class RolesController extends BaseDashboardController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Display a listing of the resource. |
23
|
|
|
* |
24
|
|
|
* @return Response |
25
|
|
|
*/ |
26
|
1 |
|
public function index() |
27
|
|
|
{ |
28
|
1 |
|
$roles = $this->roleRepositoryInterface->getAll(); |
29
|
|
|
|
30
|
1 |
|
return $this->view('roles.index')->with(['roles' => $roles]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new resource. |
35
|
|
|
* |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
1 |
|
public function create() |
39
|
|
|
{ |
40
|
1 |
|
$permissions = $this->permissionRepositoryInterface->getAll(); |
41
|
|
|
|
42
|
1 |
|
return $this->view('roles.create')->with(['permissions' => $permissions]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Store a newly created resource in storage. |
47
|
|
|
* |
48
|
|
|
* @param Request $request |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
2 |
View Code Duplication |
public function store(Request $request) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
try { |
55
|
2 |
|
$this->roleRepositoryInterface->create($request->all()); |
56
|
2 |
|
} catch (FormValidationException $e) { |
57
|
1 |
|
Flash::error($e->getMessage()); |
58
|
|
|
|
59
|
1 |
|
return redirect() |
60
|
1 |
|
->route('roles.create') |
61
|
1 |
|
->withErrors($e->getErrors()) |
62
|
1 |
|
->withInput(); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
Flash::success(trans('dashboard::dashboard.flash.role.create.success')); |
66
|
|
|
|
67
|
1 |
|
return redirect()->route('roles.index'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Show the form for editing the specified resource. |
72
|
|
|
* |
73
|
|
|
* @param int $id |
74
|
|
|
* |
75
|
|
|
* @return Response |
76
|
|
|
*/ |
77
|
2 |
|
public function edit($id) |
78
|
|
|
{ |
79
|
2 |
|
if (!$role = $this->roleRepositoryInterface->getById($id)) { |
80
|
1 |
|
Flash::error(trans('dashboard::dashboard.errors.role.found')); |
81
|
|
|
|
82
|
1 |
|
return redirect()->route('roles.index'); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$permissions = $this->permissionRepositoryInterface->getAll(); |
86
|
|
|
|
87
|
1 |
|
return $this->view('roles.edit')->with(['role' => $role, 'permissions' => $permissions]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Update the specified resource in storage. |
92
|
|
|
* |
93
|
|
|
* @param Request $request |
94
|
|
|
* @param $id |
95
|
|
|
* |
96
|
|
|
* @return Response |
97
|
|
|
*/ |
98
|
3 |
View Code Duplication |
public function update(Request $request, $id) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
try { |
101
|
3 |
|
$this->roleRepositoryInterface->update($request->all(), $id); |
102
|
3 |
|
} catch (FormValidationException $e) { |
103
|
1 |
|
Flash::error($e->getMessage()); |
104
|
|
|
|
105
|
1 |
|
return redirect() |
106
|
2 |
|
->route('roles.edit', ['id' => $id]) |
107
|
1 |
|
->withErrors($e->getErrors()) |
108
|
1 |
|
->withInput(); |
109
|
1 |
|
} catch (RolesException $e) { |
110
|
1 |
|
Flash::error($e->getMessage()); |
111
|
|
|
|
112
|
3 |
|
return redirect()->route('roles.index'); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
Flash::success(trans('dashboard::dashboard.flash.role.edit.success')); |
116
|
|
|
|
117
|
1 |
|
return redirect()->route('roles.edit', ['id' => $id]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Remove the specified resource from storage. |
122
|
|
|
* |
123
|
|
|
* @param int $id |
124
|
|
|
* |
125
|
|
|
* @return \Illuminate\Http\RedirectResponse |
126
|
|
|
*/ |
127
|
3 |
|
public function delete($id) |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
2 |
|
$this->roleRepositoryInterface->delete($id); |
131
|
3 |
|
} catch (RolesException $e) { |
132
|
2 |
|
Flash::error($e->getMessage()); |
133
|
|
|
|
134
|
1 |
|
return redirect()->route('roles.index'); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
Flash::success(trans('dashboard::dashboard.flash.role.delete.success')); |
138
|
|
|
|
139
|
1 |
|
return redirect()->route('roles.index'); |
140
|
|
|
} |
141
|
|
|
} |
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.