1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Models; |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
use Cache; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Sco\Admin\Exceptions\AdminHttpException; |
10
|
|
|
use Sco\Admin\Observers\PermissionObserver; |
11
|
|
|
use Sco\Admin\Traits\EntrustPermissionTrait; |
12
|
|
|
use Sco\Tree\Traits\TreeTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sco\Admin\Models\Permission |
16
|
|
|
* |
17
|
|
|
* @property int $id 主键 |
18
|
|
|
* @property int $pid 父ID |
19
|
|
|
* @property string $icon 图标class |
20
|
|
|
* @property string $display_name 显示名称 |
21
|
|
|
* @property string $name 名称 |
22
|
|
|
* @property bool $is_menu 是否作为菜单 |
23
|
|
|
* @property bool $sort 排序 |
24
|
|
|
* @property string $description 描述 |
25
|
|
|
* @property \Carbon\Carbon $created_at |
26
|
|
|
* @property \Carbon\Carbon $updated_at |
27
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Sco\Admin\Models\Role[] $roles |
28
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereCreatedAt($value) |
29
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereDescription($value) |
30
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereDisplayName($value) |
31
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereIcon($value) |
32
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereId($value) |
33
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereIsMenu($value) |
34
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereName($value) |
35
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission wherePid($value) |
36
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereSort($value) |
37
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Sco\Admin\Models\Permission whereUpdatedAt($value) |
38
|
|
|
* @mixin \Eloquent |
39
|
|
|
*/ |
40
|
|
|
class Permission extends Model |
41
|
|
|
{ |
42
|
|
|
use TreeTrait, EntrustPermissionTrait; |
43
|
|
|
|
44
|
|
|
protected $hidden = ['created_at', 'updated_at']; |
45
|
|
|
|
46
|
|
|
protected $guarded = ['created_at', 'updated_at']; |
47
|
|
|
|
48
|
|
|
protected $treeNodeParentIdName = 'pid'; |
49
|
|
|
|
50
|
|
|
private $allRoutes = null; |
51
|
|
|
|
52
|
|
|
private $permList = null; |
53
|
|
|
|
54
|
|
|
private $menuList = null; |
55
|
|
|
|
56
|
|
|
protected $fillable = ['pid', 'display_name', 'name', 'icon', 'is_menu', 'sort', 'description']; |
57
|
|
|
|
58
|
|
|
protected $events = [ |
59
|
|
|
'created' => \Sco\ActionLog\Events\ModelWasCreated::class, |
60
|
|
|
'updated' => \Sco\ActionLog\Events\ModelWasUpdated::class, |
61
|
|
|
'deleted' => \Sco\ActionLog\Events\ModelWasDeleted::class, |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
public function __construct(array $attributes = []) |
66
|
|
|
{ |
67
|
|
|
parent::__construct($attributes); |
68
|
|
|
$this->table = config('admin.permissions_table'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \Illuminate\Support\Collection |
74
|
|
|
*/ |
75
|
|
|
public function getMenuTreeList() |
76
|
|
|
{ |
77
|
|
|
$routes = $this->getDescendants(0); |
78
|
|
|
//dd($routes); |
79
|
|
|
return $routes; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getAll() |
83
|
|
|
{ |
84
|
|
|
if ($this->allRoutes) { |
85
|
|
|
return $this->allRoutes; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->allRoutes = Cache::rememberForever('permission_all', |
89
|
|
|
function () { |
90
|
|
|
return $this->orderBy('sort')->get(); |
|
|
|
|
91
|
|
|
}); |
92
|
|
|
return $this->allRoutes; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Tree Trait 获取所有节点 |
97
|
|
|
* |
98
|
|
|
* @return mixed|null |
99
|
|
|
*/ |
100
|
|
|
protected function getTreeAllNodes() |
101
|
|
|
{ |
102
|
|
|
return $this->getAll(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 获取权限列表 |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Support\Collection|null |
109
|
|
|
*/ |
110
|
|
|
public function getPermRouteList() |
111
|
|
|
{ |
112
|
|
|
if ($this->permList) { |
113
|
|
|
return $this->permList; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->permList = $this->getLayerOfDescendants(0); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getMenuList() |
120
|
|
|
{ |
121
|
|
|
if ($this->menuList) { |
122
|
|
|
return $this->menuList; |
123
|
|
|
} |
124
|
|
|
$all = $this->getAll(); |
125
|
|
|
|
126
|
|
|
$routes = collect([]); |
127
|
|
|
foreach ($all as $route) { |
128
|
|
|
if ($route->is_menu) { |
129
|
|
|
$routes->push($route); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->setAllNodes($routes); |
134
|
|
|
return $this->menuList = $this->getLayerOfDescendants(0); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* 删除菜单 |
139
|
|
|
* |
140
|
|
|
* @param int|array $ids 菜单ID |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function destroyMenu($ids) |
145
|
|
|
{ |
146
|
|
|
if (!is_array($ids)) { |
147
|
|
|
$ids = [intval($ids)]; |
148
|
|
|
} |
149
|
|
|
$items = collect(); |
150
|
|
|
foreach ($ids as $id) { |
151
|
|
|
$items->push($id); |
152
|
|
|
$items = $items->merge($this->getDescendants($id)->keys()); |
153
|
|
|
} |
154
|
|
|
$items = $items->unique(); |
155
|
|
|
if ($items->isEmpty()) { |
156
|
|
|
throw new AdminHttpException('菜单不存在'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
DB::transaction(function () use ($items) { |
160
|
|
|
$this->destroy($items->toArray()); |
161
|
|
|
}); |
162
|
|
|
|
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public static function boot() |
167
|
|
|
{ |
168
|
|
|
parent::boot(); |
169
|
|
|
|
170
|
|
|
static::observe(PermissionObserver::class); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: