94 lines
4.2 KiB
PHP
94 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Dcat\Admin\Models\Permission;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class EndpointPermissionSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run()
|
|
{
|
|
$permissions = [
|
|
'endpoint' => ['name' => '系统权限', 'curd' => false, 'in_path' => true, 'children' => [
|
|
'monitor_data' => ['name' => '监测数据管理', 'curd' => false, 'children' => [
|
|
'weather' => ['name' => '气象管理', 'curd' => ['index']],
|
|
'camera' => ['name' => '智能监控', 'curd' => ['index']],
|
|
'soil' => ['name' => '土壤监控', 'curd' => ['index'], 'children' => ['setting' => '设置']],
|
|
'water' => ['name' => '水质监控', 'curd' => ['index'], 'children' => ['setting' => '设置']],
|
|
]],
|
|
'base_data' => ['name' => '基础数据管理', 'curd' => false, 'children' => [
|
|
'citydata_statistics' => ['name' => '全市基础数据', 'curd' => ['index', 'edit']],
|
|
'agricultural_basic' => ['name' => '基地数据', 'curd' => true],
|
|
'crops_build' => ['name' => '农业产业结构', 'curd' => true],
|
|
'crops_output' => ['name' => '产量排行榜', 'curd' => true],
|
|
'crops_flow' => ['name' => '农产品流向', 'curd' => true],
|
|
'crops_price' => ['name' => '农产品价格走势', 'curd' => ['index'], 'children' => ['is_enable' => '自动监测开关']],
|
|
]],
|
|
'device_data' => ['name' => '设备管理', 'curd' => false, 'children' => [
|
|
'device' => ['name' => '设备管理', 'curd' => true],
|
|
]],
|
|
'manage' => ['name' => '系统管理', 'curd' => false, 'children' => [
|
|
'admin_users' => ['name' => '管理员管理', 'curd' => true, 'children' => [
|
|
'edit_password' => '修改密码', 'enable' => '启用/禁用',
|
|
]],
|
|
'admin_roles' => ['name' => '角色管理', 'curd' => true],
|
|
'operation_log' => ['name' => '操作日志', 'curd' => ['index']],
|
|
]],
|
|
]],
|
|
];
|
|
$this->createPermissionData($permissions);
|
|
}
|
|
|
|
/**
|
|
* 插入权限
|
|
*
|
|
* @param array $permissions
|
|
* @param string $key
|
|
* @param int $pId
|
|
*/
|
|
public function createPermissionData(array $permissions, string $key = '', int $pId = 0)
|
|
{
|
|
$curdArr = [
|
|
'index' => '列表',
|
|
'create' => '新增',
|
|
'edit' => '修改',
|
|
'destroy' => '删除',
|
|
'show' => '详情',
|
|
];
|
|
foreach ($permissions as $slug => $permission) {
|
|
//是否已存在该权限
|
|
$slugKey = ($key ? $key.'.'.$slug : $slug);
|
|
|
|
$pper = Permission::updateOrCreate(['slug' => $slugKey], ['name' => is_string($permission) ? $permission : $permission['name'], 'parent_id' => $pId]);
|
|
|
|
if (! is_string($permission)) {
|
|
if (! isset($permission['children'])) {
|
|
$permission['children'] = [];
|
|
}
|
|
//判断是否默认插入curd权限
|
|
if (isset($permission['curd']) && $permission['curd']) {
|
|
if (is_array($permission['curd'])) {
|
|
$permission['curd'] = array_reverse($permission['curd']);
|
|
foreach ($permission['curd'] as $value) {
|
|
$permission['children'] = array_merge([$value => $curdArr[$value]], $permission['children']);
|
|
}
|
|
} else {
|
|
$permission['children'] = array_merge($curdArr, $permission['children']);
|
|
}
|
|
}
|
|
|
|
if (count($permission['children']) > 0) {
|
|
$_key = ($permission['curd'] !== false || ($permission['in_path'] ?? false)) ? ($key ? $key.'.'.$slug : $slug) : $key;
|
|
$this->createPermissionData($permission['children'], $_key ?? $slug, $pper->id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|