PermissionSeeder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Permission;
  4. use Illuminate\Database\Seeder;
  5. use App\Services\PermissionService;
  6. class PermissionSeeder extends Seeder
  7. {
  8. public function __construct(
  9. protected PermissionService $permissionService,
  10. ) {}
  11. const VIEW = 1;
  12. const ADD = 2;
  13. const EDIT = 4;
  14. const DELETE = 8;
  15. const PRINT = 16;
  16. const EXPORT = 32;
  17. const IMPORT = 64;
  18. const LIMIT = 128;
  19. const MENU = 256;
  20. const ALL_PERMS = 511;
  21. public function run(): void
  22. {
  23. $permissions = [
  24. [
  25. "scope" => "dashboard",
  26. "description" => "Dashboard",
  27. "bits" => self::ALL_PERMS,
  28. "children" => [],
  29. ],
  30. [
  31. "scope" => "config",
  32. "description" => "Configurações",
  33. "bits" => self::MENU,
  34. "children" => [
  35. [
  36. "scope" => "config.user",
  37. "description" => "Configurações de Usuários",
  38. "bits" => self::MENU | self::VIEW | self::ADD | self::EDIT | self::DELETE,
  39. "children" => [],
  40. ],
  41. [
  42. "scope" => "config.permission",
  43. "description" => "Configurações de Permissões",
  44. "bits" => self::MENU | self::VIEW | self::ADD | self::EDIT | self::DELETE,
  45. "children" => [],
  46. ],
  47. [
  48. "scope" => "config.city",
  49. "description" => "Configurações de Cidades",
  50. "bits" => self::MENU | self::VIEW | self::ADD | self::EDIT | self::DELETE,
  51. "children" => [],
  52. ],
  53. [
  54. "scope" => "config.country",
  55. "description" => "Configurações de Países",
  56. "bits" => self::MENU | self::VIEW | self::ADD | self::EDIT | self::DELETE,
  57. "children" => [],
  58. ],
  59. [
  60. "scope" => "config.state",
  61. "description" => "Configurações de Estados",
  62. "bits" => self::MENU | self::VIEW | self::ADD | self::EDIT | self::DELETE,
  63. "children" => [],
  64. ],
  65. ],
  66. ],
  67. ];
  68. $this->createPermissionsAndChildren(permissions: $permissions);
  69. }
  70. /**
  71. * Recursively creates or updates permissions and handles nesting.
  72. *
  73. * @param array $permissions The array of permission data.
  74. * @param Permission|null $parent The parent Permission object (for nested sets).
  75. */
  76. private function createPermissionsAndChildren(
  77. array $permissions,
  78. ?Permission $parent = null,
  79. ): void {
  80. foreach ($permissions as $permissionData) {
  81. $children = $permissionData["children"];
  82. unset($permissionData["children"]);
  83. $permissionNode = Permission::updateOrCreate(
  84. ["scope" => $permissionData["scope"]],
  85. $permissionData,
  86. );
  87. if ($parent) {
  88. $parent->appendNode($permissionNode);
  89. }
  90. if (!empty($children)) {
  91. $this->createPermissionsAndChildren(
  92. permissions: $children,
  93. parent: $permissionNode,
  94. );
  95. }
  96. }
  97. }
  98. }