generated from moonshine-software/moonshine-package-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
HasMoonShineRolePermissions.php
52 lines (40 loc) · 1.32 KB
/
HasMoonShineRolePermissions.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
<?php
namespace Sweet1s\MoonshineRBAC\Traits;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
trait HasMoonShineRolePermissions
{
public function getRolePriorityAttribute($value)
{
return $value !== null ? json_decode($value, true) : [];
}
/**
* @param string|null $resourceClass
* @param string|null $ability
* @param string|null $permission
*
* @return bool
*/
public function isHavePermission(string $resourceClass = null, string $ability = null, string $permission = null): bool
{
$currentPermission = $resourceClass . '.' . $ability;
if ($permission != null) {
$currentPermission = $permission;
}
if (config('moonshine.auth.providers.moonshine.model')::SUPER_ADMIN_ROLE_ID == $this->id) {
return true;
}
try {
return $this->hasPermissionTo($currentPermission);
} catch (PermissionDoesNotExist $e) {
$this->createPermissionIfNotExists($currentPermission);
return false;
}
}
private function createPermissionIfNotExists(string $permission): void
{
config('permission.models.permission')::updateOrCreate([
'name' => $permission,
'guard_name' => config('moonshine.auth.guard')
]);
}
}