1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 2019/2/25 Time: 10:34 |
4
|
|
|
* |
5
|
|
|
* @author Eddy <[email protected]> |
6
|
|
|
* @version v1.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace App\Model\Admin; |
10
|
|
|
|
11
|
|
|
use Spatie\Permission\Contracts\Permission as PermissionContract; |
12
|
|
|
use Spatie\Permission\PermissionRegistrar; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
14
|
|
|
use Spatie\Permission\Guard; |
15
|
|
|
use Spatie\Permission\Exceptions\PermissionDoesNotExist; |
16
|
|
|
use Illuminate\Support\Collection; |
17
|
|
|
|
18
|
|
|
class Menu extends Model implements PermissionContract |
19
|
|
|
{ |
20
|
|
|
const STATUS_ENABLE = 1; |
21
|
|
|
const STATUS_DISABLE = 0; |
22
|
|
|
|
23
|
|
|
const LOCK_NAME = 1; |
24
|
|
|
const UNLOCK_NAME = 0; |
25
|
|
|
|
26
|
|
|
protected $guarded = []; |
27
|
|
|
|
28
|
|
|
public function parent() |
29
|
|
|
{ |
30
|
|
|
return $this->belongsTo('App\Model\Admin\Menu', 'pid'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function children() |
34
|
|
|
{ |
35
|
|
|
return $this->hasMany('App\Model\Admin\Menu', 'pid'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A permission can be applied to roles. |
40
|
|
|
*/ |
41
|
|
|
public function roles(): BelongsToMany |
42
|
|
|
{ |
43
|
|
|
return $this->belongsToMany( |
44
|
|
|
config('permission.models.role'), |
45
|
|
|
config('permission.table_names.role_has_permissions'), |
46
|
|
|
'permission_id', |
47
|
|
|
'role_id' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Find a permission by its name. |
53
|
|
|
*/ |
54
|
|
|
public static function findByName(string $name, $guardName): PermissionContract |
55
|
|
|
{ |
56
|
|
|
$guardName = $guardName ?? Guard::getDefaultName(static::class); |
57
|
|
|
$permission = static::getPermissions(['name' => $name, 'guard_name' => $guardName])->first(); |
58
|
|
|
if (! $permission) { |
59
|
|
|
throw PermissionDoesNotExist::create($name, $guardName); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $permission; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Find a permission by its id. |
67
|
|
|
*/ |
68
|
|
|
public static function findById(int $id, $guardName): PermissionContract |
69
|
|
|
{ |
70
|
|
|
$guardName = $guardName ?? Guard::getDefaultName(static::class); |
71
|
|
|
$permission = static::getPermissions(['id' => $id, 'guard_name' => $guardName])->first(); |
72
|
|
|
|
73
|
|
|
if (! $permission) { |
74
|
|
|
throw PermissionDoesNotExist::withId($id); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $permission; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Find or Create a permission by its name and guard name. |
82
|
|
|
*/ |
83
|
|
|
public static function findOrCreate(string $name, $guardName): PermissionContract |
84
|
|
|
{ |
85
|
|
|
$guardName = $guardName ?? Guard::getDefaultName(static::class); |
86
|
|
|
$permission = static::getPermissions(['name' => $name, 'guard_name' => $guardName])->first(); |
87
|
|
|
|
88
|
|
|
if (! $permission) { |
89
|
|
|
return static::query()->create(['name' => $name, 'guard_name' => $guardName]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $permission; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the current cached permissions. |
97
|
|
|
*/ |
98
|
|
|
protected static function getPermissions(array $params = []): Collection |
99
|
|
|
{ |
100
|
|
|
return app(PermissionRegistrar::class)->getPermissions($params); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|