GroupMemberService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Models\GroupInfo;
  4. use App\Models\GroupMember;
  5. use App\Models\UserInfo;
  6. use App\Services\UserService;
  7. use Illuminate\Auth\Access\AuthorizationException;
  8. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  9. use Illuminate\Validation\ValidationException;
  10. /**
  11. * 团队成员的业务逻辑:我加入的团队、成员列表、加人、改角色、移人 / 退出。
  12. *
  13. * 取代 v2 `GroupMemberController` 的 view=user / view=group / store / destroy。
  14. * v2 那份原地保留不动(铁律 2),两边不共用代码。
  15. *
  16. * 规则:
  17. * - 看成员列表:任何成员
  18. * - 加人、移人、改角色:owner 与 manager({@see GroupAbilityService::MANAGE_MEMBERS})
  19. * - owner 那一行谁都不能改、不能移;本人可以退出(owner 除外)
  20. * - 改角色时 role 与 power 同写,v4 看到的身份与 v3 一致
  21. */
  22. class GroupMemberService
  23. {
  24. public function __construct(private readonly GroupAbilityService $abilities) {}
  25. /**
  26. * 我加入的团队,按加入时间排序。
  27. *
  28. * `owned`:true 只要我创建的,false 只要我参加的别人的团队(取代 v2 的 view2 与
  29. * `group-my-number`——数量读分页的 total)。`search`:按团队名模糊匹配。
  30. *
  31. * @param array{owned?: bool, search?: string|null} $filters
  32. * @return LengthAwarePaginator<int, GroupMember>
  33. */
  34. public function listForUser(string $userUid, array $filters, int $perPage): LengthAwarePaginator
  35. {
  36. return GroupMember::with([
  37. 'group:uid,name,description,owner,status,created_at',
  38. 'group.'.UserService::eagerLoadActor('creator')[0],
  39. ])
  40. ->where('user_id', $userUid)
  41. ->whereHas('group', function ($query) use ($userUid, $filters) {
  42. if (array_key_exists('owned', $filters)) {
  43. $query->where('owner', $filters['owned'] ? '=' : '<>', $userUid);
  44. }
  45. if (! empty($filters['search'])) {
  46. $query->where('name', 'ilike', '%'.$filters['search'].'%');
  47. }
  48. })
  49. ->orderBy('created_at')
  50. ->orderBy('id')
  51. ->paginate($perPage);
  52. }
  53. /**
  54. * 团队的成员列表,只有成员看得到。
  55. *
  56. * @return LengthAwarePaginator<int, GroupMember>
  57. *
  58. * @throws AuthorizationException 不是成员
  59. */
  60. public function listForGroup(GroupInfo $group, string $viewerUid, int $perPage): LengthAwarePaginator
  61. {
  62. if (! $this->abilities->membership($viewerUid, $group->uid)) {
  63. throw new AuthorizationException(__('messages.group_not_member'));
  64. }
  65. return $group->members()
  66. ->with(UserService::eagerLoadActor()[0])
  67. ->orderBy('power')
  68. ->orderBy('created_at')
  69. ->orderBy('id')
  70. ->paginate($perPage);
  71. }
  72. /**
  73. * 加一名成员。
  74. *
  75. * @param array{user_id: string, role?: string} $data
  76. *
  77. * @throws AuthorizationException 调用者不能管理成员
  78. * @throws ValidationException 用户不存在或已是成员
  79. */
  80. public function add(GroupInfo $group, string $actorUid, array $data): GroupMember
  81. {
  82. $this->authorizeManage($group, $actorUid);
  83. if (! UserInfo::where('userid', $data['user_id'])->exists()) {
  84. throw ValidationException::withMessages(['user_id' => __('messages.group_user_not_found')]);
  85. }
  86. if ($group->members()->where('user_id', $data['user_id'])->exists()) {
  87. throw ValidationException::withMessages(['user_id' => __('messages.group_member_exists')]);
  88. }
  89. $role = $data['role'] ?? GroupMember::ROLE_READER;
  90. $member = new GroupMember;
  91. $member->forceFill([
  92. 'id' => app('snowflake')->id(),
  93. 'user_id' => $data['user_id'],
  94. 'group_id' => $group->uid,
  95. 'group_name' => $group->name,
  96. 'role' => $role,
  97. 'power' => GroupMember::powerOf($role),
  98. ])->save();
  99. return $member->load(UserService::eagerLoadActor()[0]);
  100. }
  101. /**
  102. * 改成员角色。
  103. *
  104. * @param array{role?: string} $data
  105. *
  106. * @throws AuthorizationException 调用者不能管理成员,或目标是 owner
  107. */
  108. public function update(GroupInfo $group, GroupMember $member, string $actorUid, array $data): GroupMember
  109. {
  110. $this->authorizeManage($group, $actorUid);
  111. $this->guardOwner($member);
  112. if (isset($data['role'])) {
  113. $member->forceFill([
  114. 'role' => $data['role'],
  115. 'power' => GroupMember::powerOf($data['role']),
  116. ])->save();
  117. }
  118. return $member->load(UserService::eagerLoadActor()[0]);
  119. }
  120. /**
  121. * 移除成员,或本人退出。owner 不能被移除、也不能退出。
  122. *
  123. * @throws AuthorizationException
  124. */
  125. public function remove(GroupInfo $group, GroupMember $member, string $actorUid): void
  126. {
  127. $this->guardOwner($member);
  128. if ($member->user_id !== $actorUid) {
  129. $this->authorizeManage($group, $actorUid);
  130. }
  131. $member->delete();
  132. }
  133. /**
  134. * @throws AuthorizationException
  135. */
  136. private function authorizeManage(GroupInfo $group, string $actorUid): void
  137. {
  138. $role = $this->abilities->roleIn($actorUid, $group->uid);
  139. if (! GroupAbilityService::roleAllows($role, GroupAbilityService::MANAGE_MEMBERS)) {
  140. throw new AuthorizationException(__('messages.group_forbidden'));
  141. }
  142. }
  143. /**
  144. * @throws AuthorizationException
  145. */
  146. private function guardOwner(GroupMember $member): void
  147. {
  148. if ($member->effectiveRole() === GroupMember::ROLE_OWNER) {
  149. throw new AuthorizationException(__('messages.group_owner_immutable'));
  150. }
  151. }
  152. }