['pcs', null, null, null], 2 => ['channel', Channel::class, 'name', 'owner_uid'], 3 => ['article', Article::class, 'title', 'owner'], 4 => ['collection', Collection::class, 'title', 'owner'], 5 => ['fragment', null, null, null], 6 => ['workflow', Project::class, 'title', 'owner_id'], 7 => ['project', Project::class, 'title', 'owner_id'], 8 => ['ai_model', AiModel::class, 'name', 'owner_id'], ]; public function __construct( private readonly GroupAbilityService $abilities, private readonly UserService $users, ) {} /** * 团队文档列表,每条附上资源的类型、标题与拥有者(按类型批量取,不逐行查)。 * * @return LengthAwarePaginator * * @throws AuthorizationException 不是成员 */ public function list(GroupInfo $group, string $viewerUid, int $perPage): LengthAwarePaginator { if (! $this->abilities->membership($viewerUid, $group->uid)) { throw new AuthorizationException(__('messages.group_not_member')); } $page = $group->shares()->orderByDesc('created_at')->orderBy('id')->paginate($perPage); $this->attachResources($page->getCollection()->all()); return $page; } /** * 把资源移出团队:团队的 owner / manager,或资源的拥有者。 * * @throws AuthorizationException */ public function remove(GroupInfo $group, Share $share, string $actorUid): void { $this->attachResources([$share]); $isResourceOwner = ($share->getAttribute('resource')['owner_id'] ?? null) === $actorUid; if (! $isResourceOwner) { $role = $this->abilities->roleIn($actorUid, $group->uid); if (! GroupAbilityService::roleAllows($role, GroupAbilityService::MANAGE_GROUP)) { throw new AuthorizationException(__('messages.group_forbidden')); } } $share->delete(); } /** * 按资源类型批量取标题与拥有者,挂到每行的 `resource` 属性上给 Resource 读。 * 资源已被删掉的,title / owner_id / owner 为 null;拥有者是团队(不是人)时 owner 为 null。 * * @param list $shares */ private function attachResources(array $shares): void { $byType = []; foreach ($shares as $share) { $byType[(int) $share->res_type][] = $share->res_id; } $found = []; foreach ($byType as $type => $ids) { [, $model, $titleColumn, $ownerColumn] = self::TYPES[$type] ?? [null, null, null, null]; if (! $model) { continue; } foreach ($model::query()->whereIn('uid', array_unique($ids))->get(['uid', $titleColumn, $ownerColumn]) as $row) { $found[$type][$row->uid] = ['title' => $row->{$titleColumn}, 'owner_id' => (string) $row->{$ownerColumn}]; } } $owners = $this->users->byUuids(array_merge( [], ...array_map(fn (array $rows) => array_column($rows, 'owner_id'), array_values($found)), )); foreach ($shares as $share) { $type = (int) $share->res_type; $ownerId = $found[$type][$share->res_id]['owner_id'] ?? null; $share->setAttribute('resource', [ 'type' => self::TYPES[$type][0] ?? 'unknown', 'id' => $share->res_id, 'title' => $found[$type][$share->res_id]['title'] ?? null, 'owner_id' => $ownerId, 'owner' => $ownerId ? ($owners[$ownerId] ?? null) : null, ]); } } }