1 | <?php |
||||
2 | |||||
3 | namespace Thinkstudeo\Rakshak\Http\Controllers; |
||||
4 | |||||
5 | use Illuminate\Http\Request; |
||||
6 | use Thinkstudeo\Rakshak\Ability; |
||||
7 | use Thinkstudeo\Rakshak\Role; |
||||
8 | |||||
9 | class RolesController extends Controller |
||||
10 | { |
||||
11 | /** |
||||
12 | * Validation rules. |
||||
13 | * |
||||
14 | * @param \Illuminate\Http\Request $request |
||||
15 | * @return mixed |
||||
16 | */ |
||||
17 | protected function rules($role) |
||||
18 | { |
||||
19 | return [ |
||||
20 | 'name' => ['required', 'unique:roles,name,'.$role->id ?: null, 'string', 'min:3', 'max:255'], |
||||
21 | 'label' => ['required', 'string'], |
||||
22 | 'description' => ['required', 'string', 'max:255'], |
||||
23 | 'active' => ['nullable', 'boolean'], |
||||
24 | 'abilities' => ['nullable', 'array'], |
||||
25 | ]; |
||||
26 | } |
||||
27 | |||||
28 | /** |
||||
29 | * Display a listing of the resource. |
||||
30 | * |
||||
31 | * @return \Illuminate\Http\Response |
||||
32 | */ |
||||
33 | public function index() |
||||
34 | { |
||||
35 | $roles = Role::all(); |
||||
36 | |||||
37 | return view('rakshak::roles.index', compact('roles')); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
38 | } |
||||
39 | |||||
40 | /** |
||||
41 | * Show the form for creating a new resource. |
||||
42 | * |
||||
43 | * @return \Illuminate\Http\Response |
||||
44 | */ |
||||
45 | public function create() |
||||
46 | { |
||||
47 | $abilities = Ability::whereActive(1)->get(); |
||||
48 | $role = new Role(); |
||||
49 | |||||
50 | return view('rakshak::roles.create', compact('abilities', 'role')); |
||||
0 ignored issues
–
show
|
|||||
51 | } |
||||
52 | |||||
53 | /** |
||||
54 | * Store a newly created resource in storage. |
||||
55 | * |
||||
56 | * @param \Illuminate\Http\Request request |
||||
57 | * @return \Illuminate\Http\Response |
||||
58 | */ |
||||
59 | public function store(Request $request) |
||||
60 | { |
||||
61 | $this->authorize('create', Role::class); |
||||
62 | |||||
63 | $validated = $request->validate($this->rules($request)); |
||||
64 | |||||
65 | $role = Role::create(array_except($validated, ['abilities'])); |
||||
0 ignored issues
–
show
The function
array_except() has been deprecated: Arr::except() should be used directly instead. Will be removed in Laravel 6.0.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
66 | |||||
67 | if ($request->filled('abilities')) { |
||||
68 | foreach ($request->abilities as $ability) { |
||||
69 | $role->addAbility($ability['name']); |
||||
70 | } |
||||
71 | } |
||||
72 | |||||
73 | if ($request->expectsJson()) { |
||||
74 | return response()->json(['message' => "New role {$role->name} has been created.", 'record' => $role], 201); |
||||
0 ignored issues
–
show
|
|||||
75 | } |
||||
76 | |||||
77 | return redirect(route('rakshak.roles.index')) |
||||
0 ignored issues
–
show
|
|||||
78 | ->with('status', 'success') |
||||
79 | ->with('message', "Role {$role->name} has been created."); |
||||
80 | } |
||||
81 | |||||
82 | /** |
||||
83 | * Show the form for editing the specified resource. |
||||
84 | * |
||||
85 | * @param Thinkstudeo\Rakshak\Role $role |
||||
0 ignored issues
–
show
|
|||||
86 | * @return \Illuminate\Http\Response |
||||
87 | */ |
||||
88 | public function edit(Role $role) |
||||
89 | { |
||||
90 | $abilities = Ability::whereActive(true)->get(); |
||||
91 | |||||
92 | return view('rakshak::roles.edit', compact('role', 'abilities')); |
||||
0 ignored issues
–
show
|
|||||
93 | } |
||||
94 | |||||
95 | /** |
||||
96 | * Update the specified resource in storage. |
||||
97 | * |
||||
98 | * @param \Illuminate\Http\Request $request |
||||
99 | * @param \Thinkstudeo\Rakshak\Role $role |
||||
100 | * @return \Illuminate\Http\Response |
||||
101 | */ |
||||
102 | public function update(Request $request, Role $role) |
||||
103 | { |
||||
104 | $this->authorize('update', $role); |
||||
105 | |||||
106 | $validated = $request->validate($this->rules($role)); |
||||
107 | |||||
108 | $role = tap($role)->update(array_except($validated, ['abilities'])); |
||||
0 ignored issues
–
show
The function
array_except() has been deprecated: Arr::except() should be used directly instead. Will be removed in Laravel 6.0.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
109 | |||||
110 | if ($request->filled('abilities')) { |
||||
111 | $abilityIds = []; |
||||
112 | foreach ($request->abilities as $ability) { |
||||
113 | $abilityIds[] = $ability['id']; |
||||
114 | } |
||||
115 | $role->abilities()->sync($abilityIds); |
||||
116 | } |
||||
117 | |||||
118 | if ($request->expectsJson()) { |
||||
119 | return response()->json(['message' => "The role {$role->name} has been updated.", 'record' => $role], 200); |
||||
0 ignored issues
–
show
|
|||||
120 | } |
||||
121 | |||||
122 | return redirect() |
||||
0 ignored issues
–
show
|
|||||
123 | ->back() |
||||
124 | ->with('status', 'success') |
||||
125 | ->with('message', "Role {$role->name} updated successfully."); |
||||
126 | } |
||||
127 | |||||
128 | /** |
||||
129 | * Remove the specified resource from storage. |
||||
130 | * |
||||
131 | * @param \Thinkstudeo\Rakshak\Role $role |
||||
132 | * @return \Illuminate\Http\Response |
||||
133 | */ |
||||
134 | public function destroy(Role $role) |
||||
135 | { |
||||
136 | $role->delete(); |
||||
137 | |||||
138 | if (request()->expectsJson()) { |
||||
139 | return response()->json(['message' => "The role {$role->name} has been deleted."], 200); |
||||
0 ignored issues
–
show
|
|||||
140 | } |
||||
141 | |||||
142 | return redirect(route('rakshak.roles.index')) |
||||
0 ignored issues
–
show
|
|||||
143 | ->with('status', 'success') |
||||
144 | ->with('message', "Role {$role->name} has been deleted."); |
||||
145 | } |
||||
146 | } |
||||
147 |