InitializeRoutePermission.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Permission;
  4. use App\Models\PermissionGroup;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Route;
  7. class InitializeRoutePermission extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'lpc:initialize-route-permission';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Initialize routing permissions';
  21. /**
  22. * Execute the console command.
  23. */
  24. public function handle()
  25. {
  26. $routes = Route::getRoutes();
  27. $permissionGroup = PermissionGroup::query()->firstOrCreate([
  28. 'name' => 'default',
  29. ]);
  30. foreach ($routes as $route) {
  31. if (! $route->getName()) {
  32. continue;
  33. }
  34. Permission::query()->firstOrCreate([
  35. 'name' => $route->getName(),
  36. 'guard_name' => 'api',
  37. ], [
  38. 'description' => $route->getName(),
  39. 'permission_group_id' => $permissionGroup->id,
  40. ]);
  41. }
  42. }
  43. }