PermissionSeeder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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" => "franchisor_products",
  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" => "franchisor_financial",
  217. "description" => "Financeiro",
  218. "bits" => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  219. "children" => [],
  220. ],
  221. // Feriados
  222. [
  223. "scope" => "holiday",
  224. "description" => "Feriados",
  225. "bits" => Permission::ALL_PERMS,
  226. "children" => [
  227. [
  228. "scope" => "base-holiday",
  229. "description" => "Feriados Base",
  230. "bits" => Permission::ALL_PERMS,
  231. "children" => [],
  232. ],
  233. ],
  234. ],
  235. // TBR
  236. [
  237. "scope" => "franchisor_tbr",
  238. "description" => "TBR",
  239. "bits" => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  240. "children" => [],
  241. ],
  242. // Configurações
  243. [
  244. "scope" => "config",
  245. "description" => "Configurações",
  246. "bits" => Permission::MENU | Permission::VIEW,
  247. "children" => [
  248. [
  249. "scope" => "config.user",
  250. "description" => "Usuários",
  251. "bits" => Permission::CRUD,
  252. "children" => [],
  253. ],
  254. [
  255. "scope" => "config.user-type",
  256. "description" => "Tipos de Usuário",
  257. "bits" => Permission::CRUD,
  258. "children" => [],
  259. ],
  260. [
  261. "scope" => "config.permission",
  262. "description" => "Permissões",
  263. "bits" => Permission::CRUD,
  264. "children" => [],
  265. ],
  266. [
  267. "scope" => "config.city",
  268. "description" => "Cidades",
  269. "bits" => Permission::CRUD,
  270. "children" => [],
  271. ],
  272. [
  273. "scope" => "config.country",
  274. "description" => "Países",
  275. "bits" => Permission::CRUD,
  276. "children" => [],
  277. ],
  278. [
  279. "scope" => "config.state",
  280. "description" => "Estados",
  281. "bits" => Permission::CRUD,
  282. "children" => [],
  283. ],
  284. ],
  285. ],
  286. // Unidade — escopos adicionais usados em subrotas de unit
  287. [
  288. "scope" => "unit-user",
  289. "description" => "Usuários da Unidade",
  290. "bits" => Permission::ALL_PERMS,
  291. "children" => [],
  292. ],
  293. ];
  294. $this->createPermissionsAndChildren(permissions: $permissions);
  295. }
  296. /**
  297. * Recursively creates or updates permissions and handles nesting.
  298. */
  299. private function createPermissionsAndChildren(
  300. array $permissions,
  301. ?Permission $parent = null,
  302. ): void {
  303. foreach ($permissions as $permissionData) {
  304. $children = $permissionData["children"];
  305. unset($permissionData["children"]);
  306. $permissionNode = Permission::updateOrCreate(
  307. ["scope" => $permissionData["scope"]],
  308. $permissionData,
  309. );
  310. if ($parent) {
  311. $parent->appendNode($permissionNode);
  312. }
  313. if (!empty($children)) {
  314. $this->createPermissionsAndChildren(
  315. permissions: $children,
  316. parent: $permissionNode,
  317. );
  318. }
  319. }
  320. }
  321. }