RefreshPermissions.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Commands;
  3. use App\Models\Permission;
  4. use App\Models\UserTypePermission;
  5. use Database\Seeders\PermissionSeeder;
  6. use Database\Seeders\UserTypePermissionSeeder;
  7. use Illuminate\Console\Command;
  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. }