| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace App\Policies;
- use Illuminate\Auth\Access\HandlesAuthorization;
- use App\Models\Board;
- use App\Models\BoardMeta;
- use App\Models\Post;
- use App\Models\User;
- use App\Services\PostService;
- use App\Services\CommentService;
- class PostPolicy
- {
- use HandlesAuthorization;
- public PostService $postService;
- /**
- * Create a new policy instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->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;
- }
- }
|