CommentAccess.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Http\Middleware\Board;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Gate;
  6. use App\Services\BoardService;
  7. use App\Services\PostService;
  8. use Exception;
  9. class CommentAccess
  10. {
  11. public function handle(Request $request, Closure $next)
  12. {
  13. try{
  14. $code = $request->route('code');
  15. $postID = $request->route('postID');
  16. // 게시판 코드 확인
  17. if (!$code || !$postID) {
  18. throw new Exception('잘못된 접근입니다.', 404);
  19. }
  20. $boardService = new BoardService();
  21. // 게시판 정보 조회
  22. $board = $boardService->find($code);
  23. // 게시판 존재 유무
  24. if (!$board->exists) {
  25. throw new Exception('존재하지 않는 게시판입니다.');
  26. }
  27. // 게시판 사용 여부
  28. if ($board->is_display == 0) {
  29. throw new Exception('더 이상 사용하지 않는 게시판입니다.');
  30. }
  31. $postService = new PostService();
  32. // 게시글 조회
  33. $post = $postService->postModel->isExists($postID);
  34. // 게시글 존재 유무
  35. if (!$post) {
  36. throw new Exception('존재하지 않는 게시글입니다.');
  37. }
  38. // 게시판 설정 값
  39. $boardMeta = $boardService->meta($board->id);
  40. // 보기 권한 확인
  41. $response = Gate::inspect('viewAny', ['App\Models\Comment', $boardMeta]);
  42. if($response->denied()) {
  43. throw new Exception($response->message());
  44. }
  45. $request->merge([
  46. 'board' => $board,
  47. 'boardMeta' => $boardMeta
  48. ]);
  49. }catch(Exception $e) {
  50. return response(view(layout('board.comment.error'), [
  51. 'message' => $e->getMessage()
  52. ]));
  53. }
  54. return $next($request);
  55. }
  56. }