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