forked from Advisr/nova-permission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permission.php
166 lines (143 loc) · 4.42 KB
/
Permission.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Vyuldashev\NovaPermission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Nova;
use Laravel\Nova\Resource;
use Spatie\Permission\PermissionRegistrar;
class Permission extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = \Spatie\Permission\Models\Permission::class;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'name',
];
public static function getModel()
{
return app(PermissionRegistrar::class)->getPermissionClass();
}
/**
* Get the logical group associated with the resource.
*
* @return string
*/
public static function group(): string
{
return __('nova-permission-tool::navigation.sidebar-label');
}
/**
* Determine if this resource is available for navigation.
*
* @param Request $request
* @return bool
*/
public static function availableForNavigation(Request $request): bool
{
return Gate::allows('viewAny', app(PermissionRegistrar::class)->getPermissionClass());
}
public static function label()
{
return __('nova-permission-tool::resources.Permissions');
}
public static function singularLabel()
{
return __('nova-permission-tool::resources.Permission');
}
/**
* Get the fields displayed by the resource.
*
* @param Request $request
* @return array
*/
public function fields(Request $request): array
{
$guardOptions = collect(config('auth.guards'))->mapWithKeys(function ($value, $key) {
return [$key => $key];
});
$userResource = Nova::resourceForModel(getModelForGuard($this->guard_name));
return [
ID::make()->sortable(),
Text::make(__('nova-permission-tool::permissions.name'), 'name')
->rules(['required', 'string', 'max:255'])
->creationRules('unique:'.config('permission.table_names.permissions'))
->updateRules('unique:'.config('permission.table_names.permissions').',name,{{resourceId}}'),
Text::make(__('nova-permission-tool::permissions.display_name'), function () {
return __('nova-permission-tool::permissions.display_names.'.$this->name);
})->canSee(function () {
return is_array(__('nova-permission-tool::permissions.display_names'));
}),
Select::make(__('nova-permission-tool::permissions.guard_name'), 'guard_name')
->options($guardOptions->toArray())
->rules(['required', Rule::in($guardOptions)]),
DateTime::make(__('nova-permission-tool::permissions.created_at'), 'created_at')->exceptOnForms(),
DateTime::make(__('nova-permission-tool::permissions.updated_at'), 'updated_at')->exceptOnForms(),
RoleBooleanGroup::make(__('nova-permission-tool::permissions.roles'), 'roles'),
MorphToMany::make($userResource::label(), 'users', $userResource)
->searchable()
->singularLabel($userResource::singularLabel()),
];
}
/**
* Get the cards available for the request.
*
* @param Request $request
* @return array
*/
public function cards(Request $request): array
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param Request $request
* @return array
*/
public function filters(Request $request): array
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param Request $request
* @return array
*/
public function lenses(Request $request): array
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param Request $request
* @return array
*/
public function actions(Request $request): array
{
return [
new AttachToRole,
];
}
}