DocumentationAccessService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services;
  4. use App\Models\DocumentationFolder;
  5. use App\Models\DocumentationFolderPermission;
  6. use App\Models\Role;
  7. use App\Models\User;
  8. use Illuminate\Database\Eloquent\Collection;
  9. class DocumentationAccessService
  10. {
  11. /** @var array<int, DocumentationFolder> */
  12. private array $folders = [];
  13. /** @var array<int, Collection<int, DocumentationFolderPermission>> */
  14. private array $effectiveRules = [];
  15. public function isAdministrator(User $user): bool
  16. {
  17. return $user->resolvedRoleSlug() === Role::ADMIN;
  18. }
  19. public function canRead(User $user, DocumentationFolder $folder): bool
  20. {
  21. return $this->can($user, $folder, false);
  22. }
  23. public function canWrite(User $user, DocumentationFolder $folder): bool
  24. {
  25. return $this->can($user, $folder, true);
  26. }
  27. public function canManagePermissions(User $user, DocumentationFolder $folder): bool
  28. {
  29. return $this->isAdministrator($user)
  30. || ($user->hasPermission('documents.update') && $this->canWrite($user, $folder));
  31. }
  32. /**
  33. * Returns readable folders plus their ancestors required to render the tree.
  34. * Ancestors without read access are navigation-only nodes.
  35. *
  36. * @return Collection<int, DocumentationFolder>
  37. */
  38. public function navigationFolders(User $user): Collection
  39. {
  40. $folders = DocumentationFolder::query()
  41. ->with(['permissionRules.role', 'creator'])
  42. ->orderBy('name')
  43. ->get();
  44. foreach ($folders as $folder) {
  45. $this->folders[$folder->id] = $folder;
  46. }
  47. $included = [];
  48. foreach ($folders as $folder) {
  49. $canRead = $this->canRead($user, $folder);
  50. $folder->setAttribute('can_read', $canRead);
  51. $folder->setAttribute('can_write', $this->canWrite($user, $folder));
  52. if (! $canRead) {
  53. continue;
  54. }
  55. $current = $folder;
  56. while ($current) {
  57. $included[$current->id] = true;
  58. $current = $current->parent_id ? ($this->folders[$current->parent_id] ?? null) : null;
  59. }
  60. }
  61. return $folders
  62. ->filter(fn (DocumentationFolder $folder): bool => isset($included[$folder->id]))
  63. ->values();
  64. }
  65. public function forgetCachedRules(): void
  66. {
  67. $this->effectiveRules = [];
  68. $this->folders = [];
  69. }
  70. private function can(User $user, DocumentationFolder $folder, bool $write): bool
  71. {
  72. if ($this->isAdministrator($user)) {
  73. return true;
  74. }
  75. $this->folders[$folder->id] = $folder;
  76. foreach ($this->rulesFor($folder) as $rule) {
  77. if (! $this->matches($rule, $user)) {
  78. continue;
  79. }
  80. if ($write && $rule->can_write) {
  81. return true;
  82. }
  83. if (! $write && ($rule->can_read || $rule->can_write)) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. /** @return Collection<int, DocumentationFolderPermission> */
  90. private function rulesFor(DocumentationFolder $folder): Collection
  91. {
  92. if (isset($this->effectiveRules[$folder->id])) {
  93. return $this->effectiveRules[$folder->id];
  94. }
  95. if ($folder->inherits_permissions && $folder->parent_id) {
  96. $parent = $this->folders[$folder->parent_id] ?? DocumentationFolder::query()
  97. ->with('permissionRules.role')
  98. ->find($folder->parent_id);
  99. if ($parent) {
  100. $this->folders[$parent->id] = $parent;
  101. return $this->effectiveRules[$folder->id] = $this->rulesFor($parent);
  102. }
  103. }
  104. $rules = $folder->relationLoaded('permissionRules')
  105. ? $folder->permissionRules
  106. : $folder->permissionRules()->with('role')->get();
  107. return $this->effectiveRules[$folder->id] = $rules;
  108. }
  109. private function matches(DocumentationFolderPermission $rule, User $user): bool
  110. {
  111. return match ($rule->subject_type) {
  112. DocumentationFolderPermission::SUBJECT_ALL => true,
  113. DocumentationFolderPermission::SUBJECT_USER => $rule->user_id === $user->id,
  114. DocumentationFolderPermission::SUBJECT_ROLE => $rule->role_id === $user->role_id
  115. || $rule->role?->slug === $user->resolvedRoleSlug(),
  116. default => false,
  117. };
  118. }
  119. }