postService = new PostService(); $this->commentService = new CommentService(); } /** * Determine whether the user can view any models. * * @param \App\Models\User $user * @return \Illuminate\Auth\Access\Response|bool */ public function viewAny(User $user) { // } /** * Determine whether the user can view the model. * * @param \App\Models\User $user * @param \App\Models\Post $post * @param \App\Models\BoardMeta $boardMeta * @return \Illuminate\Auth\Access\Response|bool */ public function view(?User $user, Post $post, BoardMeta $boardMeta) { // 게시글 보기 권한 확인 $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_2); if ($permitValue == 1 && !$user) { return $this->deny('로그인 후 이용 해주세요.', 401); } else if ($permitValue == 100 && !$user?->isAdministrator()) { return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_2]); } // 1:1 문의 게시판은 로그인 필수 if ($post->is_secret) { if (!$user) { return $this->deny('로그인 후 이용 해주세요.', 401); } if($user->id != $post->user_id) { return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_2]); } } return true; } /** * Determine whether the user can create models. * * @param \App\Models\User $user * @param \App\Models\Board $board * @param \App\Models\BoardMeta $boardMeta * @return \Illuminate\Auth\Access\Response|bool */ public function create(?User $user, Board $board, BoardMeta $boardMeta) { // 게시글 쓰기 권한 확인 $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_3, 0); if ($permitValue == 1 && !$user) { return $this->deny('로그인 후 이용 해주세요.', 401); } else if ($permitValue == 100 && !$user?->isAdministrator()) { return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_3]); } // 1:1 문의 게시판은 로그인 필수 if($boardMeta->item('use_personal', 0)) { if(!$user) { return $this->deny('로그인 후 이용 해주세요.', 401); } } // 글 한개만 작성 가능 if ($boardMeta->item('use_only_one_post', 0)) { if ($this->postService->userPostRows($board, $user) >= 1) { return $this->deny('이 게시판은 하루에 한 글만 작성 가능합니다.'); } } return true; } /** * Determine whether the user can update the model. * * @param \App\Models\User $user * @param \App\Models\Post $post * @param \App\Models\BoardMeta $boardMeta * @return \Illuminate\Auth\Access\Response|bool */ public function update(?User $user, Post $post, BoardMeta $boardMeta) { if(!$user && $post->user_id) { return $this->deny('로그인 후 이용 해주세요.', 401); }else if(!$post->user_id) { return true; } // 게시글 수정 금지 기간 확인 if(($protectPostDay = $boardMeta->item('protect_post_day', 0)) > 0) { if((time() - strtotime($post->created_at)) >= ($protectPostDay * 86400)) { return $this->deny(sprintf('%d일 이상된 게시글은 수정이 불가합니다.', $protectPostDay)); } } // 게시글 수정 보호 if($boardMeta->item('protect_update_post', 0) > 0) { if($post->comment_rows > 0) { return $this->deny('댓글이 달린 게시글은 수정이 불가합니다.'); } } return ($user->id == $post->user_id ? true : $this->deny("권한이 없습니다.")); } /** * Determine whether the user can delete the model. * * @param \App\Models\User $user * @param \App\Models\Post $post * @param \App\Models\BoardMeta $boardMeta * @return \Illuminate\Auth\Access\Response|bool */ public function delete(?User $user, Post $post, BoardMeta $boardMeta) { if(!$user && $post->user_id) { return $this->deny('로그인 후 이용 해주세요.', 401); }else if(!$post->user_id) { return true; } // 게시글 삭제 금지 기간 확인 if(($protectPostDay = $boardMeta->item('protect_post_day', 0)) > 0) { if((time() - strtotime($post->created_at)) >= ($protectPostDay * 86400)) { return $this->deny(sprintf('%d일 이상된 게시글은 삭제가 불가합니다.', $protectPostDay)); } } // 게시글 삭제 보호 if($boardMeta->item('protect_delete_post', 0) > 0) { if($post->comment_rows > 0) { return $this->deny('댓글이 달린 게시글은 삭제가 불가 합니다.'); } } return ($user->id == $post->user_id ? true : $this->deny("권한이 없습니다.")); } /** * Determine whether the user can restore the model. * * @param \App\Models\User $user * @param \App\Models\Post $post * @return \Illuminate\Auth\Access\Response|bool */ public function restore(User $user, Post $post) { // } /** * Determine whether the user can permanently delete the model. * * @param \App\Models\User $user * @param \App\Models\Post $post * @param \App\Models\BoardMeta $boardMeta * @return \Illuminate\Auth\Access\Response|bool */ public function forceDelete(?User $user, Post $post, BoardMeta $boardMeta) { if($boardMeta->item('block_delete', 0)) { if(!$user?->is_admin) { return $this->deny('이 게시글은 관리자만 삭제 가능합니다.'); } } return ($user?->id == $post->user_id ? true : $this->deny("권한이 없습니다.")); } /** * 이미지 첨부권한 확인 */ public function uploader(?User $user, BoardMeta $boardMeta) { $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_6, 0); if ($permitValue == 1 && !$user) { return $this->deny('로그인 후 이용 해주세요.', 401); } else if ($permitValue == 100 && !$user?->isAdministrator()) { return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_6]); } return true; } /** * 파일 다운로드 권한 확인 */ public function download(?User $user, Post $post, BoardMeta $boardMeta) { $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_7, 0); if ($permitValue == 1 && !$user || (!$user && $post->user_id)) { return $this->deny('로그인 후 이용 해주세요.', 401); } else if ($permitValue == 100 && !$user?->isAdministrator()) { return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_7]); } // 다운로드 제한 - 댓글 필수 if ($boardMeta->item('need_comment_for_download', 0)) { if ($this->commentService->userCommentRows($post, $user) <= 0) { return $this->deny('다운로드를 하려면 댓글이 필요합니다.'); } } // 다운로드 제한 - 추천 필수 if ($boardMeta->item('use_post_like', 0) && $boardMeta->item('need_like_for_download', 0)) { if (!$this->postService->userPostIsLike($post, $user)) { return $this->deny('다운로드를 하려면 추천이 필요합니다.'); } } return true; } }