83 lines
1.9 KiB
PHP
83 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Admin;
|
|
|
|
use App\Models\OrderReduceRange;
|
|
use Dcat\Admin\Models\Administrator as DcatAdministrator;
|
|
|
|
class Administrator extends DcatAdministrator
|
|
{
|
|
public const SESSION_KEY = 'user_permissions';
|
|
|
|
public function hasPermission(string $slug): bool
|
|
{
|
|
//判断是否是超级管理员
|
|
if ($this->isAdministrator()) {
|
|
return true;
|
|
}
|
|
//判断是否有权限
|
|
return in_array($slug, $this->getCachePermissions());
|
|
}
|
|
|
|
/**
|
|
* 获取缓存权限
|
|
*
|
|
* @return array|null
|
|
*/
|
|
public function getCachePermissions(): ?array
|
|
{
|
|
$key = self::SESSION_KEY;
|
|
if (session()->has($key)) {
|
|
return session($key);
|
|
}
|
|
//获取角色权限
|
|
$permissions = [];
|
|
foreach ($this->roles()->with('permissions')->get() as $role) {
|
|
foreach ($role->permissions as $permission) {
|
|
$permissions[] = $permission->slug;
|
|
}
|
|
}
|
|
|
|
array_unique($permissions);
|
|
|
|
//缓存权限
|
|
session([$key => $permissions]);
|
|
|
|
return $permissions;
|
|
}
|
|
|
|
public function clearCachePermission()
|
|
{
|
|
session()->forget(self::SESSION_KEY);
|
|
}
|
|
|
|
/**
|
|
* 管理员订单调价范围
|
|
*
|
|
* @return void
|
|
*/
|
|
public function orderReduceRange()
|
|
{
|
|
return $this->hasOne(OrderReduceRange::class, 'administrator_id');
|
|
}
|
|
|
|
/**
|
|
* 在调价权限范围内
|
|
*
|
|
* @param integer $reduced
|
|
* @return void
|
|
*/
|
|
public function inReduceRange(int $reduced)
|
|
{
|
|
if (is_null($this->orderReduceRange)) {
|
|
return false;
|
|
}
|
|
return $reduced <= ($this->orderReduceRange->max*100);
|
|
}
|
|
|
|
public function stores()
|
|
{
|
|
return $this->belongsToMany(\App\Models\Store\Store::class, 'store_administrators', 'administrator_id', 'store_id');
|
|
}
|
|
}
|