PermissionSeeder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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" => "kanban",
  105. "description" => "Kanban",
  106. "bits" => Permission::ALL_PERMS,
  107. "children" => [
  108. [
  109. "scope" => "kanban-status",
  110. "description" => "Status do Kanban",
  111. "bits" => Permission::ALL_PERMS,
  112. "children" => [],
  113. ],
  114. [
  115. "scope" => "kanban-movement",
  116. "description" => "Movimentos do Kanban",
  117. "bits" => Permission::ALL_PERMS,
  118. "children" => [],
  119. ],
  120. ],
  121. ],
  122. // Suporte
  123. [
  124. "scope" => "support-ticket",
  125. "description" => "Chamados de Suporte",
  126. "bits" => Permission::ALL_PERMS,
  127. "children" => [
  128. [
  129. "scope" => "support-status",
  130. "description" => "Status de Suporte",
  131. "bits" => Permission::ALL_PERMS,
  132. "children" => [],
  133. ],
  134. [
  135. "scope" => "support-movement",
  136. "description" => "Movimentos de Suporte",
  137. "bits" => Permission::ALL_PERMS,
  138. "children" => [],
  139. ],
  140. ],
  141. ],
  142. // Notificações
  143. [
  144. "scope" => "notification",
  145. "description" => "Notificações",
  146. "bits" => Permission::ALL_PERMS,
  147. "children" => [
  148. [
  149. "scope" => "notification-recipient",
  150. "description" => "Destinatários de Notificações",
  151. "bits" => Permission::ALL_PERMS,
  152. "children" => [],
  153. ],
  154. ],
  155. ],
  156. // Produtos / Inventário
  157. [
  158. "scope" => "product",
  159. "description" => "Produtos",
  160. "bits" => Permission::ALL_PERMS,
  161. "children" => [
  162. [
  163. "scope" => "product-kit",
  164. "description" => "Kits de Produtos",
  165. "bits" => Permission::ALL_PERMS,
  166. "children" => [],
  167. ],
  168. [
  169. "scope" => "product-movement",
  170. "description" => "Movimentação de Produtos",
  171. "bits" => Permission::ALL_PERMS,
  172. "children" => [],
  173. ],
  174. [
  175. "scope" => "product-order",
  176. "description" => "Pedidos de Produtos",
  177. "bits" => Permission::ALL_PERMS,
  178. "children" => [
  179. [
  180. "scope" => "product-order-item",
  181. "description" => "Itens de Pedido de Produtos",
  182. "bits" => Permission::ALL_PERMS,
  183. "children" => [],
  184. ],
  185. ],
  186. ],
  187. ],
  188. ],
  189. [
  190. "scope" => "franchisor-inventory",
  191. "description" => "Estoque (Franqueadora)",
  192. "bits" => Permission::ALL_PERMS,
  193. "children" => [],
  194. ],
  195. [
  196. "scope" => "franchisee-inventory",
  197. "description" => "Estoque (Franqueado)",
  198. "bits" => Permission::ALL_PERMS,
  199. "children" => [],
  200. ],
  201. // Fornecedores / Pagamentos
  202. [
  203. "scope" => "supplier",
  204. "description" => "Fornecedores",
  205. "bits" => Permission::ALL_PERMS,
  206. "children" => [],
  207. ],
  208. [
  209. "scope" => "payment-method",
  210. "description" => "Formas de Pagamento",
  211. "bits" => Permission::ALL_PERMS,
  212. "children" => [],
  213. ],
  214. // Financeiro
  215. [
  216. "scope" => "financial",
  217. "description" => "Financeiro",
  218. "bits" => Permission::MENU | Permission::VIEW,
  219. "children" => [
  220. [
  221. "scope" => "financial-account-payable",
  222. "description" => "Contas a Pagar",
  223. "bits" => Permission::ALL_PERMS,
  224. "children" => [],
  225. ],
  226. [
  227. "scope" => "financial-account-receive",
  228. "description" => "Contas a Receber",
  229. "bits" => Permission::ALL_PERMS,
  230. "children" => [],
  231. ],
  232. [
  233. "scope" => "financial-invoice",
  234. "description" => "Notas Fiscais",
  235. "bits" => Permission::ALL_PERMS,
  236. "children" => [],
  237. ],
  238. [
  239. "scope" => "financial-plan-account",
  240. "description" => "Plano de Contas",
  241. "bits" => Permission::ALL_PERMS,
  242. "children" => [],
  243. ],
  244. ],
  245. ],
  246. // Tesouraria
  247. [
  248. "scope" => "treasury",
  249. "description" => "Tesouraria",
  250. "bits" => Permission::MENU | Permission::VIEW,
  251. "children" => [
  252. [
  253. "scope" => "treasury-account",
  254. "description" => "Contas de Tesouraria",
  255. "bits" => Permission::ALL_PERMS,
  256. "children" => [],
  257. ],
  258. [
  259. "scope" => "treasury-imports",
  260. "description" => "Importações de Tesouraria",
  261. "bits" => Permission::ALL_PERMS,
  262. "children" => [
  263. [
  264. "scope" => "treasury-import-line",
  265. "description" => "Linhas de Importação",
  266. "bits" => Permission::ALL_PERMS,
  267. "children" => [],
  268. ],
  269. ],
  270. ],
  271. [
  272. "scope" => "treasury-launch",
  273. "description" => "Lançamentos de Tesouraria",
  274. "bits" => Permission::ALL_PERMS,
  275. "children" => [],
  276. ],
  277. ],
  278. ],
  279. // Integrações
  280. [
  281. "scope" => "integrations",
  282. "description" => "Integrações",
  283. "bits" => Permission::ALL_PERMS,
  284. "children" => [
  285. [
  286. "scope" => "integration-variable",
  287. "description" => "Variáveis de Integração",
  288. "bits" => Permission::ALL_PERMS,
  289. "children" => [],
  290. ],
  291. ],
  292. ],
  293. // Feriados
  294. [
  295. "scope" => "holiday",
  296. "description" => "Feriados",
  297. "bits" => Permission::ALL_PERMS,
  298. "children" => [
  299. [
  300. "scope" => "base-holiday",
  301. "description" => "Feriados Base",
  302. "bits" => Permission::ALL_PERMS,
  303. "children" => [],
  304. ],
  305. ],
  306. ],
  307. // TBR
  308. [
  309. "scope" => "tbr",
  310. "description" => "TBR",
  311. "bits" => Permission::ALL_PERMS,
  312. "children" => [
  313. [
  314. "scope" => "tbr-calculation",
  315. "description" => "Cálculo de TBR",
  316. "bits" => Permission::VIEW | Permission::ADD,
  317. "children" => [],
  318. ],
  319. [
  320. "scope" => "inhabitant-classification",
  321. "description" => "Classificações de Habitantes",
  322. "bits" => Permission::ALL_PERMS,
  323. "children" => [],
  324. ],
  325. ],
  326. ],
  327. // Configurações
  328. [
  329. "scope" => "config",
  330. "description" => "Configurações",
  331. "bits" => Permission::MENU | Permission::VIEW,
  332. "children" => [
  333. [
  334. "scope" => "config.user",
  335. "description" => "Usuários",
  336. "bits" => Permission::CRUD,
  337. "children" => [],
  338. ],
  339. [
  340. "scope" => "config.user-type",
  341. "description" => "Tipos de Usuário",
  342. "bits" => Permission::CRUD,
  343. "children" => [],
  344. ],
  345. [
  346. "scope" => "config.permission",
  347. "description" => "Permissões",
  348. "bits" => Permission::CRUD,
  349. "children" => [],
  350. ],
  351. [
  352. "scope" => "config.city",
  353. "description" => "Cidades",
  354. "bits" => Permission::CRUD,
  355. "children" => [],
  356. ],
  357. [
  358. "scope" => "config.country",
  359. "description" => "Países",
  360. "bits" => Permission::CRUD,
  361. "children" => [],
  362. ],
  363. [
  364. "scope" => "config.state",
  365. "description" => "Estados",
  366. "bits" => Permission::CRUD,
  367. "children" => [],
  368. ],
  369. ],
  370. ],
  371. // Unidade — escopos adicionais usados em subrotas de unit
  372. [
  373. "scope" => "unit-user",
  374. "description" => "Usuários da Unidade",
  375. "bits" => Permission::ALL_PERMS,
  376. "children" => [],
  377. ],
  378. ];
  379. $this->createPermissionsAndChildren(permissions: $permissions);
  380. }
  381. /**
  382. * Recursively creates or updates permissions and handles nesting.
  383. */
  384. private function createPermissionsAndChildren(
  385. array $permissions,
  386. ?Permission $parent = null,
  387. ): void {
  388. foreach ($permissions as $permissionData) {
  389. $children = $permissionData["children"];
  390. unset($permissionData["children"]);
  391. $permissionNode = Permission::updateOrCreate(
  392. ["scope" => $permissionData["scope"]],
  393. $permissionData,
  394. );
  395. if ($parent) {
  396. $parent->appendNode($permissionNode);
  397. }
  398. if (!empty($children)) {
  399. $this->createPermissionsAndChildren(
  400. permissions: $children,
  401. parent: $permissionNode,
  402. );
  403. }
  404. }
  405. }
  406. }