PostUploader.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 Exception;
  8. class PostUploader
  9. {
  10. public function handle(Request $request, Closure $next)
  11. {
  12. try{
  13. $code = $request->route('code');
  14. // 게시판 코드 확인
  15. if (!$code) {
  16. throw new Exception('잘못된 접근입니다.', 404);
  17. }
  18. $boardService = new BoardService();
  19. // 게시판 정보 조회
  20. $board = $boardService->find($code);
  21. // 게시판 존재 유무
  22. if (!$board->exists) {
  23. throw new Exception('존재하지 않는 게시판입니다.');
  24. }
  25. // 게시판 사용 여부
  26. if ($board->is_display == 0) {
  27. throw new Exception('더 이상 사용하지 않는 게시판입니다.');
  28. }
  29. $boardMeta = $boardService->meta($board->id);
  30. // 이미지 첨부 권한 확인
  31. $response = Gate::inspect('uploader', ['App\Model\Post', $boardMeta]);
  32. if($response->denied()) {
  33. throw new Exception($response->message());
  34. }
  35. }catch(Exception $e) {
  36. return alert($e->getMessage(), null, false);
  37. }
  38. return $next($request);
  39. }
  40. }