1
|
|
|
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelApiHelper\Traits\JsonResponses; |
4
|
|
|
use Arcanesoft\Auth\Policies\PermissionsPolicy; |
5
|
|
|
use Arcanesoft\Contracts\Auth\Models\Permission; |
6
|
|
|
use Arcanesoft\Contracts\Auth\Models\PermissionsGroup; |
7
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role; |
8
|
|
|
use Log; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PermissionsController |
12
|
|
|
* |
13
|
|
|
* @package Arcanesoft\Auth\Http\Controllers\Admin |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class PermissionsController extends Controller |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Traits |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use JsonResponses; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Properties |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** @var \Arcanesoft\Contracts\Auth\Models\Permission|\Arcanesoft\Auth\Models\Permission */ |
31
|
|
|
protected $permission; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
protected $perPage = 30; |
35
|
|
|
|
36
|
|
|
/* ----------------------------------------------------------------- |
37
|
|
|
| Constructor |
38
|
|
|
| ----------------------------------------------------------------- |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Instantiate the controller. |
43
|
|
|
* |
44
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission $permission |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Permission $permission) |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
|
50
|
|
|
$this->permission = $permission; |
51
|
|
|
|
52
|
|
|
$this->setCurrentPage('auth-permissions'); |
53
|
|
|
$this->addBreadcrumbRoute(trans('auth::permissions.titles.permissions'), 'admin::auth.permissions.index'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* ----------------------------------------------------------------- |
57
|
|
|
| Main Methods |
58
|
|
|
| ----------------------------------------------------------------- |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
public function index() |
62
|
|
|
{ |
63
|
|
|
$this->authorize(PermissionsPolicy::PERMISSION_LIST); |
64
|
|
|
|
65
|
|
|
$permissions = $this->permission->with(['group']) |
|
|
|
|
66
|
|
|
->withCount(['roles']) |
67
|
|
|
->orderBy('group_id') |
68
|
|
|
->paginate($this->perPage); |
69
|
|
|
|
70
|
|
|
$this->setTitle($title = trans('auth::permissions.titles.permissions-list')); |
71
|
|
|
$this->addBreadcrumb($title); |
72
|
|
|
|
73
|
|
|
return $this->view('admin.permissions.index', compact('permissions')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function group(PermissionsGroup $group) |
77
|
|
|
{ |
78
|
|
|
$this->authorize(PermissionsPolicy::PERMISSION_LIST); |
79
|
|
|
|
80
|
|
|
$groupId = $group->id ? $group->id : 0; |
|
|
|
|
81
|
|
|
|
82
|
|
|
$permissions = $this->permission->where('group_id', $groupId) |
83
|
|
|
->with('group', 'roles') |
84
|
|
|
->paginate($this->perPage); |
85
|
|
|
|
86
|
|
|
$this->addBreadcrumbRoute(trans('auth::permissions.titles.permissions-list'), 'admin::auth.permissions.index'); |
87
|
|
|
|
88
|
|
|
$groupName = $groupId == 0 ? trans('auth::permission-groups.custom') : $group->name; |
|
|
|
|
89
|
|
|
$this->setTitle($title = trans('auth::permissions.titles.permissions-list')." - $groupName"); |
90
|
|
|
$this->addBreadcrumb($groupName); |
91
|
|
|
|
92
|
|
|
return $this->view('admin.permissions.index', compact('permissions')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function show(Permission $permission) |
96
|
|
|
{ |
97
|
|
|
$this->authorize(PermissionsPolicy::PERMISSION_SHOW); |
98
|
|
|
|
99
|
|
|
$permission->load(['roles', 'roles.users']); |
100
|
|
|
|
101
|
|
|
$this->setTitle($title = 'Permission details'); |
102
|
|
|
$this->addBreadcrumb($title); |
103
|
|
|
|
104
|
|
|
return $this->view('admin.permissions.show', compact('permission')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function detachRole(Permission $permission, Role $role) |
108
|
|
|
{ |
109
|
|
|
$this->authorize(PermissionsPolicy::PERMISSION_UPDATE); |
110
|
|
|
|
111
|
|
|
try { |
112
|
|
|
$permission->detachRole($role, false); |
113
|
|
|
|
114
|
|
|
$message = $this->transNotification( |
115
|
|
|
'detached', |
116
|
|
|
['role' => $role->name, 'permission' => $permission->name], |
|
|
|
|
117
|
|
|
['role' => $role->toArray(), 'permissions' => $permission->toArray()] |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return $this->jsonResponseSuccess(compact('message')); |
121
|
|
|
} |
122
|
|
|
catch(\Exception $e) { |
123
|
|
|
return $this->jsonResponseError(['message' => $e->getMessage()], 500); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/* ----------------------------------------------------------------- |
128
|
|
|
| Other Methods |
129
|
|
|
| ----------------------------------------------------------------- |
130
|
|
|
*/ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Notify with translation. |
134
|
|
|
* |
135
|
|
|
* @param string $action |
136
|
|
|
* @param array $replace |
137
|
|
|
* @param array $context |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function transNotification($action, array $replace = [], array $context = []) |
142
|
|
|
{ |
143
|
|
|
$title = trans("auth::permissions.messages.{$action}.title"); |
144
|
|
|
$message = trans("auth::permissions.messages.{$action}.message", $replace); |
145
|
|
|
|
146
|
|
|
Log::info($message, $context); |
147
|
|
|
$this->notifySuccess($message, $title); |
148
|
|
|
|
149
|
|
|
return $message; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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: