Browse Source

✨ (annotation): 团队 editor 及以上可在团队 channel 加批注、为模型签 agent 授权

- DiscussionService 判「句子管理者」改用 ChannelAbilityService::writableUids(),
  不再走 v2 ChannelApi::getCanEditByUser(只认拥有者 + 共享,不认团队角色)
- AgentGrantService 签发授权改用 ChannelAbilityService::canWrite()
- ChannelAbilityService 新增 canWrite() / writableUids()
- 团队 reader 与团队外的人仍 403;补对应测试

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 14 hours ago
parent
commit
8b64f8768a

+ 4 - 2
api-v13/app/Services/V3/AgentGrantService.php

@@ -2,7 +2,6 @@
 
 namespace App\Services\V3;
 
-use App\Http\Api\ChannelApi;
 use App\Models\AiModel;
 use Firebase\JWT\JWT;
 use Firebase\JWT\Key;
@@ -35,6 +34,8 @@ class AgentGrantService
 
     private const TYPE = 'agent-grant';
 
+    public function __construct(private readonly ChannelAbilityService $channels) {}
+
     /**
      * 人类为自己名下的模型签发 channel 写入授权。
      *
@@ -55,7 +56,8 @@ class AgentGrantService
         if ($model->owner_id !== $userUid) {
             throw new AuthorizationException(__('messages.agent_grant_not_owner'));
         }
-        if (! ChannelApi::userCanEdit($userUid, $data['target_id'])) {
+        // 认团队角色:团队 editor 及以上可以为团队名下的 channel 签授权
+        if (! $this->channels->canWrite($data['target_id'], $userUid)) {
             throw new AuthorizationException(__('messages.agent_grant_no_power'));
         }
 

+ 25 - 0
api-v13/app/Services/V3/ChannelAbilityService.php

@@ -119,6 +119,31 @@ class ChannelAbilityService
         }
     }
 
+    /**
+     * 能不能往某个 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<string>
+     */
+    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。
      */

+ 4 - 2
api-v13/app/Services/V3/DiscussionService.php

@@ -3,7 +3,6 @@
 namespace App\Services\V3;
 
 use App\Exceptions\BusinessException;
-use App\Http\Api\ChannelApi;
 use App\Http\Api\MdRender;
 use App\Http\Api\Mq;
 use App\Http\Api\ShareApi;
@@ -89,6 +88,7 @@ class DiscussionService
         private readonly UserService $users,
         private readonly AgentGrantService $grants,
         private readonly TipitakaReadingService $reading,
+        private readonly ChannelAbilityService $channels,
     ) {}
 
     /**
@@ -874,11 +874,13 @@ class DiscussionService
     }
 
     /**
+     * 能写内容的 channel:本人的、本人是团队 editor 及以上的团队名下的、共享权限 ≥20 的。
+     *
      * @return array<int, string>
      */
     private function editableChannels(string $userUid): array
     {
-        return $this->editableChannels[$userUid] ??= ChannelApi::getCanEditByUser($userUid);
+        return $this->editableChannels[$userUid] ??= $this->channels->writableUids($userUid);
     }
 
     /**

+ 20 - 0
api-v13/tests/Feature/AgentGrantV3Test.php

@@ -66,6 +66,26 @@ it('refuses to issue a grant without the right to do so', function () {
         ->assertStatus(422)->assertJsonValidationErrors('target_type');
 });
 
+it('lets a team editor grant a model on a team channel, but not a reader', function () {
+    [$team, $users] = makeTeam();
+    $channel = makeChannel($team);
+    $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
+
+    $model = AiModel::factory()->ownedBy($users['editor'])->create();
+    $grant = grantFor($users['editor'], $model, $channel);
+    $this->postJson('/api/v3/discussions', [
+        'target_type' => 'sentence', 'target_id' => $sentence, 'type' => 'note', 'content' => 'by agent',
+    ], modelHeader($model, $grant))
+        ->assertCreated()
+        ->assertJsonPath('data.editor.id', $users['editor'])
+        ->assertJsonPath('data.co_editor.id', $model->uid);
+
+    $readerModel = AiModel::factory()->ownedBy($users['reader'])->create();
+    $this->postJson('/api/v3/agent-grants', [
+        'ai_model' => $readerModel->uid, 'target_type' => 'channel', 'target_id' => $channel,
+    ], authHeader($users['reader']))->assertForbidden();
+});
+
 /* ------------------------------------------------------------------ 代写 */
 
 it('records the grantor as editor and the model as co-editor', function () {

+ 15 - 0
api-v13/tests/Feature/DiscussionV3Test.php

@@ -493,6 +493,21 @@ it('lets only the sentence manager create annotations, without notifying', funct
     Log::shouldNotHaveReceived('debug', fn (string $message) => $message === 'mq publish');
 });
 
+it('lets team editors and above, but not readers, annotate sentences in a team channel', function () {
+    [$team, $users] = makeTeam();
+    $target = makeTargetSentence($team);
+    $payload = ['target_type' => 'sentence', 'target_id' => $target, 'type' => 'note', 'content' => 'team note'];
+
+    foreach (['owner', 'manager', 'editor'] as $role) {
+        $this->postJson('/api/v3/discussions', $payload, authHeader($users[$role]))->assertCreated();
+    }
+    foreach (['reader', 'outsider'] as $role) {
+        $this->postJson('/api/v3/discussions', $payload, authHeader($users[$role]))->assertForbidden();
+    }
+    $this->getJson("/api/v3/discussions/summary?target_type=sentence&target_id={$target}", authHeader($users['editor']))
+        ->assertJsonPath('data.creatable_types', ['discussion', 'issue', 'glossary', 'note', 'commentary', 'assessment']);
+});
+
 it('validates annotation input', function () {
     $owner = makeStudio('owner');
     $target = makeTargetSentence($owner);