1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Controllers\Admin\Role; |
3
|
|
|
|
4
|
|
|
use Illuminate\Http\RedirectResponse; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\View\View; |
7
|
|
|
use Xetaravel\Models\Role; |
8
|
|
|
use Ultraware\Roles\Models\Permission; |
9
|
|
|
use Xetaravel\Http\Controllers\Admin\Controller; |
10
|
|
|
use Xetaravel\Models\Validators\RoleValidator; |
11
|
|
|
use Xetaravel\Models\Repositories\RoleRepository; |
12
|
|
|
|
13
|
|
|
class RoleController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Show the search page. |
17
|
|
|
* |
18
|
|
|
* @return \Illuminate\View\View |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function index(): View |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$roles = Role::paginate(10); |
23
|
|
|
|
24
|
|
|
$breadcrumbs = $this->breadcrumbs->addCrumb('Manage Roles', route('admin.role.role.index')); |
|
|
|
|
25
|
|
|
|
26
|
|
|
return view('Admin::Role.role.index', compact('roles', 'breadcrumbs')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Show the role create form. |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\View\View |
33
|
|
|
*/ |
34
|
|
|
public function showCreateForm(): View |
35
|
|
|
{ |
36
|
|
|
$permissions = Permission::pluck('name', 'id'); |
37
|
|
|
$attributes = Permission::pluck('id')->toArray(); |
38
|
|
|
|
39
|
|
|
$optionsAttributes = []; |
40
|
|
View Code Duplication |
foreach ($attributes as $attribute) { |
|
|
|
|
41
|
|
|
$optionsAttributes[$attribute] = [ |
42
|
|
|
'title' => 'Role Information', |
43
|
|
|
'data-content' => Permission::where('id', $attribute)->select('description')->first()->description, |
44
|
|
|
'data-toggle' => 'popover', |
45
|
|
|
'data-trigger' => 'hover', |
46
|
|
|
'data-placement' => 'top' |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$breadcrumbs = $this->breadcrumbs |
51
|
|
|
->addCrumb('Manage Roles', route('admin.role.role.index')) |
52
|
|
|
->addCrumb("Create", route('admin.role.role.create')); |
53
|
|
|
|
54
|
|
|
return view('Admin::Role.role.create', compact('permissions', 'breadcrumbs', 'optionsAttributes')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Show the update form. |
59
|
|
|
* |
60
|
|
|
* @param \Illuminate\Http\Request $request |
61
|
|
|
* @param string $slug The slug of the role. |
62
|
|
|
* @param int $id The id of the role. |
63
|
|
|
* |
64
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public function showUpdateForm(Request $request, string $slug, int $id) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$role = Role::findOrFail($id); |
69
|
|
|
|
70
|
|
|
$permissions = Permission::pluck('name', 'id'); |
71
|
|
|
$attributes = Permission::pluck('id')->toArray(); |
72
|
|
|
$permission = Permission::where('slug', 'access.administration')->first(); |
73
|
|
|
|
74
|
|
|
$optionsAttributes = []; |
75
|
|
View Code Duplication |
foreach ($attributes as $attribute) { |
|
|
|
|
76
|
|
|
$optionsAttributes[$attribute] = [ |
77
|
|
|
'title' => 'Role Information', |
78
|
|
|
'data-content' => Permission::where('id', $attribute)->select('description')->first()->description, |
79
|
|
|
'data-toggle' => 'popover', |
80
|
|
|
'data-trigger' => 'hover', |
81
|
|
|
'data-placement' => 'top' |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$breadcrumbs = $this->breadcrumbs |
86
|
|
|
->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0') |
87
|
|
|
->addCrumb('Manage Roles', route('admin.role.role.index')) |
88
|
|
|
->addCrumb( |
89
|
|
|
'Update ' . e($role->name), |
90
|
|
|
route('admin.role.role.update', $role->slug, $role->id) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return view( |
94
|
|
|
'Admin::Role.role.update', |
95
|
|
|
compact('role', 'permissions', 'breadcrumbs', 'optionsAttributes', 'permission') |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Handle an user update request for the application. |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Http\Request $request |
103
|
|
|
* @param int $id The id of the role to update. |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Http\RedirectResponse |
106
|
|
|
*/ |
107
|
|
|
public function update(Request $request, int $id): RedirectResponse |
108
|
|
|
{ |
109
|
|
|
$role = Role::findOrFail($id); |
110
|
|
|
|
111
|
|
|
RoleValidator::update($request->all(), $role->id)->validate(); |
112
|
|
|
RoleRepository::update($request->all(), $role); |
113
|
|
|
|
114
|
|
|
$role->syncPermissions($request->get('permissions')); |
115
|
|
|
|
116
|
|
|
return redirect() |
117
|
|
|
->back() |
118
|
|
|
->with('success', 'This role has been updated successfully !'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.