Files
Gnuboard7/modules/_bundled/sirsoft-page/src/Http/Resources/PublicPageResource.php
T
chym1217 ba2adcf748 fix(page): 미발행 페이지를 권한 관리자가 사용자 화면에서 미리보도록 수정
미발행 페이지는 관리자여도 사용자 화면(/page/{slug})에 접근할 수 없어
발행 전 미리보기가 불가능했다. 페이지 조회 권한(sirsoft-page.pages.read)
보유 관리자는 미발행 페이지를 사용자 화면에서 미리볼 수 있게 하고, 미리보기
화면에 안내 배너를 노출한다. 비로그인·일반 회원은 기존대로 404를 유지한다.

- getPublishedPageBySlug 에 미리보기 허용 플래그 추가 (권한 판정은 컨트롤러)
- 미발행 미리보기 시 첨부 URL 을 admin 컨텍스트로 전환해 첨부까지 미리보기
- page/show.json 에 미발행 미리보기 안내 배너 + ko/en/ja 다국어
- E2E 시드 커맨드 구현 + 커맨드 등록 + 사용자 화면 미리보기 spec
2026-07-06 09:37:05 +09:00

78 lines
2.5 KiB
PHP

<?php
namespace Modules\Sirsoft\Page\Http\Resources;
use App\Http\Resources\BaseApiResource;
use Illuminate\Http\Request;
/**
* 페이지 API 리소스 (공개용)
*
* 발행된 페이지 공개 조회 시 사용합니다.
* 관리자 전용 정보(creator, updater 등)는 제외됩니다.
*/
class PublicPageResource extends BaseApiResource
{
/**
* 미발행 페이지 관리자 미리보기 여부.
*
* true 이면 첨부 URL을 발행 가드가 없는 'admin' 컨텍스트로 생성하여
* 관리자가 미발행 페이지의 첨부까지 미리볼 수 있도록 합니다.
*/
protected bool $preview = false;
/**
* 미리보기(미발행 관리자 조회) 여부를 설정합니다.
*
* @param bool $preview 미리보기 여부
* @return $this
*/
public function withPreview(bool $preview): self
{
$this->preview = $preview;
return $this;
}
/**
* 리소스를 배열로 변환합니다.
*
* @param Request $request HTTP 요청 객체
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$locale = app()->getLocale();
$fallback = config('app.fallback_locale', 'ko');
return [
'id' => $this->id,
'slug' => $this->slug,
'title' => $this->getLocalizedTitle(),
'content' => is_array($this->content)
? ($this->content[$locale] ?? $this->content[$fallback] ?? (! empty($this->content) ? array_values($this->content)[0] : ''))
: (string) ($this->content ?? ''),
'content_mode' => $this->content_mode ?? 'html',
'is_preview' => $this->preview,
'published_at' => $this->published_at
? $this->formatDateTimeStringForUser($this->published_at)
: null,
'seo_meta' => $this->seo_meta,
'current_version' => $this->current_version,
'attachments' => $this->whenLoaded(
'attachments',
fn () => PageAttachmentResource::collectionFor(
$this->attachments,
$this->preview ? 'admin' : 'public'
)
),
'created_at' => $this->created_at
? $this->formatDateTimeStringForUser($this->created_at)
: null,
'updated_at' => $this->updated_at
? $this->formatDateTimeStringForUser($this->updated_at)
: null,
];
}
}