Просмотр исходного кода

🛂 (library): 百科页按 id 打开词条:公开不列出可看、私有 403

原来按 guid 也套「会被列出」的口径,channel 公开不列出(20)的词条 404、私有的也 404。
改为「能访问」:公开 / 公开不列出 / 系统 channel / studio 级术语正常显示,私有 403,
不存在 404。列表、相关词条、其它版本仍只列公开的。studio 级判断兼容空串 channal。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda 19 часов назад
Родитель
Сommit
bac5d81e00
2 измененных файлов с 63 добавлено и 10 удалено
  1. 40 8
      api-v13/app/Services/WikiService.php
  2. 23 2
      api-v13/tests/Feature/WikiPageTest.php

+ 40 - 8
api-v13/app/Services/WikiService.php

@@ -9,7 +9,9 @@ use App\Services\Markup\Format;
 use App\Services\Markup\MarkupService;
 use App\Services\Markup\RenderOptions;
 use App\Services\V3\ChannelAbilityService;
+use Illuminate\Auth\Access\AuthorizationException;
 use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\ModelNotFoundException;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Str;
@@ -20,8 +22,9 @@ use Illuminate\Support\Str;
  * 直接查库,不经 OpenSearch:质量取 dhamma_terms.quality,分类取 term_categories(v3 写入时从正文
  * `{{category|…}}` 抽出),摘要取 summary 或正文第一段。正文用新渲染器({@see MarkupService})出 HTML。
  *
- * 只列公开的:所在 channel 会被列出(status 30 或系统 channel,{@see ChannelAbilityService::scopeListable()}),
- * 或 studio 级术语(不属于任何 channel)。
+ * 列表、相关词条、其它版本只列公开的:所在 channel 会被列出(status 30 或系统 channel,
+ * {@see ChannelAbilityService::scopeListable()}),或 studio 级术语(不属于任何 channel)。
+ * 按 guid 打开的词条页按「能访问」判(公开不列出的也能看,私有 403)。
  */
 class WikiService
 {
@@ -71,13 +74,15 @@ class WikiService
     /**
      * 按 guid 或词找要显示的词条。
      *
-     * - guid:这一条(要公开)
+     * - guid:这一条,拿到链接就能看(公开不列出的也行),私有 channel 的 403
      * - 词:先找该语言社区 channel(`_community_translation_{lang}_`)里的,没有就取公开词条里质量最高、最近更新的
+     *
+     * @throws AuthorizationException guid 指向私有 channel 的术语
      */
     public function find(string $lang, string $wordOrId): ?DhammaTerm
     {
         if (Str::isUuid($wordOrId)) {
-            return $this->publicTerms(null)->where('guid', $wordOrId)->first();
+            return $this->accessibleTerm($wordOrId);
         }
 
         $community = Channel::where('name', '_community_translation_'.strtolower($lang).'_')
@@ -97,6 +102,33 @@ class WikiService
             ->first();
     }
 
+    /**
+     * 按 guid 取一条:拿到链接就能看,与「会不会被列出」无关。
+     *
+     * channel 是公开(30)或公开不列出(20)、系统 channel、或 studio 级术语都能看;
+     * library 没有登录态,私有 channel 的一律 403({@see ChannelAbilityService::assertReadable()}),
+     * 术语或它所在的 channel 不存在(含软删)返回 null(404)。
+     *
+     * @throws AuthorizationException
+     */
+    private function accessibleTerm(string $guid): ?DhammaTerm
+    {
+        $term = DhammaTerm::where('guid', $guid)->first();
+        if ($term === null) {
+            return null;
+        }
+        $channelUid = $term->channelUid();
+        if ($channelUid !== null) {
+            try {
+                $this->channels->assertReadable($channelUid, null);
+            } catch (ModelNotFoundException) {
+                return null;
+            }
+        }
+
+        return $term;
+    }
+
     /**
      * 重定向的目标(`word` 或 `word@tag`):先在同一 channel(studio 级的在同一 owner 的 studio 级术语)里找,
      * 找不到返回 null(读者停在原词条上)。
@@ -112,9 +144,9 @@ class WikiService
         $tag === ''
             ? $query->where(fn (Builder $q) => $q->whereNull('tag')->orWhere('tag', ''))
             : $query->where('tag', $tag);
-        $term->channal !== null
-            ? $query->where('channal', $term->channal)
-            : $query->whereNull('channal')->where('owner', $term->owner);
+        $term->channelUid() !== null
+            ? $query->where('channal', $term->channelUid())
+            : $query->studioLevel()->where('owner', $term->owner);
 
         $target = $query->first();
 
@@ -220,7 +252,7 @@ class WikiService
     private function publicTerms(?string $lang): Builder
     {
         $query = DhammaTerm::query()->where(function (Builder $q) {
-            $q->whereNull('channal')->orWhereIn('channal', function ($sub) {
+            $q->studioLevel()->orWhereIn('channal', function ($sub) {
                 $sub->selectRaw('uid::text')->from('channels')->whereNull('deleted_at');
                 $this->channels->scopeListable($sub, null);
             });

+ 23 - 2
api-v13/tests/Feature/WikiPageTest.php

@@ -119,13 +119,34 @@ it('follows a redirect to the target term in the same channel', function () {
     $this->get('/library/wiki/zh-Hans/nirv%C4%81%E1%B9%87a')->assertRedirect(route('library.wiki.show', ['zh-Hans', $target]));
 });
 
-it('hides terms of private channels and unknown words', function () {
+it('forbids terms of private channels by id, and hides them and unknown words by word', function () {
     $owner = makeStudio('owner');
     $private = makeChannel($owner);
     Channel::where('uid', $private)->update(['status' => Channel::STATUS_PRIVATE]);
     $guid = wikiTerm($private, 'secret');
 
-    $this->get("/library/wiki/zh-Hans/{$guid}")->assertNotFound();
+    $this->get("/library/wiki/zh-Hans/{$guid}")->assertForbidden();
+    $this->get('/library/wiki/zh-Hans/'.Str::uuid())->assertNotFound();
     $this->get('/library/wiki/zh-Hans/secret')->assertNotFound();
     $this->get('/library/wiki/zh-Hans/no-such-word')->assertNotFound();
 });
+
+it('shows a term of an unlisted channel by id, but never lists it', function () {
+    $owner = makeStudio('owner');
+    $unlisted = makeChannel($owner);
+    Channel::where('uid', $unlisted)->update(['status' => Channel::STATUS_UNLISTED]);
+    $guid = wikiTerm($unlisted, 'zzunlisted', ['quality' => 'featured', 'note' => '不列出的正文。'], ['学处']);
+
+    expect($this->get("/library/wiki/zh-Hans/{$guid}")->assertOk()->getContent())->toContain('不列出的正文。');
+    $this->get('/library/wiki/zh-Hans/zzunlisted')->assertNotFound();
+    expect($this->get('/library/wiki/zh-Hans?category=vinaya&quality=pending')->assertOk()->getContent())
+        ->not->toContain('zzunlisted');
+});
+
+it('treats an empty-string channal as a studio-level term', function () {
+    $owner = makeStudio('owner');
+    $guid = wikiTerm(makeChannel($owner), 'legacy', ['note' => '老数据正文。']);
+    DhammaTerm::where('guid', $guid)->update(['channal' => '']);
+
+    expect($this->get("/library/wiki/zh-Hans/{$guid}")->assertOk()->getContent())->toContain('老数据正文。');
+});