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 canOverridePermission(): bool |
29
|
|
|
{ |
30
|
|
|
/** @var \Illuminate\Contracts\Cache\Factory $cacheManager */ |
31
|
|
|
$cacheManager = app('cache'); |
32
|
|
|
|
33
|
|
|
$cache = method_exists($cacheManager->store(), 'tags') |
34
|
|
|
? $cacheManager->store()->tags($this->getOverridePermissionCacheTags()) |
|
|
|
|
35
|
|
|
: $cacheManager->store(); |
36
|
|
|
|
37
|
|
|
$key = $this->getOverridePermissionCacheKey(); |
38
|
|
|
|
39
|
|
|
return $cache->remember($key, PermissionRegistrar::$cacheExpirationTime, function () { |
40
|
|
|
$guard = config('nova.guard') ?? config('auth.defaults.guard'); |
41
|
|
|
|
42
|
|
|
return $this->roles() |
43
|
|
|
->where('guard_name', '=', $guard) |
44
|
|
|
->where('override_permission', '=', true) |
45
|
|
|
->exists(); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function hasPermissionToOnModel($permission, $model = null, $guardName = null): bool |
50
|
|
|
{ |
51
|
|
|
if (empty($model) || ! $model instanceof HasAuthorizations) { |
52
|
|
|
return $this->can($permission); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$key = implode(':', [ |
56
|
|
|
'nova-permission', |
57
|
|
|
'authorization', |
58
|
|
|
class_basename($model), |
59
|
|
|
$model->getKey(), |
|
|
|
|
60
|
|
|
Str::snake($permission) |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
/** @var \Illuminate\Contracts\Cache\Factory $cacheManager */ |
64
|
|
|
$cacheManager = app('cache'); |
65
|
|
|
|
66
|
|
|
$authorization = $cacheManager->store() |
67
|
|
|
->remember($key, PermissionRegistrar::$cacheExpirationTime, function () use ($permission, $model, $guardName) { |
68
|
|
|
return $model->authorizations() |
69
|
|
|
->where('name', '=', $permission) |
70
|
|
|
->where('guand_name', '=', $guardName ?? $this->getDefaultGuardName()) |
71
|
|
|
->first(); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
return ! empty($authorization) |
75
|
|
|
? $this->hasPermissionTo($authorization) |
76
|
|
|
: $this->can($permission); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.