abilitiesMany([$channel], $userUid)[$channel->uid]; } /** * 整页批量算:团队角色一次、共享权限一次,查询数与行数无关。 * * @param iterable $channels * @return array */ public function abilitiesMany(iterable $channels, ?string $userUid): array { $teams = $userUid ? $this->teamRoles($userUid) : []; $powers = []; if ($userUid) { $ids = []; foreach ($channels as $channel) { $ids[] = $channel->uid; } $powers = Share::whereIn('res_id', $ids) ->where('res_type', self::SHARE_CHANNEL) ->whereIn('cooperator_id', [$userUid, ...array_keys($teams)]) ->selectRaw('res_id, max(power) as power') ->groupBy('res_id') ->pluck('power', 'res_id') ->all(); } $result = []; foreach ($channels as $channel) { $read = (int) $channel->status >= Channel::STATUS_UNLISTED || (bool) $channel->is_system; $write = $update = $delete = false; if ($userUid !== null) { if ($channel->owner_uid === $userUid) { $write = $update = $delete = true; } else { $role = $teams[$channel->owner_uid] ?? null; $power = (int) ($powers[$channel->uid] ?? 0); $write = in_array($role, self::WRITER_ROLES, true) || $power >= self::POWER_WRITE; $update = GroupAbilityService::roleAllows($role, GroupAbilityService::UPDATE) || $power >= self::POWER_MANAGE; $delete = GroupAbilityService::roleAllows($role, GroupAbilityService::DELETE); $read = $read || $role !== null || $power >= self::POWER_READ; } } if ($channel->is_system) { $update = $delete = false; } $result[$channel->uid] = ['read' => $read || $write, 'write' => $write, 'update' => $update, 'delete' => $delete]; } return $result; } /** * 读正文前的闸:channel 不存在(或已软删)404,看不见 403。 * * 给按 channel 取正文的接口用(`tipitaka-reading`、`search/{id}` 的整章文档): * 私有 channel 只有拥有者、团队成员、被共享的人能读;公开与「公开不列出」谁都能读。 * * @throws AuthorizationException */ public function assertReadable(string $channelUid, ?string $userUid): void { $channel = Channel::query() ->where('uid', $channelUid) ->firstOrFail(['uid', 'owner_uid', 'status', 'is_system']); if (! $this->abilities($channel, $userUid)['read']) { throw new AuthorizationException(__('messages.channel_forbidden')); } } /** * 能不能往某个 channel 写内容(句子、批注等)。channel 不存在或已软删按不能写。 */ public function canWrite(string $channelUid, string $userUid): bool { $channel = Channel::query() ->where('uid', $channelUid) ->first(['uid', 'owner_uid', 'status', 'is_system']); return $channel !== null && $this->abilities($channel, $userUid)['write']; } /** * 本人能写内容的全部 channel uid({@see self::scopeWritable()} 的结果)。 * * @return list */ public function writableUids(string $userUid): array { $query = Channel::query(); $this->scopeWritable($query, $userUid); return $query->pluck('uid')->map(fn ($uid) => (string) $uid)->all(); } /** * 能不能在某个 studio(本人或团队)名下新建 channel。 */ public function canCreateIn(string $userUid, string $studioUid): bool { return $this->groups->can($userUid, $studioUid, GroupAbilityService::CREATE); } /** * 能不能看某个 studio(本人或团队)的工作区列表:本人,或该团队的任何成员。 */ public function canListStudio(string $userUid, string $studioUid): bool { return $this->groups->can($userUid, $studioUid, GroupAbilityService::READ); } /** * 查询约束:列表里列得出来的 channel——公开的(status 30)、系统的,加上(登录时)本人的、 * 本人所在团队名下的、共享给本人或本人所在团队的。status 20(公开不列出)只在后三种里出现。 * * @param string $table channels 表在查询里的名字(join 时可能带别名) */ public function scopeListable(Builder $query, ?string $userUid, string $table = 'channels'): void { $query->where(function (Builder $q) use ($userUid, $table) { $q->where("{$table}.status", '>=', Channel::STATUS_PUBLIC) ->orWhere("{$table}.is_system", true); if ($userUid === null) { return; } $teams = $this->teamRoles($userUid); $q->orWhere("{$table}.owner_uid", $userUid); if ($teams !== []) { $q->orWhereIn("{$table}.owner_uid", array_keys($teams)); } $shared = $this->sharedChannels([$userUid, ...array_keys($teams)], self::POWER_READ); if ($shared !== []) { $q->orWhereIn("{$table}.uid", $shared); } }); } /** * 查询约束:能写内容的 channel(本人的、本人是团队 editor 及以上的团队名下的、共享权限 ≥20 的)。 */ public function scopeWritable(Builder $query, string $userUid, string $table = 'channels'): void { $teams = $this->teamRoles($userUid); $writerTeams = array_keys(array_filter($teams, fn (string $role) => in_array($role, self::WRITER_ROLES, true))); $shared = $this->sharedChannels([$userUid, ...array_keys($teams)], self::POWER_WRITE); $query->where(function (Builder $q) use ($userUid, $table, $writerTeams, $shared) { $q->where("{$table}.owner_uid", $userUid); if ($writerTeams !== []) { $q->orWhereIn("{$table}.owner_uid", $writerTeams); } if ($shared !== []) { $q->orWhereIn("{$table}.uid", $shared); } }); } /** * 直接共享给某个 studio(本人或团队,**不含**经团队转来的)的 channel uid。 * * @return list */ public function sharedTo(string $studioUid): array { return $this->sharedChannels([$studioUid], self::POWER_READ); } /** * 本人所在的团队 → 角色。 * * @return array */ private function teamRoles(string $userUid): array { $roles = []; foreach (GroupMember::where('user_id', $userUid)->get(['group_id', 'role', 'power']) as $member) { $roles[$member->group_id] = $member->effectiveRole(); } return $roles; } /** * 共享给这些人 / 团队、且权限不低于 $minPower 的 channel uid。 * * @param list $cooperators * @return list */ private function sharedChannels(array $cooperators, int $minPower): array { return Share::whereIn('cooperator_id', $cooperators) ->where('res_type', self::SHARE_CHANNEL) ->where('power', '>=', $minPower) ->pluck('res_id') // shares.res_id 是 varchar,channels.uid 是 uuid;非法值进了 where 会让 pgsql 报错 ->filter(fn ($id) => Str::isUuid($id)) ->unique() ->values() ->all(); } }