1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaPermission\Resources; |
4
|
|
|
|
5
|
|
|
use BBSLab\NovaPermission\Contracts\HasAbilities; |
6
|
|
|
use BBSLab\NovaPermission\Traits\Authorizable; |
7
|
|
|
use BBSLab\NovaPermission\Traits\HasFieldName; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Validation\Rule; |
10
|
|
|
use Laravel\Nova\Fields\BelongsToMany; |
11
|
|
|
use Laravel\Nova\Fields\Boolean; |
12
|
|
|
use Laravel\Nova\Fields\DateTime; |
13
|
|
|
use Laravel\Nova\Fields\ID; |
14
|
|
|
use Laravel\Nova\Fields\Select; |
15
|
|
|
use Laravel\Nova\Fields\Text; |
16
|
|
|
use Laravel\Nova\Resource; |
17
|
|
|
|
18
|
|
|
class Role extends Resource implements HasAbilities |
19
|
|
|
{ |
20
|
|
|
use Authorizable, |
21
|
|
|
HasFieldName; |
22
|
|
|
|
23
|
|
|
public static $permissionsForAbilities = [ |
24
|
|
|
'viewAny' => 'viewAny role', |
25
|
|
|
'view' => 'view role', |
26
|
|
|
'create' => 'create role', |
27
|
|
|
'update' => 'update role', |
28
|
|
|
'delete' => 'delete role', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
public static $canSeeOverridePermissionCallback = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The model the resource corresponds to. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public static $model; |
39
|
|
|
|
40
|
|
|
public static function canSeeOverridePermmission($callback) |
41
|
|
|
{ |
42
|
|
|
static::$canSeeOverridePermissionCallback = $callback; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The single value that should be used to represent the resource when being displayed. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public static $title = 'name'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the search result subtitle for the resource. |
54
|
|
|
* |
55
|
|
|
* @return string|null |
56
|
|
|
*/ |
57
|
|
|
public function subtitle() |
58
|
|
|
{ |
59
|
|
|
return "Guard: {$this->guard_name}"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The columns that should be searched. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
public static $search = [ |
68
|
|
|
'name', 'guard_name', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the displayable label of the resource. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public static function label() |
77
|
|
|
{ |
78
|
|
|
return trans('nova-permission::resources.role.label'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the displayable singular label of the resource. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function singularLabel() |
87
|
|
|
{ |
88
|
|
|
return trans('nova-permission::resources.role.singular_label'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the fields displayed by the resource. |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Http\Request $request |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function fields(Request $request) |
98
|
|
|
{ |
99
|
|
|
$guardOptions = collect(config('auth.guards'))->mapWithKeys(function ($value, $key) { |
100
|
|
|
return [$key => $key]; |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
$fields = [ |
104
|
|
|
ID::make()->sortable(), |
105
|
|
|
Text::make($this->getTranslatedFieldName('Name'), 'name') |
106
|
|
|
->rules('required', 'string', 'max:255') |
107
|
|
|
->creationRules('unique:'.config('permission.table_names.roles')) |
108
|
|
|
->updateRules('unique:'.config('permission.table_names.roles').',name,{{resourceId}}'), |
109
|
|
|
|
110
|
|
|
Select::make($this->getTranslatedFieldName('Guard name'), 'guard_name') |
111
|
|
|
->options($guardOptions->toArray()) |
112
|
|
|
->rules(['required', Rule::in($guardOptions)]), |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
$overrideField = Boolean::make($this->getTranslatedFieldName('Override permission'), 'override_permission'); |
116
|
|
|
|
117
|
|
|
if (is_callable(static::$canSeeOverridePermissionCallback)) { |
118
|
|
|
$overrideField->canSee(static::$canSeeOverridePermissionCallback); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$fields[] = $overrideField; |
122
|
|
|
|
123
|
|
|
return array_merge($fields, [ |
124
|
|
|
DateTime::make($this->getTranslatedFieldName('Created at'), 'created_at') |
125
|
|
|
->onlyOnDetail(), |
126
|
|
|
DateTime::make($this->getTranslatedFieldName('Updated at'), 'updated_at') |
127
|
|
|
->onlyOnDetail(), |
128
|
|
|
BelongsToMany::make(Permission::label(), 'permissions', Permission::class) |
129
|
|
|
->searchable() |
130
|
|
|
->singularLabel(Permission::singularLabel()), |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|