RefreshPermissions.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Commands;
  3. use App\Models\Permission;
  4. use App\Models\UserTypePermission;
  5. use Illuminate\Console\Command;
  6. use Database\Seeders\PermissionSeeder;
  7. use Database\Seeders\UserTypePermissionSeeder;
  8. class RefreshPermissions extends Command
  9. {
  10. public function __construct(
  11. protected PermissionSeeder $permissionSeeder,
  12. protected UserTypePermissionSeeder $userTypePermissionSeeder,
  13. ) {
  14. parent::__construct();
  15. }
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'permissions:refresh';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Refresh all permissions and user type permissions';
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return int
  32. */
  33. public function handle()
  34. {
  35. $this->info('Starting permissions refresh...');
  36. try {
  37. // Run the Permission seeder
  38. $this->info('Running Permission seeder...');
  39. Permission::truncate();
  40. $this->permissionSeeder->run();
  41. $this->info('Permission seeder completed successfully.');
  42. // Run the UserTypePermission seeder
  43. $this->info('Running UserTypePermission seeder...');
  44. UserTypePermission::truncate();
  45. $this->userTypePermissionSeeder->run();
  46. $this->info('UserTypePermission seeder completed successfully.');
  47. $this->info('Permissions refresh completed successfully!');
  48. return Command::SUCCESS;
  49. } catch (\Exception $e) {
  50. $this->error('An error occurred while refreshing permissions:');
  51. $this->error($e);
  52. return Command::FAILURE;
  53. }
  54. }
  55. }