PermissionSeeder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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" => "franchisor_support",
  125. "description" => "Chamados de Suporte",
  126. "bits" => Permission::ALL_PERMS,
  127. "children" => [],
  128. ],
  129. // Notificações
  130. [
  131. "scope" => "notification",
  132. "description" => "Notificações",
  133. "bits" => Permission::ALL_PERMS,
  134. "children" => [
  135. [
  136. "scope" => "notification-recipient",
  137. "description" => "Destinatários de Notificações",
  138. "bits" => Permission::ALL_PERMS,
  139. "children" => [],
  140. ],
  141. ],
  142. ],
  143. // Produtos / Inventário
  144. [
  145. "scope" => "franchisor_products",
  146. "description" => "Produtos",
  147. "bits" => Permission::ALL_PERMS,
  148. "children" => [
  149. [
  150. "scope" => "product-kit",
  151. "description" => "Kits de Produtos",
  152. "bits" => Permission::ALL_PERMS,
  153. "children" => [],
  154. ],
  155. [
  156. "scope" => "product-movement",
  157. "description" => "Movimentação de Produtos",
  158. "bits" => Permission::ALL_PERMS,
  159. "children" => [],
  160. ],
  161. [
  162. "scope" => "product-order",
  163. "description" => "Pedidos de Produtos",
  164. "bits" => Permission::ALL_PERMS,
  165. "children" => [
  166. [
  167. "scope" => "product-order-item",
  168. "description" => "Itens de Pedido de Produtos",
  169. "bits" => Permission::ALL_PERMS,
  170. "children" => [],
  171. ],
  172. ],
  173. ],
  174. ],
  175. ],
  176. [
  177. "scope" => "franchisor-inventory",
  178. "description" => "Estoque (Franqueadora)",
  179. "bits" => Permission::ALL_PERMS,
  180. "children" => [],
  181. ],
  182. [
  183. "scope" => "franchisee-inventory",
  184. "description" => "Estoque (Franqueado)",
  185. "bits" => Permission::ALL_PERMS,
  186. "children" => [],
  187. ],
  188. // Fornecedores / Pagamentos
  189. [
  190. "scope" => "supplier",
  191. "description" => "Fornecedores",
  192. "bits" => Permission::ALL_PERMS,
  193. "children" => [],
  194. ],
  195. [
  196. "scope" => "payment-method",
  197. "description" => "Formas de Pagamento",
  198. "bits" => Permission::ALL_PERMS,
  199. "children" => [],
  200. ],
  201. // Financeiro
  202. [
  203. "scope" => "franchisor_financial",
  204. "description" => "Financeiro",
  205. "bits" => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  206. "children" => [],
  207. ],
  208. // Feriados
  209. [
  210. "scope" => "holiday",
  211. "description" => "Feriados",
  212. "bits" => Permission::ALL_PERMS,
  213. "children" => [
  214. [
  215. "scope" => "base-holiday",
  216. "description" => "Feriados Base",
  217. "bits" => Permission::ALL_PERMS,
  218. "children" => [],
  219. ],
  220. ],
  221. ],
  222. // TBR
  223. [
  224. "scope" => "franchisor_tbr",
  225. "description" => "TBR",
  226. "bits" => Permission::MENU | Permission::VIEW | Permission::ADD | Permission::EDIT,
  227. "children" => [],
  228. ],
  229. // Configurações
  230. [
  231. "scope" => "config",
  232. "description" => "Configurações",
  233. "bits" => Permission::MENU | Permission::VIEW,
  234. "children" => [
  235. [
  236. "scope" => "config.user",
  237. "description" => "Usuários",
  238. "bits" => Permission::CRUD,
  239. "children" => [],
  240. ],
  241. [
  242. "scope" => "config.user-type",
  243. "description" => "Tipos de Usuário",
  244. "bits" => Permission::CRUD,
  245. "children" => [],
  246. ],
  247. [
  248. "scope" => "config.permission",
  249. "description" => "Permissões",
  250. "bits" => Permission::CRUD,
  251. "children" => [],
  252. ],
  253. [
  254. "scope" => "config.city",
  255. "description" => "Cidades",
  256. "bits" => Permission::CRUD,
  257. "children" => [],
  258. ],
  259. [
  260. "scope" => "config.country",
  261. "description" => "Países",
  262. "bits" => Permission::CRUD,
  263. "children" => [],
  264. ],
  265. [
  266. "scope" => "config.state",
  267. "description" => "Estados",
  268. "bits" => Permission::CRUD,
  269. "children" => [],
  270. ],
  271. ],
  272. ],
  273. // Unidade — escopos adicionais usados em subrotas de unit
  274. [
  275. "scope" => "unit-user",
  276. "description" => "Usuários da Unidade",
  277. "bits" => Permission::ALL_PERMS,
  278. "children" => [],
  279. ],
  280. ];
  281. $this->createPermissionsAndChildren(permissions: $permissions);
  282. }
  283. /**
  284. * Recursively creates or updates permissions and handles nesting.
  285. */
  286. private function createPermissionsAndChildren(
  287. array $permissions,
  288. ?Permission $parent = null,
  289. ): void {
  290. foreach ($permissions as $permissionData) {
  291. $children = $permissionData["children"];
  292. unset($permissionData["children"]);
  293. $permissionNode = Permission::updateOrCreate(
  294. ["scope" => $permissionData["scope"]],
  295. $permissionData,
  296. );
  297. if ($parent) {
  298. $parent->appendNode($permissionNode);
  299. }
  300. if (!empty($children)) {
  301. $this->createPermissionsAndChildren(
  302. permissions: $children,
  303. parent: $permissionNode,
  304. );
  305. }
  306. }
  307. }
  308. }