GroupShareService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Models\AiModel;
  4. use App\Models\Article;
  5. use App\Models\Channel;
  6. use App\Models\Collection;
  7. use App\Models\GroupInfo;
  8. use App\Models\Project;
  9. use App\Models\Share;
  10. use App\Services\UserService;
  11. use Illuminate\Auth\Access\AuthorizationException;
  12. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  13. /**
  14. * 团队文档:共享给某个团队的资源(`shares` 表里 cooperator 是这个团队的行)。
  15. *
  16. * 取代 v2 `ShareController@index` 的 view=group,以及团队页上经 `DELETE /v2/share/{id}`
  17. * 把资源移出团队。资源本身的协作者管理(view=res、给人加共享)仍是 v2 share 资源,不在这里。
  18. *
  19. * 规则:成员都能看;团队的 owner / manager 或资源的拥有者能把资源移出团队。
  20. */
  21. class GroupShareService
  22. {
  23. /** shares.res_type 的编号 → 对外的类型名,以及取标题、拥有者的列 */
  24. private const TYPES = [
  25. 1 => ['pcs', null, null, null],
  26. 2 => ['channel', Channel::class, 'name', 'owner_uid'],
  27. 3 => ['article', Article::class, 'title', 'owner'],
  28. 4 => ['collection', Collection::class, 'title', 'owner'],
  29. 5 => ['fragment', null, null, null],
  30. 6 => ['workflow', Project::class, 'title', 'owner_id'],
  31. 7 => ['project', Project::class, 'title', 'owner_id'],
  32. 8 => ['ai_model', AiModel::class, 'name', 'owner_id'],
  33. ];
  34. public function __construct(
  35. private readonly GroupAbilityService $abilities,
  36. private readonly UserService $users,
  37. ) {}
  38. /**
  39. * 团队文档列表,每条附上资源的类型、标题与拥有者(按类型批量取,不逐行查)。
  40. *
  41. * @return LengthAwarePaginator<int, Share>
  42. *
  43. * @throws AuthorizationException 不是成员
  44. */
  45. public function list(GroupInfo $group, string $viewerUid, int $perPage): LengthAwarePaginator
  46. {
  47. if (! $this->abilities->membership($viewerUid, $group->uid)) {
  48. throw new AuthorizationException(__('messages.group_not_member'));
  49. }
  50. $page = $group->shares()->orderByDesc('created_at')->orderBy('id')->paginate($perPage);
  51. $this->attachResources($page->getCollection()->all());
  52. return $page;
  53. }
  54. /**
  55. * 把资源移出团队:团队的 owner / manager,或资源的拥有者。
  56. *
  57. * @throws AuthorizationException
  58. */
  59. public function remove(GroupInfo $group, Share $share, string $actorUid): void
  60. {
  61. $this->attachResources([$share]);
  62. $isResourceOwner = ($share->getAttribute('resource')['owner_id'] ?? null) === $actorUid;
  63. if (! $isResourceOwner) {
  64. $role = $this->abilities->roleIn($actorUid, $group->uid);
  65. if (! GroupAbilityService::roleAllows($role, GroupAbilityService::MANAGE_GROUP)) {
  66. throw new AuthorizationException(__('messages.group_forbidden'));
  67. }
  68. }
  69. $share->delete();
  70. }
  71. /**
  72. * 按资源类型批量取标题与拥有者,挂到每行的 `resource` 属性上给 Resource 读。
  73. * 资源已被删掉的,title / owner_id / owner 为 null;拥有者是团队(不是人)时 owner 为 null。
  74. *
  75. * @param list<Share> $shares
  76. */
  77. private function attachResources(array $shares): void
  78. {
  79. $byType = [];
  80. foreach ($shares as $share) {
  81. $byType[(int) $share->res_type][] = $share->res_id;
  82. }
  83. $found = [];
  84. foreach ($byType as $type => $ids) {
  85. [, $model, $titleColumn, $ownerColumn] = self::TYPES[$type] ?? [null, null, null, null];
  86. if (! $model) {
  87. continue;
  88. }
  89. foreach ($model::query()->whereIn('uid', array_unique($ids))->get(['uid', $titleColumn, $ownerColumn]) as $row) {
  90. $found[$type][$row->uid] = ['title' => $row->{$titleColumn}, 'owner_id' => (string) $row->{$ownerColumn}];
  91. }
  92. }
  93. $owners = $this->users->byUuids(array_merge(
  94. [],
  95. ...array_map(fn (array $rows) => array_column($rows, 'owner_id'), array_values($found)),
  96. ));
  97. foreach ($shares as $share) {
  98. $type = (int) $share->res_type;
  99. $ownerId = $found[$type][$share->res_id]['owner_id'] ?? null;
  100. $share->setAttribute('resource', [
  101. 'type' => self::TYPES[$type][0] ?? 'unknown',
  102. 'id' => $share->res_id,
  103. 'title' => $found[$type][$share->res_id]['title'] ?? null,
  104. 'owner_id' => $ownerId,
  105. 'owner' => $ownerId ? ($owners[$ownerId] ?? null) : null,
  106. ]);
  107. }
  108. }
  109. }