1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaPermission\Models; |
4
|
|
|
|
5
|
|
|
use BBSLab\NovaPermission\Contracts\Permission as PermissionContract; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Cache; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Spatie\Permission\Models\Permission as Model; |
11
|
|
|
use Spatie\Permission\PermissionRegistrar; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Permission. |
15
|
|
|
* |
16
|
|
|
* @property int $id |
17
|
|
|
* @property string $name |
18
|
|
|
* @property string $guard_name |
19
|
|
|
* @property string $group |
20
|
|
|
* @property int $authorizable_id |
21
|
|
|
* @property string $authorizable_type |
22
|
|
|
* @property \Illuminate\Database\Eloquent\Model $authorizable |
23
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $roles |
24
|
|
|
* @property \Carbon\Carbon $created_at |
25
|
|
|
* @property \Carbon\Carbon $updated_at |
26
|
|
|
*/ |
27
|
|
|
class Permission extends Model implements PermissionContract |
28
|
|
|
{ |
29
|
|
|
protected static function boot() |
30
|
|
|
{ |
31
|
|
|
parent::boot(); |
32
|
|
|
|
33
|
|
|
static::saving(function (Permission $permission) { |
34
|
|
|
$permission->forgetPermissionFromCache(); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
static::deleting(function (Permission $permission) { |
38
|
|
|
$permission->forgetPermissionFromCache(); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the authorizable instance. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
46
|
|
|
*/ |
47
|
|
|
public function authorizable(): MorphTo |
48
|
|
|
{ |
49
|
|
|
return $this->morphTo(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get permission builder representation. |
54
|
|
|
* |
55
|
|
|
* @param \Illuminate\Support\Collection $roles |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function serializeForPermissionBuilder(Collection $roles = null): array |
59
|
|
|
{ |
60
|
|
|
if (empty($roles)) { |
61
|
|
|
$roles = app(PermissionRegistrar::class)->getRoleClass() |
62
|
|
|
->newQuery() |
63
|
|
|
->orderBy('name') |
64
|
|
|
->get(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$roles = array_fill_keys($roles->pluck('id')->toArray(), false); |
68
|
|
|
|
69
|
|
|
return [ |
70
|
|
|
'id' => $this->id, |
71
|
|
|
'name' => $this->name, |
72
|
|
|
'guard_name' => $this->guard_name, |
73
|
|
|
'roles' => array_replace( |
74
|
|
|
$roles, |
75
|
|
|
$this->roles->mapWithKeys(function ($role) { |
76
|
|
|
return [$role->id => true]; |
77
|
|
|
})->toArray() |
78
|
|
|
), |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function forgetPermissionFromCache() |
83
|
|
|
{ |
84
|
|
|
if ($this->authorizable) { |
85
|
|
|
$key = implode(':', [ |
86
|
|
|
'nova-permission', |
87
|
|
|
'authorization', |
88
|
|
|
$this->getOriginal('authorizable_type', $this->authorizable_type), |
89
|
|
|
$this->getOriginal('authorizable_id', $this->authorizable_id), |
90
|
|
|
Str::snake($this->getOriginal('name', $this->name)) |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
Cache::forget($key); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|