76 lines
2.8 KiB
PHP
76 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Dcat\Admin\Models\Permission;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AdminPermissionSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run()
|
|
{
|
|
$permissions = [
|
|
'base' => ['name' => '系统管理', 'curd' => false, 'children' => [
|
|
'administrators' => ['name' => '用户管理', 'curd' => ['index', 'create', 'edit', 'destroy']],
|
|
'roles' => ['name' => '角色管理', 'curd' => ['index', 'create', 'edit', 'destroy']],
|
|
'keywords' => ['name' => '字典管理', 'curd' => ['index', 'create', 'edit', 'destroy']],
|
|
'settings' => ['name' => '配置管理', 'curd' => ['index', 'create', 'edit', 'destroy']],
|
|
]],
|
|
];
|
|
DB::table('admin_permissions')->truncate();
|
|
$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 = 'dcat.admin.'.($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 ? ($key ? $key.'.'.$slug : $slug) : $key;
|
|
$this->createPermissionData($permission['children'], $_key ?? $slug, $pper->id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|