144 lines
7.2 KiB
PHP
144 lines
7.2 KiB
PHP
<?php
|
|
|
|
use App\Models\User\User;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\Account\Account;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Domains\Permission\Services\PermissionService;
|
|
use App\Domains\Permission\Repositories\RoleRepository;
|
|
use App\Domains\Permission\Repositories\PermissionRepository;
|
|
|
|
class PermissionSeeder extends Seeder
|
|
{
|
|
const ROLES = [
|
|
'name' => '超级管理员', 'type' => 0, 'creator' => 0, 'children' => [
|
|
['name' => '管理员', 'type' => 0, 'creator' => 0],
|
|
['name' => '用户', 'type' => 0, 'creator' => 0],
|
|
],
|
|
];
|
|
|
|
const PERMISSIONS = [
|
|
[
|
|
'name' => 'system.ctrl.menu',
|
|
'title' => '系统设置',
|
|
'path' => '#',
|
|
'icon' => 'ios-settings',
|
|
'type' => 0,
|
|
'children' => [
|
|
[
|
|
'name' => 'api.apps.menu', 'title' => '应用管理', 'path' => '/system/application', 'icon' => '', 'type' => 0,
|
|
'children' => [
|
|
['name' => 'api.apps.root', 'title' => '超级管理员', 'description' => 'index', 'type' => 1],
|
|
['name' => 'api.apps.index', 'title' => '查看', 'description' => 'index', 'type' => 1],
|
|
['name' => 'api.apps.create', 'title' => '创建', 'description' => 'create', 'type' => 1],
|
|
['name' => 'api.apps.update', 'title' => '编辑', 'description' => 'update', 'type' => 1],
|
|
['name' => 'api.apps.destroy', 'title' => '删除', 'description' => 'destroy', 'type' => 1],
|
|
],
|
|
],
|
|
[
|
|
'name' => 'api.permissions.menu', 'title' => '权限管理', 'path' => '/system/permission', 'icon' => '', 'type' => 0,
|
|
'children' => [
|
|
['name' => 'api.permissions.index', 'title' => '查看', 'description' => 'index', 'type' => 1],
|
|
['name' => 'api.permissions.create', 'title' => '创建', 'description' => 'create', 'type' => 1],
|
|
['name' => 'api.permissions.update', 'title' => '编辑', 'description' => 'update', 'type' => 1],
|
|
['name' => 'api.permissions.destroy', 'title' => '删除', 'description' => 'destroy', 'type' => 1],
|
|
],
|
|
],
|
|
[
|
|
'name' => 'api.roles.menu', 'title' => '角色管理', 'path' => '/system/role', 'icon' => '', 'type' => 0,
|
|
'children' => [
|
|
['name' => 'api.roles.root', 'title' => '身份管理', 'description' => 'root', 'type' => 1],
|
|
['name' => 'api.roles.index', 'title' => '查看', 'description' => 'index', 'type' => 1],
|
|
['name' => 'api.roles.create', 'title' => '创建', 'description' => 'create', 'type' => 1],
|
|
['name' => 'api.roles.update', 'title' => '编辑', 'description' => 'update', 'type' => 1],
|
|
['name' => 'api.roles.destroy', 'title' => '删除', 'description' => 'destroy', 'type' => 1],
|
|
['name' => 'api.roles.sync_permissions', 'title' => '分配权限', 'description' => 'sync', 'type' => 1],
|
|
['name' => 'api.roles.sync_roles', 'title' => '分配角色', 'description' => 'sync', 'type' => 1],
|
|
],
|
|
],
|
|
[
|
|
'name' => 'api.accounts.menu', 'title' => '账号管理', 'path' => '/system/account', 'icon' => '', 'type' => 0,
|
|
'children' => [
|
|
['name' => 'api.accounts.root', 'title' => '超级用户管理', 'description' => 'root', 'type' => 1],
|
|
['name' => 'api.accounts.index', 'title' => '查看', 'description' => 'index', 'type' => 1],
|
|
['name' => 'api.accounts.create', 'title' => '创建', 'description' => 'create', 'type' => 1],
|
|
['name' => 'api.accounts.update', 'title' => '编辑', 'description' => 'update', 'type' => 1],
|
|
['name' => 'api.accounts.destroy', 'title' => '删除', 'description' => 'destroy', 'type' => 1],
|
|
],
|
|
],
|
|
[
|
|
'name' => 'api.users.menu', 'title' => '用户管理', 'path' => '/system/user', 'icon' => '', 'type' => 0,
|
|
'children' => [
|
|
['name' => 'api.users.index', 'title' => '查看', 'description' => 'index', 'type' => 1],
|
|
['name' => 'api.users.create', 'title' => '创建', 'description' => 'create', 'type' => 1],
|
|
['name' => 'api.users.update', 'title' => '编辑', 'description' => 'update', 'type' => 1],
|
|
['name' => 'api.users.destroy', 'title' => '删除', 'description' => 'destroy', 'type' => 1],
|
|
],
|
|
],
|
|
[
|
|
'name' => 'api.logs.menu', 'title' => '日志管理', 'path' => '/system/log', 'icon' => '', 'type' => 0,
|
|
'children' => [
|
|
['name' => 'api.logs.index','title' => '查看','description' => 'index','type' => 1],
|
|
['name' => 'api.logs.destroy','title' => '删除','description' => 'destroy','type' => 1],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
|
|
const ROOT_PERMISSIONS = [
|
|
'system.*', 'api.apps.*', 'api.permissions.*', 'api.logs.*', 'api.roles.*', 'api.accounts.*', 'api.user.*'
|
|
];
|
|
|
|
const ADMIN_PERMISSIONS_EXCEPT = [
|
|
'api.logs.*', 'api.roles.root', 'api.permissions.menu', 'api.permissions.create', 'api.permissions.update', 'api.permissions.destroy',
|
|
'api.apps.*', 'web.*'
|
|
];
|
|
|
|
const USER_PERMISSIONS = [
|
|
'web.*'
|
|
];
|
|
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run()
|
|
{
|
|
if (DB::table('roles')->count() || DB::table('permissions')->count()) {
|
|
return ;
|
|
}
|
|
|
|
app(RoleRepository::class)->create(self::ROLES);
|
|
|
|
foreach (self::PERMISSIONS as $permissions) {
|
|
app(PermissionRepository::class)->create($permissions);
|
|
}
|
|
|
|
$rootRole = app(RoleRepository::class)->where('name', '超级管理员')->first();
|
|
$adminRole = app(RoleRepository::class)->where('name', '管理员')->first();
|
|
$userRole = app(RoleRepository::class)->where('name', '用户')->first();
|
|
$permissions = app(PermissionService::class)->getPermissions();
|
|
|
|
$rootRole->syncPermissions($permissions->filter(function ($value, $key) {
|
|
return Str::is(self::ROOT_PERMISSIONS, $value['name']);
|
|
}));
|
|
|
|
$adminRole->syncPermissions($permissions->filter(function ($value, $key) {
|
|
return !Str::is(self::ADMIN_PERMISSIONS_EXCEPT, $value['name']);
|
|
}));
|
|
|
|
$userRole->syncPermissions($permissions->filter(function ($value, $key) {
|
|
return Str::is(self::USER_PERMISSIONS, $value['name']);
|
|
}));
|
|
|
|
Account::where('username', 'root')->first()->assignRole($rootRole);
|
|
|
|
Account::where('username', '<>', 'root')->get()->map(function ($node) use ($adminRole) {
|
|
$node->assignRole($adminRole);
|
|
});
|
|
}
|
|
}
|