1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaPermission\Traits; |
4
|
|
|
|
5
|
|
|
use BBSLab\NovaPermission\Contracts\HasAuthorizations; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Spatie\Permission\PermissionRegistrar; |
8
|
|
|
use Spatie\Permission\Traits\HasRoles as BaseTrait; |
9
|
|
|
|
10
|
|
|
trait HasRoles |
11
|
|
|
{ |
12
|
|
|
use BaseTrait; |
13
|
|
|
|
14
|
|
|
public function getOverridePermissionCacheTags(): array |
15
|
|
|
{ |
16
|
|
|
return ['nova-permission', 'can-override']; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getOverridePermissionCacheKey(): string |
20
|
|
|
{ |
21
|
|
|
return implode(':', [ |
22
|
|
|
'nova-permission', |
23
|
|
|
'can-override', |
24
|
|
|
'user:'.$this->getKey(), |
|
|
|
|
25
|
|
|
]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function forgetOverridePermission(): void |
29
|
|
|
{ |
30
|
|
|
/** @var \Illuminate\Contracts\Cache\Factory $cacheManager */ |
31
|
|
|
$cacheManager = app('cache'); |
32
|
|
|
|
33
|
|
|
$cacheManager->flush($this->getOverridePermissionCacheKey()); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function canOverridePermission(): bool |
37
|
|
|
{ |
38
|
|
|
/** @var \Illuminate\Contracts\Cache\Factory $cacheManager */ |
39
|
|
|
$cacheManager = app('cache'); |
40
|
|
|
|
41
|
|
|
$cache = method_exists($cacheManager->store(), 'tags') |
42
|
|
|
? $cacheManager->store()->tags($this->getOverridePermissionCacheTags()) |
|
|
|
|
43
|
|
|
: $cacheManager->store(); |
44
|
|
|
|
45
|
|
|
$key = $this->getOverridePermissionCacheKey(); |
46
|
|
|
|
47
|
|
|
return $cache->remember($key, PermissionRegistrar::$cacheExpirationTime, function () { |
48
|
|
|
$guard = config('nova.guard') ?? config('auth.defaults.guard'); |
49
|
|
|
|
50
|
|
|
return $this->roles() |
51
|
|
|
->where('guard_name', '=', $guard) |
52
|
|
|
->where('override_permission', '=', true) |
53
|
|
|
->exists(); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function hasPermissionToOnModel($permission, $model = null, $guardName = null): bool |
58
|
|
|
{ |
59
|
|
|
if (empty($model) || ! $model instanceof HasAuthorizations) { |
60
|
|
|
return $this->can($permission); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$key = implode(':', [ |
64
|
|
|
'nova-permission', |
65
|
|
|
'authorization', |
66
|
|
|
class_basename($model), |
67
|
|
|
$model->getKey(), |
|
|
|
|
68
|
|
|
Str::snake($permission), |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
/** @var \Illuminate\Contracts\Cache\Factory $cacheManager */ |
72
|
|
|
$cacheManager = app('cache'); |
73
|
|
|
|
74
|
|
|
$authorization = $cacheManager->store() |
75
|
|
|
->remember($key, PermissionRegistrar::$cacheExpirationTime, function () use ($permission, $model, $guardName) { |
76
|
|
|
return $model->authorizations() |
77
|
|
|
->where('name', '=', $permission) |
78
|
|
|
->where('guard_name', '=', $guardName ?? $this->getDefaultGuardName()) |
79
|
|
|
->first(); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
return ! empty($authorization) |
83
|
|
|
? $this->hasPermissionTo($authorization) |
84
|
|
|
: $this->can($permission); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.