|
1
|
|
|
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelApiHelper\Traits\JsonResponses; |
|
4
|
|
|
use Arcanesoft\Auth\Http\Requests\Admin\Roles\CreateRoleRequest; |
|
5
|
|
|
use Arcanesoft\Auth\Http\Requests\Admin\Roles\UpdateRoleRequest; |
|
6
|
|
|
use Arcanesoft\Auth\Policies\RolesPolicy; |
|
7
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role; |
|
8
|
|
|
use Log; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class RolesController |
|
12
|
|
|
* |
|
13
|
|
|
* @package Arcanesoft\Auth\Http\Controllers\Admin |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class RolesController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
/* ----------------------------------------------------------------- |
|
19
|
|
|
| Traits |
|
20
|
|
|
| ----------------------------------------------------------------- |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
use JsonResponses; |
|
24
|
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
|
26
|
|
|
| Properties |
|
27
|
|
|
| ----------------------------------------------------------------- |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The Role model. |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Arcanesoft\Contracts\Auth\Models\Role|\Arcanesoft\Auth\Models\Role |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $role; |
|
36
|
|
|
|
|
37
|
|
|
/* ----------------------------------------------------------------- |
|
38
|
|
|
| Constructor |
|
39
|
|
|
| ----------------------------------------------------------------- |
|
40
|
|
|
*/ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Instantiate the controller. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role $role |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Role $role) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct(); |
|
50
|
|
|
|
|
51
|
|
|
$this->role = $role; |
|
52
|
|
|
|
|
53
|
|
|
$this->setCurrentPage('auth-roles'); |
|
54
|
|
|
$this->addBreadcrumbRoute(trans('auth::roles.titles.roles'), 'admin::auth.roles.index'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/* ----------------------------------------------------------------- |
|
58
|
|
|
| Main Methods |
|
59
|
|
|
| ----------------------------------------------------------------- |
|
60
|
|
|
*/ |
|
61
|
|
|
|
|
62
|
|
|
public function index() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->authorize(RolesPolicy::PERMISSION_LIST); |
|
65
|
|
|
|
|
66
|
|
|
$roles = $this->role->withCount(['users', 'permissions'])->paginate(30); |
|
67
|
|
|
|
|
68
|
|
|
$this->setTitle($title = trans('auth::roles.titles.roles-list')); |
|
69
|
|
|
$this->addBreadcrumb($title); |
|
70
|
|
|
|
|
71
|
|
|
return $this->view('admin.roles.index', compact('roles')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function create() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->authorize(RolesPolicy::PERMISSION_CREATE); |
|
77
|
|
|
|
|
78
|
|
|
$this->setTitle($title = trans('auth::roles.titles.create-role')); |
|
79
|
|
|
$this->addBreadcrumb($title); |
|
80
|
|
|
|
|
81
|
|
|
return $this->view('admin.roles.create'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function store(CreateRoleRequest $request) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->authorize(RolesPolicy::PERMISSION_CREATE); |
|
87
|
|
|
|
|
88
|
|
|
$this->role->fill($request->getValidatedData()); |
|
|
|
|
|
|
89
|
|
|
$this->role->save(); |
|
|
|
|
|
|
90
|
|
|
$this->role->permissions()->attach($request->get('permissions')); |
|
91
|
|
|
|
|
92
|
|
|
$this->transNotification('created', ['name' => $this->role->name], $this->role->toArray()); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
return redirect()->route('admin::auth.roles.index'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function show(Role $role) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->authorize(RolesPolicy::PERMISSION_SHOW); |
|
100
|
|
|
|
|
101
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $role */ |
|
102
|
|
|
$role->load(['users', 'permissions', 'permissions.group']); |
|
103
|
|
|
|
|
104
|
|
|
$this->setTitle($title = trans('auth::roles.titles.role-details')); |
|
105
|
|
|
$this->addBreadcrumb($title); |
|
106
|
|
|
|
|
107
|
|
|
return $this->view('admin.roles.show', compact('role')); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function edit(Role $role) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->authorize(RolesPolicy::PERMISSION_UPDATE); |
|
113
|
|
|
|
|
114
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $role */ |
|
115
|
|
|
$role->load(['users', 'permissions']); |
|
116
|
|
|
|
|
117
|
|
|
$this->setTitle($title = trans('auth::roles.titles.edit-role')); |
|
118
|
|
|
$this->addBreadcrumb($title); |
|
119
|
|
|
|
|
120
|
|
|
return $this->view('admin.roles.edit', compact('role')); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function update(UpdateRoleRequest $request, Role $role) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->authorize(RolesPolicy::PERMISSION_UPDATE); |
|
126
|
|
|
|
|
127
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $role */ |
|
128
|
|
|
$role->fill($request->getValidatedData()); |
|
129
|
|
|
$role->save(); |
|
130
|
|
|
$role->permissions()->sync($request->get('permissions')); |
|
131
|
|
|
|
|
132
|
|
|
$this->transNotification('updated', ['name' => $role->name], $role->toArray()); |
|
133
|
|
|
|
|
134
|
|
|
return redirect() |
|
135
|
|
|
->route('admin::auth.roles.show', [$role->hashed_id]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function activate(Role $role) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->authorize(RolesPolicy::PERMISSION_UPDATE); |
|
141
|
|
|
|
|
142
|
|
|
try { |
|
143
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $role */ |
|
144
|
|
|
($active = $role->isActive()) ? $role->deactivate() : $role->activate(); |
|
145
|
|
|
|
|
146
|
|
|
$message = $this->transNotification( |
|
147
|
|
|
$active ? 'disabled' : 'enabled', |
|
148
|
|
|
['name' => $role->name], |
|
149
|
|
|
$role->toArray() |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
return $this->jsonResponseSuccess(compact('message')); |
|
153
|
|
|
} |
|
154
|
|
|
catch(\Exception $e) { |
|
155
|
|
|
return $this->jsonResponseError(['message' => $e->getMessage()], 500); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function delete(Role $role) |
|
160
|
|
|
{ |
|
161
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $role */ |
|
162
|
|
|
$this->authorize(RolesPolicy::PERMISSION_DELETE); |
|
163
|
|
|
|
|
164
|
|
|
try { |
|
165
|
|
|
$role->delete(); |
|
166
|
|
|
|
|
167
|
|
|
$message = $this->transNotification('deleted', ['name' => $role->name], $role->toArray()); |
|
168
|
|
|
|
|
169
|
|
|
return $this->jsonResponseSuccess(compact('message')); |
|
170
|
|
|
} |
|
171
|
|
|
catch(\Exception $e) { |
|
172
|
|
|
return $this->jsonResponseError(['message' => $e->getMessage()], 500); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/* ----------------------------------------------------------------- |
|
177
|
|
|
| Other Methods |
|
178
|
|
|
| ----------------------------------------------------------------- |
|
179
|
|
|
*/ |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Notify with translation. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $action |
|
185
|
|
|
* @param array $replace |
|
186
|
|
|
* @param array $context |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function transNotification($action, array $replace = [], array $context = []) |
|
191
|
|
|
{ |
|
192
|
|
|
$title = trans("auth::roles.messages.{$action}.title"); |
|
193
|
|
|
$message = trans("auth::roles.messages.{$action}.message", $replace); |
|
194
|
|
|
|
|
195
|
|
|
Log::info($message, $context); |
|
196
|
|
|
$this->notifySuccess($message, $title); |
|
197
|
|
|
|
|
198
|
|
|
return $message; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: