97 lines
4.4 KiB
PHP
97 lines
4.4 KiB
PHP
<?php
|
|
|
|
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,
|
|
];
|
|
|
|
const PERMISSIONS = [
|
|
[
|
|
'name' => 'system.ctrl.menu',
|
|
'title' => '系统设置',
|
|
'path' => '#',
|
|
'icon' => 'ios-settings',
|
|
'type' => 0,
|
|
'open' => 3,
|
|
'children' => [
|
|
[
|
|
'name' => 'api.permissions.menu', 'title' => '权限管理', 'path' => '/permissions', 'icon' => 'ios-cube', 'type' => 0, 'open' => 3,
|
|
'children' => [
|
|
['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' => '/roles', 'icon' => 'ios-appstore', 'type' => 0, 'open' => 3,
|
|
'children' => [
|
|
['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' => '/accounts', 'icon' => 'ios-browsers', 'type' => 0, 'open' => 3,
|
|
'children' => [
|
|
['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.logs.menu', 'title' => '日志管理', 'path' => '/logs', 'icon' => 'ios-cube', 'type' => 0, 'open' => 3,
|
|
'children' => [
|
|
['name' => 'api.logs.index','title' => '查看','description' => 'index','type' => 1],
|
|
['name' => 'api.logs.destroy','title' => '删除','description' => 'destroy','type' => 1],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
|
|
const ROOT_PERMISSIONS = [
|
|
'system.*', 'api.permissions.*', 'api.logs.*', 'api.roles.*', 'api.accounts.*'
|
|
];
|
|
|
|
/**
|
|
* 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();
|
|
|
|
$permissions = app(PermissionService::class)->getPermissions();
|
|
|
|
$rootRole->syncPermissions($permissions->filter(function ($value, $key) {
|
|
return Str::is(self::ROOT_PERMISSIONS, $value['name']);
|
|
}));
|
|
|
|
Account::where('username', 'root')->first()->assignRole($rootRole);
|
|
}
|
|
}
|