1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Permission;
- use App\Models\PermissionGroup;
- use App\Models\Role;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Route;
- class InitializeRoutePermission extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'lpc:initialize-route-permission';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Initialize routing permissions';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $routes = Route::getRoutes();
- $permissionGroup = PermissionGroup::query()->firstOrCreate([
- 'name' => 'default',
- ]);
- foreach ($routes as $route) {
- if (! $route->getName()) {
- continue;
- }
- if (!in_array('auth:sanctum', $route->middleware())) {
- continue;
- }
- Permission::query()->firstOrCreate([
- 'name' => $route->getName(),
- 'guard_name' => 'api',
- ], [
- 'description' => $route->getName(),
- 'permission_group_id' => $permissionGroup->id,
- ]);
- }
- $superAdminRoleId = config("auth.super_admin_role_id");
- $superAdminRole = [
- 'id' => $superAdminRoleId,
- 'name' => 'admin',
- 'guard_name' => 'api',
- 'description' => '管理员权限'
- ];
- $role = Role::query()->firstOrCreate(['id' => $superAdminRoleId], $superAdminRole);
- $role?->syncPermissions(Permission::query()->pluck("name")->toArray());
- }
- }
|