|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Backend; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use Spatie\Permission\Models\Permission; |
|
7
|
|
|
use Spatie\Permission\Models\Role; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
|
|
11
|
|
|
class RoleController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->middleware(function ($request, $next) { |
|
16
|
|
|
if(!auth()->user()->getRoleNames()->contains('super-admin')) |
|
17
|
|
|
abort(403, 'Access denied'); |
|
18
|
|
|
return $next($request); |
|
19
|
|
|
}); |
|
20
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Show the application roles index. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function index(Request $request) |
|
27
|
|
|
{ |
|
28
|
|
|
$sortDirection = $request->input('sort_dir') ?: 'asc'; |
|
29
|
|
|
$sortColumn = $request->input('sort') ?: 'created_at'; |
|
30
|
|
|
return view('backend.roles.index', [ |
|
31
|
|
|
'roles' => Role::orderBy($sortColumn, $sortDirection)->paginate(10) |
|
32
|
|
|
]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Display the specified resource edit form. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function edit(Role $role) |
|
39
|
|
|
{ |
|
40
|
|
|
return view('backend.roles.edit', [ |
|
41
|
|
|
'role' => $role, |
|
42
|
|
|
'groupedPermissions' => Permission::all('name', 'id')->map(function ($perm) { |
|
43
|
|
|
$perm->name = str_replace('_', ' ', $perm->name); |
|
44
|
|
|
return $perm; |
|
45
|
|
|
})->groupBy(function ($perm) { |
|
46
|
|
|
foreach (array_keys(app('laragen')->getModules()) as $module) { |
|
|
|
|
|
|
47
|
|
|
if(Str::contains($perm->name, str_replace('_', ' ', $module))) |
|
48
|
|
|
return Str::title(str_replace('_', ' ', $module)); |
|
49
|
|
|
} |
|
50
|
|
|
return "Others"; |
|
51
|
|
|
}), |
|
52
|
|
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Show the form for creating a new resource. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function create() |
|
59
|
|
|
{ |
|
60
|
|
|
return view('backend.roles.create', [ |
|
61
|
|
|
'groupedPermissions' => Permission::all('name', 'id')->map(function ($perm) { |
|
62
|
|
|
$perm->name = str_replace('_', ' ', $perm->name); |
|
63
|
|
|
return $perm; |
|
64
|
|
|
})->groupBy(function ($perm) { |
|
65
|
|
|
foreach (array_keys(app('laragen')->getModules()) as $module) { |
|
66
|
|
|
if(Str::contains($perm->name, str_replace('_', ' ', $module))) |
|
67
|
|
|
return Str::title(str_replace('_', ' ', $module)); |
|
68
|
|
|
} |
|
69
|
|
|
return "Others"; |
|
70
|
|
|
}), |
|
71
|
|
|
]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Store a newly created resource in storage. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function store(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!$request->name) |
|
80
|
|
|
return redirect()->back()->withErrors(['name' => 'Name cannot be empty.']); |
|
81
|
|
|
|
|
82
|
|
|
$role = Role::create(['name' => $request->name]); |
|
83
|
|
|
$role->permissions()->attach($request->permissions); |
|
84
|
|
|
return redirect()->route('backend.roles.index')->withSuccess(__('Role successfully created.')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Update the specified resource in storage. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function update(Request $request, Role $role) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($request->has("permissions")) { |
|
93
|
|
|
$role->permissions()->sync($request->input("permissions")); |
|
94
|
|
|
} |
|
95
|
|
|
$role->update(['name' => $request->name]); |
|
96
|
|
|
return redirect()->route('backend.roles.index')->withSuccess(__('Role successfully updated.')); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Remove the specified resource from storage. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function destroy(Role $role) |
|
103
|
|
|
{ |
|
104
|
|
|
$role->delete(); |
|
105
|
|
|
|
|
106
|
|
|
return redirect()->route('backend.roles.index')->withSuccess(__('roles.deleted')); |
|
107
|
|
|
} |
|
108
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.