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 Ultraware\Roles\Models\Permission; |
8
|
|
|
use Xetaravel\Http\Controllers\Admin\Controller; |
9
|
|
|
use Xetaravel\Models\Repositories\RoleRepository; |
10
|
|
|
use Xetaravel\Models\Role; |
11
|
|
|
use Xetaravel\Models\Validators\RoleValidator; |
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
|
|
View Code Duplication |
public function showCreateForm(): View |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$permissions = Permission::pluck('name', 'id'); |
37
|
|
|
|
38
|
|
|
$optionsAttributes = $this->getOptionAttributes(); |
39
|
|
|
|
40
|
|
|
$breadcrumbs = $this->breadcrumbs |
41
|
|
|
->addCrumb('Manage Roles', route('admin.role.role.index')) |
42
|
|
|
->addCrumb("Create", route('admin.role.role.create')); |
43
|
|
|
|
44
|
|
|
return view('Admin::Role.role.create', compact('permissions', 'breadcrumbs', 'optionsAttributes')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Handle a role create request for the application. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Http\Request $request |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Http\RedirectResponse |
53
|
|
|
*/ |
54
|
|
|
public function create(Request $request): RedirectResponse |
55
|
|
|
{ |
56
|
|
|
RoleValidator::create($request->all())->validate(); |
57
|
|
|
|
58
|
|
|
$role = RoleRepository::create($request->all()); |
59
|
|
|
$role->syncPermissions($request->get('permissions')); |
60
|
|
|
|
61
|
|
|
return redirect() |
62
|
|
|
->route('admin.role.role.index') |
63
|
|
|
->with('success', 'This role has been created successfully !'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Show the update form. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Http\Request $request |
70
|
|
|
* @param string $slug The slug of the role. |
71
|
|
|
* @param int $id The id of the role. |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
public function showUpdateForm(Request $request, string $slug, int $id) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$role = Role::findOrFail($id); |
78
|
|
|
|
79
|
|
|
$permissions = Permission::pluck('name', 'id'); |
80
|
|
|
$permission = Permission::where('slug', 'access.administration')->first(); |
81
|
|
|
|
82
|
|
|
$optionsAttributes = $this->getOptionAttributes(); |
83
|
|
|
|
84
|
|
|
$breadcrumbs = $this->breadcrumbs |
85
|
|
|
->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0') |
86
|
|
|
->addCrumb('Manage Roles', route('admin.role.role.index')) |
87
|
|
|
->addCrumb( |
88
|
|
|
'Update ' . e($role->name), |
89
|
|
|
route('admin.role.role.update', $role->slug, $role->id) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return view( |
93
|
|
|
'Admin::Role.role.update', |
94
|
|
|
compact('role', 'permissions', 'breadcrumbs', 'optionsAttributes', 'permission') |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Handle an user update request for the application. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Http\Request $request |
102
|
|
|
* @param int $id The id of the role to update. |
103
|
|
|
* |
104
|
|
|
* @return \Illuminate\Http\RedirectResponse |
105
|
|
|
*/ |
106
|
|
|
public function update(Request $request, int $id): RedirectResponse |
107
|
|
|
{ |
108
|
|
|
$role = Role::findOrFail($id); |
109
|
|
|
|
110
|
|
|
RoleValidator::update($request->all(), $role->id)->validate(); |
111
|
|
|
RoleRepository::update($request->all(), $role); |
112
|
|
|
|
113
|
|
|
$role->syncPermissions($request->get('permissions')); |
114
|
|
|
|
115
|
|
|
return redirect() |
116
|
|
|
->back() |
117
|
|
|
->with('success', 'This role has been updated successfully !'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Handle the delete request for the role. |
122
|
|
|
* |
123
|
|
|
* @param int $id The id of the role to delete. |
124
|
|
|
* |
125
|
|
|
* @return \Illuminate\Http\RedirectResponse |
126
|
|
|
*/ |
127
|
|
|
public function delete(int $id): RedirectResponse |
128
|
|
|
{ |
129
|
|
|
$role = Role::findOrFail($id); |
130
|
|
|
|
131
|
|
|
if (!$role->is_deletable) { |
132
|
|
|
return redirect() |
133
|
|
|
->route('admin.role.role.index') |
134
|
|
|
->with('danger', 'You can not delete this role !'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Sync the `user` role on all users for this group. |
138
|
|
|
foreach ($role->users as $user) { |
139
|
|
|
// Only do that if the user does not have another role. |
140
|
|
|
if ($user->roles->count() == 1) { |
141
|
|
|
$user->roles()->sync(Role::find(3), false); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($role->delete()) { |
146
|
|
|
return redirect() |
147
|
|
|
->route('admin.role.role.index') |
148
|
|
|
->with('success', 'This role has been deleted successfully !'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return redirect() |
152
|
|
|
->route('admin.role.role.index') |
153
|
|
|
->with('danger', 'An error occurred while deleting this role !'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return a list of attributes for the permissions option field. |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
protected function getOptionAttributes(): array |
162
|
|
|
{ |
163
|
|
|
$attributes = Permission::pluck('id')->toArray(); |
164
|
|
|
$optionsAttributes = []; |
165
|
|
|
|
166
|
|
|
foreach ($attributes as $attribute) { |
167
|
|
|
$optionsAttributes[$attribute] = [ |
168
|
|
|
'title' => 'Role Information', |
169
|
|
|
'data-content' => Permission::where('id', $attribute)->select('description')->first()->description, |
170
|
|
|
'data-toggle' => 'popover', |
171
|
|
|
'data-trigger' => 'hover', |
172
|
|
|
'data-placement' => 'top' |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $optionsAttributes; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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.