CommentPolicy.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Policies;
  3. use Illuminate\Auth\Access\HandlesAuthorization;
  4. use App\Models\User;
  5. use App\Models\BoardMeta;
  6. use App\Models\Comment;
  7. use App\Services\CommentService;
  8. class CommentPolicy
  9. {
  10. use HandlesAuthorization;
  11. public CommentService $commentService;
  12. /**
  13. * Create a new policy instance.
  14. *
  15. * @return void
  16. */
  17. public function __construct()
  18. {
  19. $this->commentService = new CommentService();
  20. }
  21. /**
  22. * Determine whether the user can view any models.
  23. *
  24. * @param \App\Models\User $user
  25. * @param \App\Models\Comment $comment
  26. * @param \App\Models\BoardMeta $boardMeta
  27. * @return \Illuminate\Auth\Access\Response|bool
  28. */
  29. public function viewAny(?User $user, BoardMeta $boardMeta)
  30. {
  31. // 댓글 사용 여부
  32. if(!$boardMeta->item('use_comment', 0)) {
  33. return $this->deny('현재 댓글 기능은 사용이 중단 되었습니다.');
  34. }
  35. // 댓글 보기 권한 확인
  36. $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_4);
  37. if ($permitValue == 1 && !$user) {
  38. return $this->deny('로그인 후 이용 해주세요.', 401);
  39. } else if ($permitValue == 100 && !$user?->isAdministrator()) {
  40. return $this->deny(MAP_BOARD_PERMIT_ALERT_TYPE[BOARD_PERMIT_TYPE_2]);
  41. }
  42. return true;
  43. }
  44. /**
  45. * Determine whether the user can view the model.
  46. *
  47. * @param \App\Models\User $user
  48. * @param \App\Models\Comment $comment
  49. * @return \Illuminate\Auth\Access\Response|bool
  50. */
  51. public function view(?User $user, Comment $comment)
  52. {
  53. if($comment->is_secret) {
  54. if(!$user) {
  55. return false;
  56. }
  57. if($user->id != $comment->user_id) {
  58. return false;
  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\BoardMeta $boardMeta
  68. * @return \Illuminate\Auth\Access\Response|bool
  69. */
  70. public function create(?User $user, BoardMeta $boardMeta)
  71. {
  72. // 댓글 작성 권한 확인
  73. $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_5, 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_2]);
  78. }
  79. return true;
  80. }
  81. /**
  82. * Determine whether the user can update the model.
  83. *
  84. * @param \App\Models\User $user
  85. * @param \App\Models\Comment $board
  86. * @param \App\Models\BoardMeta $boardMeta
  87. * @return \Illuminate\Auth\Access\Response|bool
  88. */
  89. public function update(?User $user, Comment $comment, BoardMeta $boardMeta)
  90. {
  91. if(!$user && $comment->user_id) {
  92. return $this->deny('로그인 후 이용 해주세요.', 401);
  93. }else if(!$comment->user_id) {
  94. return true;
  95. }
  96. // 댓글 수정 보호
  97. if($boardMeta->item('protect_update_comment', 0)) {
  98. // 답글이 달려 있는지 확인
  99. if($comment->isLeaf() && !$user->is_admin) { // 관리자는 삭제 가능
  100. return false;
  101. }
  102. }
  103. // 댓글 수정/삭제 금지 기간 확인
  104. if (($protectCommentDay = $boardMeta->item('protect_comment_day', 0)) > 0) {
  105. if ((time() - strtotime($comment->created_at)) >= ($protectCommentDay * 86400)) {
  106. return $this->deny(sprintf('이 게시판은 %d일 이상된 게시글은 수정이 불가합니다.', $protectCommentDay));
  107. }
  108. }
  109. return ($user->id == $comment->user_id ? true : $this->deny("권한이 없습니다."));
  110. }
  111. /**
  112. * Determine whether the user can delete the model.
  113. *
  114. * @param \App\Models\User $user
  115. * @param \App\Models\Comment $board
  116. * @param \App\Models\BoardMeta $boardMeta
  117. * @return \Illuminate\Auth\Access\Response|bool
  118. */
  119. public function delete(?User $user, Comment $comment, BoardMeta $boardMeta)
  120. {
  121. if(!$user && $comment->user_id) {
  122. return $this->deny('로그인 후 이용 해주세요.', 401);
  123. }else if(!$comment->user_id) {
  124. return true;
  125. }
  126. // 댓글 삭제 보호
  127. if($boardMeta->item('protect_delete_comment', 0)) {
  128. // 답글이 달려 있는지 확인
  129. if($comment->isLeaf() && !$user->is_admin) { // 관리자는 삭제 가능
  130. return false;
  131. }
  132. }
  133. return ($user->id == $comment->user_id ? true : $this->deny("권한이 없습니다."));
  134. }
  135. /**
  136. * Determine whether the user can permanently delete the model.
  137. *
  138. * @param \App\Models\User $user
  139. * @param \App\Models\Comment $comment
  140. * @param \App\Models\BoardMeta $boardMeta
  141. * @return \Illuminate\Auth\Access\Response|bool
  142. */
  143. public function forceDelete(?User $user, Comment $comment, BoardMeta $boardMeta)
  144. {
  145. if($boardMeta->item('block_delete', 0)) {
  146. if(!$user?->is_admin) {
  147. return $this->deny('이 댓글은 관리자만 삭제 가능합니다.');
  148. }
  149. }
  150. // 댓글 수정/삭제 금지 기간 확인
  151. if (($protectCommentDay = $boardMeta->item('protect_comment_day', 0)) > 0) {
  152. if ((time() - strtotime($comment->created_at)) >= ($protectCommentDay * 86400)) {
  153. return $this->deny(sprintf('이 게시판은 %d일 이상된 댓글은 삭제가 불가합니다.', $protectCommentDay));
  154. }
  155. }
  156. return ($user?->id == $comment->user_id ? true : $this->deny("권한이 없습니다."));
  157. }
  158. }