PermissionSeeder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace Database\Seeders;
  3. use App\Models\Permission;
  4. use App\Services\PermissionService;
  5. use Illuminate\Database\Seeder;
  6. class PermissionSeeder extends Seeder
  7. {
  8. public function __construct(
  9. protected PermissionService $permissionService,
  10. ) {}
  11. public function run(): void
  12. {
  13. $permissions = [
  14. [
  15. "scope" => "dashboard",
  16. "description" => "Dashboard",
  17. "bits" => Permission::ALL_PERMS,
  18. "children" => [],
  19. ],
  20. [
  21. "scope" => "franchisor_franchisee",
  22. "description" => "Franqueados",
  23. "bits" => Permission::ALL_PERMS,
  24. "children" => [],
  25. ],
  26. // Pacotes / Aulas
  27. [
  28. "scope" => "franchisor_packages",
  29. "description" => "Pacotes (Franqueadora)",
  30. "bits" => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  31. "children" => [],
  32. ],
  33. [
  34. "scope" => "class-package-unit",
  35. "description" => "Pacotes (Unidade)",
  36. "bits" => Permission::ALL_PERMS,
  37. "children" => [],
  38. ],
  39. [
  40. "scope" => "class-package-franchisee",
  41. "description" => "Pacotes (Franqueado)",
  42. "bits" => Permission::ALL_PERMS,
  43. "children" => [],
  44. ],
  45. [
  46. "scope" => "class",
  47. "description" => "Aulas",
  48. "bits" => Permission::ALL_PERMS,
  49. "children" => [
  50. [
  51. "scope" => "class-attendance",
  52. "description" => "Frequência de Aulas",
  53. "bits" => Permission::ALL_PERMS,
  54. "children" => [],
  55. ],
  56. ],
  57. ],
  58. [
  59. "scope" => "modality",
  60. "description" => "Modalidades",
  61. "bits" => Permission::ALL_PERMS,
  62. "children" => [],
  63. ],
  64. // Alunos
  65. [
  66. "scope" => "student",
  67. "description" => "Alunos",
  68. "bits" => Permission::ALL_PERMS,
  69. "children" => [
  70. [
  71. "scope" => "student-history",
  72. "description" => "Histórico de Alunos",
  73. "bits" => Permission::ALL_PERMS,
  74. "children" => [],
  75. ],
  76. [
  77. "scope" => "student-media",
  78. "description" => "Mídias de Alunos",
  79. "bits" => Permission::ALL_PERMS,
  80. "children" => [],
  81. ],
  82. [
  83. "scope" => "student-responsible",
  84. "description" => "Responsáveis de Alunos",
  85. "bits" => Permission::ALL_PERMS,
  86. "children" => [],
  87. ],
  88. ],
  89. ],
  90. [
  91. "scope" => "student-contract",
  92. "description" => "Contratos de Alunos",
  93. "bits" => Permission::ALL_PERMS,
  94. "children" => [],
  95. ],
  96. [
  97. "scope" => "media",
  98. "description" => "Mídias",
  99. "bits" => Permission::ALL_PERMS,
  100. "children" => [],
  101. ],
  102. // Kanban
  103. [
  104. "scope" => "franchisor_activities",
  105. "description" => "Atividades",
  106. "bits" => Permission::ALL_PERMS,
  107. "children" => [],
  108. ],
  109. // Suporte
  110. [
  111. "scope" => "franchisor_support",
  112. "description" => "Chamados de Suporte",
  113. "bits" => Permission::ALL_PERMS,
  114. "children" => [],
  115. ],
  116. // Notificações
  117. [
  118. "scope" => "notification",
  119. "description" => "Notificações",
  120. "bits" => Permission::ALL_PERMS,
  121. "children" => [
  122. [
  123. "scope" => "notification-recipient",
  124. "description" => "Destinatários de Notificações",
  125. "bits" => Permission::ALL_PERMS,
  126. "children" => [],
  127. ],
  128. ],
  129. ],
  130. // Produtos / Inventário
  131. [
  132. "scope" => "franchisor_products",
  133. "description" => "Produtos",
  134. "bits" => Permission::ALL_PERMS,
  135. "children" => [
  136. [
  137. "scope" => "product-kit",
  138. "description" => "Kits de Produtos",
  139. "bits" => Permission::ALL_PERMS,
  140. "children" => [],
  141. ],
  142. [
  143. "scope" => "product-movement",
  144. "description" => "Movimentação de Produtos",
  145. "bits" => Permission::ALL_PERMS,
  146. "children" => [],
  147. ],
  148. [
  149. "scope" => "product-order",
  150. "description" => "Pedidos de Produtos",
  151. "bits" => Permission::ALL_PERMS,
  152. "children" => [
  153. [
  154. "scope" => "product-order-item",
  155. "description" => "Itens de Pedido de Produtos",
  156. "bits" => Permission::ALL_PERMS,
  157. "children" => [],
  158. ],
  159. ],
  160. ],
  161. ],
  162. ],
  163. [
  164. "scope" => "franchisor-inventory",
  165. "description" => "Estoque (Franqueadora)",
  166. "bits" => Permission::ALL_PERMS,
  167. "children" => [],
  168. ],
  169. [
  170. "scope" => "franchisee-inventory",
  171. "description" => "Estoque (Franqueado)",
  172. "bits" => Permission::ALL_PERMS,
  173. "children" => [],
  174. ],
  175. // Fornecedores / Pagamentos
  176. [
  177. "scope" => "supplier",
  178. "description" => "Fornecedores",
  179. "bits" => Permission::ALL_PERMS,
  180. "children" => [],
  181. ],
  182. [
  183. "scope" => "payment-method",
  184. "description" => "Formas de Pagamento",
  185. "bits" => Permission::ALL_PERMS,
  186. "children" => [],
  187. ],
  188. // Financeiro
  189. [
  190. "scope" => "franchisor_financial",
  191. "description" => "Financeiro",
  192. "bits" => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  193. "children" => [],
  194. ],
  195. // Feriados
  196. [
  197. "scope" => "holiday",
  198. "description" => "Feriados",
  199. "bits" => Permission::ALL_PERMS,
  200. "children" => [
  201. [
  202. "scope" => "base-holiday",
  203. "description" => "Feriados Base",
  204. "bits" => Permission::ALL_PERMS,
  205. "children" => [],
  206. ],
  207. ],
  208. ],
  209. // TBR
  210. [
  211. "scope" => "franchisor_tbr",
  212. "description" => "TBR",
  213. "bits" => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  214. "children" => [],
  215. ],
  216. // Configurações
  217. [
  218. "scope" => "config",
  219. "description" => "Configurações",
  220. "bits" => Permission::MENU | Permission::VIEW,
  221. "children" => [
  222. [
  223. "scope" => "config.user",
  224. "description" => "Usuários",
  225. "bits" => Permission::CRUD,
  226. "children" => [],
  227. ],
  228. [
  229. "scope" => "config.user-type",
  230. "description" => "Tipos de Usuário",
  231. "bits" => Permission::CRUD,
  232. "children" => [],
  233. ],
  234. [
  235. "scope" => "config.permission",
  236. "description" => "Permissões",
  237. "bits" => Permission::CRUD,
  238. "children" => [],
  239. ],
  240. [
  241. "scope" => "config.city",
  242. "description" => "Cidades",
  243. "bits" => Permission::CRUD,
  244. "children" => [],
  245. ],
  246. [
  247. "scope" => "config.country",
  248. "description" => "Países",
  249. "bits" => Permission::CRUD,
  250. "children" => [],
  251. ],
  252. [
  253. "scope" => "config.state",
  254. "description" => "Estados",
  255. "bits" => Permission::CRUD,
  256. "children" => [],
  257. ],
  258. ],
  259. ],
  260. // Unidade — escopos adicionais usados em subrotas de unit
  261. [
  262. "scope" => "unit-user",
  263. "description" => "Usuários da Unidade",
  264. "bits" => Permission::ALL_PERMS,
  265. "children" => [],
  266. ],
  267. ];
  268. $this->createPermissionsAndChildren(permissions: $permissions);
  269. }
  270. /**
  271. * Recursively creates or updates permissions and handles nesting.
  272. */
  273. private function createPermissionsAndChildren(
  274. array $permissions,
  275. ?Permission $parent = null,
  276. ): void {
  277. foreach ($permissions as $permissionData) {
  278. $children = $permissionData["children"];
  279. unset($permissionData["children"]);
  280. $permissionNode = Permission::updateOrCreate(
  281. ["scope" => $permissionData["scope"]],
  282. $permissionData,
  283. );
  284. if ($parent) {
  285. $parent->appendNode($permissionNode);
  286. }
  287. if (!empty($children)) {
  288. $this->createPermissionsAndChildren(
  289. permissions: $children,
  290. parent: $permissionNode,
  291. );
  292. }
  293. }
  294. }
  295. }