PermissionSeeder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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" => "franchisee",
  22. "description" => "Franqueados",
  23. "bits" => Permission::MENU | Permission::VIEW,
  24. "children" => [
  25. [
  26. "scope" => "unit",
  27. "description" => "Unidades",
  28. "bits" => Permission::ALL_PERMS,
  29. "children" => [],
  30. ],
  31. [
  32. "scope" => "franchisee-unit",
  33. "description" => "Unidades do Franqueado",
  34. "bits" => Permission::ALL_PERMS,
  35. "children" => [],
  36. ],
  37. ],
  38. ],
  39. // Pacotes / Aulas
  40. [
  41. "scope" => "class-package",
  42. "description" => "Pacotes (Franqueadora)",
  43. "bits" => Permission::ALL_PERMS,
  44. "children" => [],
  45. ],
  46. [
  47. "scope" => "class-package-unit",
  48. "description" => "Pacotes (Unidade)",
  49. "bits" => Permission::ALL_PERMS,
  50. "children" => [],
  51. ],
  52. [
  53. "scope" => "class-package-franchisee",
  54. "description" => "Pacotes (Franqueado)",
  55. "bits" => Permission::ALL_PERMS,
  56. "children" => [],
  57. ],
  58. [
  59. "scope" => "class",
  60. "description" => "Aulas",
  61. "bits" => Permission::ALL_PERMS,
  62. "children" => [
  63. [
  64. "scope" => "class-attendance",
  65. "description" => "Frequência de Aulas",
  66. "bits" => Permission::ALL_PERMS,
  67. "children" => [],
  68. ],
  69. ],
  70. ],
  71. [
  72. "scope" => "modality",
  73. "description" => "Modalidades",
  74. "bits" => Permission::ALL_PERMS,
  75. "children" => [],
  76. ],
  77. // Alunos
  78. [
  79. "scope" => "student",
  80. "description" => "Alunos",
  81. "bits" => Permission::ALL_PERMS,
  82. "children" => [
  83. [
  84. "scope" => "student-history",
  85. "description" => "Histórico de Alunos",
  86. "bits" => Permission::ALL_PERMS,
  87. "children" => [],
  88. ],
  89. [
  90. "scope" => "student-media",
  91. "description" => "Mídias de Alunos",
  92. "bits" => Permission::ALL_PERMS,
  93. "children" => [],
  94. ],
  95. [
  96. "scope" => "student-responsible",
  97. "description" => "Responsáveis de Alunos",
  98. "bits" => Permission::ALL_PERMS,
  99. "children" => [],
  100. ],
  101. ],
  102. ],
  103. [
  104. "scope" => "student-contract",
  105. "description" => "Contratos de Alunos",
  106. "bits" => Permission::ALL_PERMS,
  107. "children" => [],
  108. ],
  109. [
  110. "scope" => "media",
  111. "description" => "Mídias",
  112. "bits" => Permission::ALL_PERMS,
  113. "children" => [],
  114. ],
  115. // Kanban
  116. [
  117. "scope" => "kanban",
  118. "description" => "Kanban",
  119. "bits" => Permission::ALL_PERMS,
  120. "children" => [
  121. [
  122. "scope" => "kanban-status",
  123. "description" => "Status do Kanban",
  124. "bits" => Permission::ALL_PERMS,
  125. "children" => [],
  126. ],
  127. [
  128. "scope" => "kanban-movement",
  129. "description" => "Movimentos do Kanban",
  130. "bits" => Permission::ALL_PERMS,
  131. "children" => [],
  132. ],
  133. ],
  134. ],
  135. // Suporte
  136. [
  137. "scope" => "support-ticket",
  138. "description" => "Chamados de Suporte",
  139. "bits" => Permission::ALL_PERMS,
  140. "children" => [
  141. [
  142. "scope" => "support-status",
  143. "description" => "Status de Suporte",
  144. "bits" => Permission::ALL_PERMS,
  145. "children" => [],
  146. ],
  147. [
  148. "scope" => "support-movement",
  149. "description" => "Movimentos de Suporte",
  150. "bits" => Permission::ALL_PERMS,
  151. "children" => [],
  152. ],
  153. ],
  154. ],
  155. // Notificações
  156. [
  157. "scope" => "notification",
  158. "description" => "Notificações",
  159. "bits" => Permission::ALL_PERMS,
  160. "children" => [
  161. [
  162. "scope" => "notification-recipient",
  163. "description" => "Destinatários de Notificações",
  164. "bits" => Permission::ALL_PERMS,
  165. "children" => [],
  166. ],
  167. ],
  168. ],
  169. // Produtos / Inventário
  170. [
  171. "scope" => "product",
  172. "description" => "Produtos",
  173. "bits" => Permission::ALL_PERMS,
  174. "children" => [
  175. [
  176. "scope" => "product-kit",
  177. "description" => "Kits de Produtos",
  178. "bits" => Permission::ALL_PERMS,
  179. "children" => [],
  180. ],
  181. [
  182. "scope" => "product-movement",
  183. "description" => "Movimentação de Produtos",
  184. "bits" => Permission::ALL_PERMS,
  185. "children" => [],
  186. ],
  187. [
  188. "scope" => "product-order",
  189. "description" => "Pedidos de Produtos",
  190. "bits" => Permission::ALL_PERMS,
  191. "children" => [
  192. [
  193. "scope" => "product-order-item",
  194. "description" => "Itens de Pedido de Produtos",
  195. "bits" => Permission::ALL_PERMS,
  196. "children" => [],
  197. ],
  198. ],
  199. ],
  200. ],
  201. ],
  202. [
  203. "scope" => "franchisor-inventory",
  204. "description" => "Estoque (Franqueadora)",
  205. "bits" => Permission::ALL_PERMS,
  206. "children" => [],
  207. ],
  208. [
  209. "scope" => "franchisee-inventory",
  210. "description" => "Estoque (Franqueado)",
  211. "bits" => Permission::ALL_PERMS,
  212. "children" => [],
  213. ],
  214. // Fornecedores / Pagamentos
  215. [
  216. "scope" => "supplier",
  217. "description" => "Fornecedores",
  218. "bits" => Permission::ALL_PERMS,
  219. "children" => [],
  220. ],
  221. [
  222. "scope" => "payment-method",
  223. "description" => "Formas de Pagamento",
  224. "bits" => Permission::ALL_PERMS,
  225. "children" => [],
  226. ],
  227. // Financeiro
  228. [
  229. "scope" => "financial",
  230. "description" => "Financeiro",
  231. "bits" => Permission::MENU | Permission::VIEW,
  232. "children" => [
  233. [
  234. "scope" => "financial-account-payable",
  235. "description" => "Contas a Pagar",
  236. "bits" => Permission::ALL_PERMS,
  237. "children" => [],
  238. ],
  239. [
  240. "scope" => "financial-account-receive",
  241. "description" => "Contas a Receber",
  242. "bits" => Permission::ALL_PERMS,
  243. "children" => [],
  244. ],
  245. [
  246. "scope" => "financial-invoice",
  247. "description" => "Notas Fiscais",
  248. "bits" => Permission::ALL_PERMS,
  249. "children" => [],
  250. ],
  251. [
  252. "scope" => "financial-plan-account",
  253. "description" => "Plano de Contas",
  254. "bits" => Permission::ALL_PERMS,
  255. "children" => [],
  256. ],
  257. ],
  258. ],
  259. // Tesouraria
  260. [
  261. "scope" => "treasury",
  262. "description" => "Tesouraria",
  263. "bits" => Permission::MENU | Permission::VIEW,
  264. "children" => [
  265. [
  266. "scope" => "treasury-account",
  267. "description" => "Contas de Tesouraria",
  268. "bits" => Permission::ALL_PERMS,
  269. "children" => [],
  270. ],
  271. [
  272. "scope" => "treasury-imports",
  273. "description" => "Importações de Tesouraria",
  274. "bits" => Permission::ALL_PERMS,
  275. "children" => [
  276. [
  277. "scope" => "treasury-import-line",
  278. "description" => "Linhas de Importação",
  279. "bits" => Permission::ALL_PERMS,
  280. "children" => [],
  281. ],
  282. ],
  283. ],
  284. [
  285. "scope" => "treasury-launch",
  286. "description" => "Lançamentos de Tesouraria",
  287. "bits" => Permission::ALL_PERMS,
  288. "children" => [],
  289. ],
  290. ],
  291. ],
  292. // Integrações
  293. [
  294. "scope" => "integrations",
  295. "description" => "Integrações",
  296. "bits" => Permission::ALL_PERMS,
  297. "children" => [
  298. [
  299. "scope" => "integration-variable",
  300. "description" => "Variáveis de Integração",
  301. "bits" => Permission::ALL_PERMS,
  302. "children" => [],
  303. ],
  304. ],
  305. ],
  306. // Feriados
  307. [
  308. "scope" => "holiday",
  309. "description" => "Feriados",
  310. "bits" => Permission::ALL_PERMS,
  311. "children" => [
  312. [
  313. "scope" => "base-holiday",
  314. "description" => "Feriados Base",
  315. "bits" => Permission::ALL_PERMS,
  316. "children" => [],
  317. ],
  318. ],
  319. ],
  320. // TBR
  321. [
  322. "scope" => "tbr",
  323. "description" => "TBR",
  324. "bits" => Permission::ALL_PERMS,
  325. "children" => [
  326. [
  327. "scope" => "tbr-calculation",
  328. "description" => "Cálculo de TBR",
  329. "bits" => Permission::VIEW | Permission::ADD,
  330. "children" => [],
  331. ],
  332. [
  333. "scope" => "inhabitant-classification",
  334. "description" => "Classificações de Habitantes",
  335. "bits" => Permission::ALL_PERMS,
  336. "children" => [],
  337. ],
  338. ],
  339. ],
  340. // Configurações
  341. [
  342. "scope" => "config",
  343. "description" => "Configurações",
  344. "bits" => Permission::MENU | Permission::VIEW,
  345. "children" => [
  346. [
  347. "scope" => "config.user",
  348. "description" => "Usuários",
  349. "bits" => Permission::CRUD,
  350. "children" => [],
  351. ],
  352. [
  353. "scope" => "config.user-type",
  354. "description" => "Tipos de Usuário",
  355. "bits" => Permission::CRUD,
  356. "children" => [],
  357. ],
  358. [
  359. "scope" => "config.permission",
  360. "description" => "Permissões",
  361. "bits" => Permission::CRUD,
  362. "children" => [],
  363. ],
  364. [
  365. "scope" => "config.city",
  366. "description" => "Cidades",
  367. "bits" => Permission::CRUD,
  368. "children" => [],
  369. ],
  370. [
  371. "scope" => "config.country",
  372. "description" => "Países",
  373. "bits" => Permission::CRUD,
  374. "children" => [],
  375. ],
  376. [
  377. "scope" => "config.state",
  378. "description" => "Estados",
  379. "bits" => Permission::CRUD,
  380. "children" => [],
  381. ],
  382. ],
  383. ],
  384. // Unidade — escopos adicionais usados em subrotas de unit
  385. [
  386. "scope" => "unit-user",
  387. "description" => "Usuários da Unidade",
  388. "bits" => Permission::ALL_PERMS,
  389. "children" => [],
  390. ],
  391. ];
  392. $this->createPermissionsAndChildren(permissions: $permissions);
  393. }
  394. /**
  395. * Recursively creates or updates permissions and handles nesting.
  396. */
  397. private function createPermissionsAndChildren(
  398. array $permissions,
  399. ?Permission $parent = null,
  400. ): void {
  401. foreach ($permissions as $permissionData) {
  402. $children = $permissionData["children"];
  403. unset($permissionData["children"]);
  404. $permissionNode = Permission::updateOrCreate(
  405. ["scope" => $permissionData["scope"]],
  406. $permissionData,
  407. );
  408. if ($parent) {
  409. $parent->appendNode($permissionNode);
  410. }
  411. if (!empty($children)) {
  412. $this->createPermissionsAndChildren(
  413. permissions: $children,
  414. parent: $permissionNode,
  415. );
  416. }
  417. }
  418. }
  419. }