PermissionService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Permission;
  4. use Illuminate\Database\Eloquent\Collection;
  5. class PermissionService
  6. {
  7. public function getAll(): Collection
  8. {
  9. return Permission::orderBy("created_at", "desc")->get();
  10. }
  11. /**
  12. * Lista as permissões em ordem de árvore (pais antes dos filhos), com a profundidade,
  13. * para montar a matriz de permissões por perfil.
  14. */
  15. public function getTree(): Collection
  16. {
  17. return Permission::defaultOrder()->withDepth()->get();
  18. }
  19. public function findById(int $id): ?Permission
  20. {
  21. return Permission::find($id);
  22. }
  23. public function findByScope(string $scope): ?Permission
  24. {
  25. return Permission::where("scope", $scope)->first();
  26. }
  27. public function create(array $data): Permission
  28. {
  29. return Permission::create($data);
  30. }
  31. public function update(int $id, array $data): ?Permission
  32. {
  33. $model = $this->findById($id);
  34. if (!$model) {
  35. return null;
  36. }
  37. $model->update($data);
  38. return $model->fresh();
  39. }
  40. public function delete(int $id): bool
  41. {
  42. $model = $this->findById($id);
  43. if (!$model) {
  44. return false;
  45. }
  46. return $model->delete();
  47. }
  48. // Add custom business logic methods here
  49. }