PostPolicy.php~ 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Policies;
  3. use Illuminate\Auth\Access\HandlesAuthorization;
  4. use App\Models\Board;
  5. use App\Models\BoardMeta;
  6. use App\Models\Post;
  7. use App\Models\User;
  8. use App\Services\PostService;
  9. use App\Services\CommentService;
  10. class PostPolicy
  11. {
  12. use HandlesAuthorization;
  13. public PostService $postService;
  14. /**
  15. * Create a new policy instance.
  16. *
  17. * @return void
  18. */
  19. public function __construct()
  20. {
  21. $this->postService = new PostService();
  22. $this->commentService = new CommentService();
  23. }
  24. /**
  25. * Determine whether the user can view any models.
  26. *
  27. * @param \App\Models\User $user
  28. * @return \Illuminate\Auth\Access\Response|bool
  29. */
  30. public function viewAny(User $user)
  31. {
  32. //
  33. }
  34. /**
  35. * Determine whether the user can view the model.
  36. *
  37. * @param \App\Models\User $user
  38. * @param \App\Models\Post $post
  39. * @param \App\Models\BoardMeta $boardMeta
  40. * @return \Illuminate\Auth\Access\Response|bool
  41. */
  42. public function view(?User $user, Post $post, BoardMeta $boardMeta)
  43. {
  44. // 게시글 보기 권한 확인
  45. $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_2);
  46. if ($permitValue == 1 && !$user) {
  47. return $this->deny('로그인 후 이용 해주세요.', 401);
  48. } else if ($permitValue == 100 && !$user?->isAdministrator()) {
  49. return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_2]);
  50. }
  51. echo '<pre>';print_r($permitValue);echo '</pre>';exit;
  52. // 1:1 문의 게시판은 로그인 필수
  53. if ($post->is_secret) {
  54. if (!$user) {
  55. return $this->deny('로그인 후 이용 해주세요.', 401);
  56. }
  57. if($user->id != $post->user_id) {
  58. return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_2]);
  59. }
  60. }
  61. return true;
  62. }
  63. /**
  64. * Determine whether the user can create models.
  65. *
  66. * @param \App\Models\User $user
  67. * @param \App\Models\Board $board
  68. * @param \App\Models\BoardMeta $boardMeta
  69. * @return \Illuminate\Auth\Access\Response|bool
  70. */
  71. public function create(?User $user, Board $board, BoardMeta $boardMeta)
  72. {
  73. // 게시글 쓰기 권한 확인
  74. $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_3, 0);
  75. if ($permitValue == 1 && !$user) {
  76. return $this->deny('로그인 후 이용 해주세요.', 401);
  77. } else if ($permitValue == 100 && !$user?->isAdministrator()) {
  78. return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_3]);
  79. }
  80. // 1:1 문의 게시판은 로그인 필수
  81. if($boardMeta->item('use_personal', 0)) {
  82. if(!$user) {
  83. return $this->deny('로그인 후 이용 해주세요.', 401);
  84. }
  85. }
  86. // 글 한개만 작성 가능
  87. if ($boardMeta->item('use_only_one_post', 0)) {
  88. if ($this->postService->userPostRows($board, $user) >= 1) {
  89. return $this->deny('이 게시판은 하루에 한 글만 작성 가능합니다.');
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * Determine whether the user can update the model.
  96. *
  97. * @param \App\Models\User $user
  98. * @param \App\Models\Post $post
  99. * @param \App\Models\BoardMeta $boardMeta
  100. * @return \Illuminate\Auth\Access\Response|bool
  101. */
  102. public function update(?User $user, Post $post, BoardMeta $boardMeta)
  103. {
  104. if(!$user && $post->user_id) {
  105. return $this->deny('로그인 후 이용 해주세요.', 401);
  106. }else if(!$post->user_id) {
  107. return true;
  108. }
  109. // 게시글 수정 금지 기간 확인
  110. if(($protectPostDay = $boardMeta->item('protect_post_day', 0)) > 0) {
  111. if((time() - strtotime($post->created_at)) >= ($protectPostDay * 86400)) {
  112. return $this->deny(sprintf('%d일 이상된 게시글은 수정이 불가합니다.', $protectPostDay));
  113. }
  114. }
  115. // 게시글 수정 보호
  116. if($boardMeta->item('protect_update_post', 0) > 0) {
  117. if($post->comment_rows > 0) {
  118. return $this->deny('댓글이 달린 게시글은 수정이 불가합니다.');
  119. }
  120. }
  121. return ($user->id == $post->user_id ? true : $this->deny("권한이 없습니다."));
  122. }
  123. /**
  124. * Determine whether the user can delete the model.
  125. *
  126. * @param \App\Models\User $user
  127. * @param \App\Models\Post $post
  128. * @param \App\Models\BoardMeta $boardMeta
  129. * @return \Illuminate\Auth\Access\Response|bool
  130. */
  131. public function delete(?User $user, Post $post, BoardMeta $boardMeta)
  132. {
  133. if(!$user && $post->user_id) {
  134. return $this->deny('로그인 후 이용 해주세요.', 401);
  135. }else if(!$post->user_id) {
  136. return true;
  137. }
  138. // 게시글 삭제 금지 기간 확인
  139. if(($protectPostDay = $boardMeta->item('protect_post_day', 0)) > 0) {
  140. if((time() - strtotime($post->created_at)) >= ($protectPostDay * 86400)) {
  141. return $this->deny(sprintf('%d일 이상된 게시글은 삭제가 불가합니다.', $protectPostDay));
  142. }
  143. }
  144. // 게시글 삭제 보호
  145. if($boardMeta->item('protect_delete_post', 0) > 0) {
  146. if($post->comment_rows > 0) {
  147. return $this->deny('댓글이 달린 게시글은 삭제가 불가 합니다.');
  148. }
  149. }
  150. return ($user->id == $post->user_id ? true : $this->deny("권한이 없습니다."));
  151. }
  152. /**
  153. * Determine whether the user can restore the model.
  154. *
  155. * @param \App\Models\User $user
  156. * @param \App\Models\Post $post
  157. * @return \Illuminate\Auth\Access\Response|bool
  158. */
  159. public function restore(User $user, Post $post)
  160. {
  161. //
  162. }
  163. /**
  164. * Determine whether the user can permanently delete the model.
  165. *
  166. * @param \App\Models\User $user
  167. * @param \App\Models\Post $post
  168. * @param \App\Models\BoardMeta $boardMeta
  169. * @return \Illuminate\Auth\Access\Response|bool
  170. */
  171. public function forceDelete(?User $user, Post $post, BoardMeta $boardMeta)
  172. {
  173. if($boardMeta->item('block_delete', 0)) {
  174. if(!$user?->is_admin) {
  175. return $this->deny('이 게시글은 관리자만 삭제 가능합니다.');
  176. }
  177. }
  178. return ($user?->id == $post->user_id ? true : $this->deny("권한이 없습니다."));
  179. }
  180. /**
  181. * 이미지 첨부권한 확인
  182. */
  183. public function uploader(?User $user, BoardMeta $boardMeta)
  184. {
  185. $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_6, 0);
  186. if ($permitValue == 1 && !$user) {
  187. return $this->deny('로그인 후 이용 해주세요.', 401);
  188. } else if ($permitValue == 100 && !$user?->isAdministrator()) {
  189. return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_6]);
  190. }
  191. return true;
  192. }
  193. /**
  194. * 파일 다운로드 권한 확인
  195. */
  196. public function download(?User $user, Post $post, BoardMeta $boardMeta)
  197. {
  198. $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_7, 0);
  199. if ($permitValue == 1 && !$user || (!$user && $post->user_id)) {
  200. return $this->deny('로그인 후 이용 해주세요.', 401);
  201. } else if ($permitValue == 100 && !$user?->isAdministrator()) {
  202. return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_7]);
  203. }
  204. // 다운로드 제한 - 댓글 필수
  205. if ($boardMeta->item('need_comment_for_download', 0)) {
  206. if ($this->commentService->userCommentRows($post, $user) <= 0) {
  207. return $this->deny('다운로드를 하려면 댓글이 필요합니다.');
  208. }
  209. }
  210. // 다운로드 제한 - 추천 필수
  211. if ($boardMeta->item('use_post_like', 0)
  212. && $boardMeta->item('need_like_for_download', 0)) {
  213. if (!$this->postService->userPostIsLike($post, $user)) {
  214. return $this->deny('다운로드를 하려면 추천이 필요합니다.');
  215. }
  216. }
  217. return true;
  218. }
  219. }