Skip to content

Commit

Permalink
Update Post show, policy
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchanhyung98 committed May 12, 2024
1 parent 4b25694 commit f8e61d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function store(StoreRequest $request): MessageResource

public function show(Post $post): ShowResource
{
Gate::authorize('view', $post);

try {
$post->increment('hit');
} catch (Exception $e) {
Expand Down
12 changes: 10 additions & 2 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Support\Facades\Auth;

class PostPolicy
{
Expand All @@ -19,9 +20,16 @@ public function viewAny(User $user): bool
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Post $post): bool
public function view(?User $_, Post $post): Response
{
return true;
if (! $post->is_open) {
$user = Auth::guard('sanctum')->user();
if ($post->user_id !== $user?->id) {
return Response::deny(__('post.view_denied'));
}
}

return Response::allow();
}

/**
Expand Down
1 change: 1 addition & 0 deletions lang/ko/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

return [

'view_denied' => '게시글 작성자만 확인할 수 있습니다.',
'store' => '게시글이 등록되었습니다.',
'update' => '게시글이 수정되었습니다.',
'update_denied' => '게시글 작성자만 수정할 수 있습니다.',
Expand Down

0 comments on commit f8e61d0

Please sign in to comment.