| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App\Policies;
- use Illuminate\Auth\Access\HandlesAuthorization;
- use App\Models\User;
- use App\Models\BoardMeta;
- use App\Models\Comment;
- use App\Services\CommentService;
- class CommentPolicy
- {
- use HandlesAuthorization;
- public CommentService $commentService;
- /**
- * Create a new policy instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->commentService = new CommentService();
- }
- /**
- * Determine whether the user can view any models.
- *
- * @param \App\Models\User $user
- * @param \App\Models\Comment $comment
- * @param \App\Models\BoardMeta $boardMeta
- * @return \Illuminate\Auth\Access\Response|bool
- */
- public function viewAny(?User $user, BoardMeta $boardMeta)
- {
- // 댓글 사용 여부
- if(!$boardMeta->item('use_comment', 0)) {
- return $this->deny('현재 댓글 기능은 사용이 중단 되었습니다.');
- }
- // 댓글 보기 권한 확인
- $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_4);
- 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]);
- }
- return true;
- }
- /**
- * Determine whether the user can view the model.
- *
- * @param \App\Models\User $user
- * @param \App\Models\Comment $comment
- * @return \Illuminate\Auth\Access\Response|bool
- */
- public function view(?User $user, Comment $comment)
- {
- if($comment->is_secret) {
- if(!$user) {
- return false;
- }
- if($user->id != $comment->user_id) {
- return false;
- }
- }
- return true;
- }
- /**
- * Determine whether the user can create models.
- *
- * @param \App\Models\User $user
- * @param \App\Models\BoardMeta $boardMeta
- * @return \Illuminate\Auth\Access\Response|bool
- */
- public function create(?User $user, BoardMeta $boardMeta)
- {
- // 댓글 작성 권한 확인
- $permitValue = $boardMeta->item(BOARD_PERMIT_TYPE_5, 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_2]);
- }
- return true;
- }
- /**
- * Determine whether the user can update the model.
- *
- * @param \App\Models\User $user
- * @param \App\Models\Comment $board
- * @param \App\Models\BoardMeta $boardMeta
- * @return \Illuminate\Auth\Access\Response|bool
- */
- public function update(?User $user, Comment $comment, BoardMeta $boardMeta)
- {
- if(!$user && $comment->user_id) {
- return $this->deny('로그인 후 이용 해주세요.', 401);
- }else if(!$comment->user_id) {
- return true;
- }
- // 댓글 수정 보호
- if($boardMeta->item('protect_update_comment', 0)) {
- // 답글이 달려 있는지 확인
- if($comment->isLeaf() && !$user->is_admin) { // 관리자는 삭제 가능
- return false;
- }
- }
- // 댓글 수정/삭제 금지 기간 확인
- if (($protectCommentDay = $boardMeta->item('protect_comment_day', 0)) > 0) {
- if ((time() - strtotime($comment->created_at)) >= ($protectCommentDay * 86400)) {
- return $this->deny(sprintf('이 게시판은 %d일 이상된 게시글은 수정이 불가합니다.', $protectCommentDay));
- }
- }
- return ($user->id == $comment->user_id ? true : $this->deny("권한이 없습니다."));
- }
- /**
- * Determine whether the user can delete the model.
- *
- * @param \App\Models\User $user
- * @param \App\Models\Comment $board
- * @param \App\Models\BoardMeta $boardMeta
- * @return \Illuminate\Auth\Access\Response|bool
- */
- public function delete(?User $user, Comment $comment, BoardMeta $boardMeta)
- {
- if(!$user && $comment->user_id) {
- return $this->deny('로그인 후 이용 해주세요.', 401);
- }else if(!$comment->user_id) {
- return true;
- }
- // 댓글 삭제 보호
- if($boardMeta->item('protect_delete_comment', 0)) {
- // 답글이 달려 있는지 확인
- if($comment->isLeaf() && !$user->is_admin) { // 관리자는 삭제 가능
- return false;
- }
- }
- return ($user->id == $comment->user_id ? true : $this->deny("권한이 없습니다."));
- }
- /**
- * Determine whether the user can permanently delete the model.
- *
- * @param \App\Models\User $user
- * @param \App\Models\Comment $comment
- * @param \App\Models\BoardMeta $boardMeta
- * @return \Illuminate\Auth\Access\Response|bool
- */
- public function forceDelete(?User $user, Comment $comment, BoardMeta $boardMeta)
- {
- if($boardMeta->item('block_delete', 0)) {
- if(!$user?->is_admin) {
- return $this->deny('이 댓글은 관리자만 삭제 가능합니다.');
- }
- }
- // 댓글 수정/삭제 금지 기간 확인
- if (($protectCommentDay = $boardMeta->item('protect_comment_day', 0)) > 0) {
- if ((time() - strtotime($comment->created_at)) >= ($protectCommentDay * 86400)) {
- return $this->deny(sprintf('이 게시판은 %d일 이상된 댓글은 삭제가 불가합니다.', $protectCommentDay));
- }
- }
- return ($user?->id == $comment->user_id ? true : $this->deny("권한이 없습니다."));
- }
- }
|