Esempio n. 1
0
 /**
  * {@inheritDoc}
  */
 public function handle($request, Closure $next)
 {
     if (is_api_request()) {
         return $next($request);
     }
     return parent::handle($request, $next);
 }
Esempio n. 2
0
 /**
  * Render an exception into an HTTP response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $e
  * @return \Illuminate\Http\Response
  */
 public function render($request, Exception $e)
 {
     if (app()->environment('production')) {
         $title = 'Error';
         $description = 'Unknown error occurred :(';
         $statusCode = 400;
         if ($e instanceof ModelNotFoundException or $e instanceof NotFoundHttpException) {
             $title = trans('errors.not_found');
             $description = trans('errors.not_found_description');
             $statusCode = 404;
         }
         return response(view('errors.notice', ['title' => $title, 'description' => $description]), $e->getCode() ?: $statusCode);
     }
     if (is_api_request()) {
         $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : $e->getCode();
         if ($e instanceof TokenExpiredException) {
             $message = 'token_expired';
         } elseif ($e instanceof TokenInvalidException) {
             $message = 'token_invalid';
         } elseif ($e instanceof JWTException) {
             $message = $e->getMessage() ?: 'could_not_create_token';
         } elseif ($e instanceof NotFoundHttpException or $e instanceof ModelNotFoundException) {
             $statusCode = 404;
             $message = $e->getMessage() ?: 'not_found';
         } elseif ($e instanceof MethodNotAllowedHttpException) {
             $message = $e->getMessage() ?: 'not_allowed';
         } elseif ($e instanceof HttpResponseException) {
             return $e->getResponse();
         } elseif ($e instanceof Exception) {
             $message = $e->getMessage() ?: 'Whoops~ Tell me what you did :(';
         }
         return json()->setStatusCode($statusCode ?: 400)->error($message);
     }
     return parent::render($request, $e);
 }
Esempio n. 3
0
 /**
  * {@inheritDoc}
  */
 protected function failedAuthorization()
 {
     if (is_api_request()) {
         return app(Response::class)->unauthorizedError();
     }
     return parent::failedAuthorization();
 }
Esempio n. 4
0
 /**
  * {@inheritDoc}
  */
 public function forbiddenResponse()
 {
     if (is_api_request()) {
         return json()->forbiddenError();
     }
     return response('Forbidden', 403);
 }
Esempio n. 5
0
 /**
  * {@inheritDoc}
  */
 public function forbiddenResponse()
 {
     if (is_api_request()) {
         return app(Response::class)->forbiddenError();
     }
     return response('Forbidden', 403);
 }
Esempio n. 6
0
 /**
  * {@inheritDoc}
  */
 public function handle($request, Closure $next)
 {
     if (is_api_request()) {
         return $this->addCookieToResponse($request, $next($request));
     }
     return parent::handle($request, $next);
 }
Esempio n. 7
0
 /**
  * Create a new authentication controller instance.
  *
  * @param UserRepository $repo
  * @param Guard          $auth
  */
 public function __construct(UserRepository $repo, Guard $auth)
 {
     $this->repo = $repo;
     $this->auth = $auth;
     if (!is_api_request()) {
         $this->middleware('guest', ['except' => ['getLogout']]);
     }
     parent::__construct();
 }
Esempio n. 8
0
 /**
  * Constructor
  */
 public function __construct()
 {
     if (!is_api_request()) {
         $this->setSharedVariables();
     }
     $this->cache = app('cache');
     if ((new \ReflectionClass($this))->implementsInterface(\App\Http\Controllers\Cacheable::class) and taggable()) {
         $this->cache = app('cache')->tags($this->cacheKeys());
     }
 }
Esempio n. 9
0
 /**
  * Handle login request to the application.
  *
  * @param \Illuminate\Http\Request $request
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function store(Request $request)
 {
     $validator = \Validator::make($request->all(), ['email' => 'required|email', 'password' => 'required|min:6']);
     if ($validator->fails()) {
         return $this->respondValidationError($validator);
     }
     $token = is_api_request() ? \JWTAuth::attempt($request->only('email', 'password')) : Auth::attempt($request->only('email', 'password'), $request->has('remember'));
     if (!$token) {
         return $this->respondLoginFailed();
     }
     event('users.login', [Auth::user()]);
     return $this->respondCreated($request->input('return'), $token);
 }
Esempio n. 10
0
 /**
  * Create a new authentication controller instance.
  *
  * @param UserRepository $repo
  * @param Guard          $auth
  */
 public function __construct(UserRepository $repo, Guard $auth)
 {
     $this->repo = $repo;
     $this->auth = $auth;
     if (!is_api_request()) {
         $this->middleware('auth', ['only' => ['destroy']]);
         $this->middleware('guest', ['except' => ['destroy']]);
     } else {
         $this->middleware('jwt.auth', ['only' => ['destroy']]);
         $this->middleware('jwt.refresh', ['only' => ['destroy']]);
     }
     parent::__construct();
 }
Esempio n. 11
0
 /**
  * Handle an incoming request.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure                 $next
  * @param string|null              $param
  * @return mixed
  */
 public function handle(Request $request, Closure $next, $param = null)
 {
     $user = $request->user();
     $model = '\\App\\' . ucfirst($param);
     $modelId = $request->route($param ? str_plural($param) : 'id');
     if (!$model::whereId($modelId)->whereAuthorId($user->id)->exists() and !$user->isAdmin()) {
         if (is_api_request()) {
             return json()->forbiddenError();
         }
         flash()->error(trans('errors.forbidden') . ' : ' . trans('errors.forbidden_description'));
         return back();
     }
     return $next($request);
 }
Esempio n. 12
0
 /**
  * Render an exception into an HTTP response.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Exception  $e
  * @return \Illuminate\Http\Response
  */
 public function render($request, Exception $e)
 {
     if (is_api_request()) {
         if ($e instanceof ModelNotFoundException or $e instanceof NotFoundHttpException) {
             return app('api.response')->notFoundError($e->getMessage() ?: trans('errors.resourceNotFound'));
         }
         if ($e instanceof MethodNotAllowedException or $e instanceof MethodNotAllowedHttpException) {
             return app('api.response')->setStatusCode(405)->error($e->getMessage() ?: trans('errors.notExistingEndpoint'));
         }
         if ($e instanceof JWTException) {
             return app('api.response')->setStatusCode($e->getStatusCode())->error($e->getMessage());
         }
         if ($e instanceof Exception) {
             return app('api.response')->error($e);
         }
     }
     if ($e instanceof ModelNotFoundException or $e instanceof NotFoundHttpException or $e instanceof MethodNotAllowedException or $e instanceof MethodNotAllowedHttpException) {
         return response(view('layouts.notice', ['title' => $e->getMessage() ?: trans('messages.whoops'), 'description' => trans('errors.resourceNotFound')]), 404);
     }
     return parent::render($request, $e);
 }
Esempio n. 13
0
 /**
  * Display the specified resource.
  *
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function show($id)
 {
     $cacheKey = cache_key("articles.show.{$id}");
     $secondKey = cache_key("articles.show.{$id}.comments");
     $query = Article::with('comments', 'tags', 'attachments', 'solution')->findOrFail($id);
     $article = $this->cache($cacheKey, 5, $query, 'findOrFail', $id);
     $secondQuery = $article->comments()->with('replies')->withTrashed()->whereNull('parent_id')->latest();
     $commentsCollection = $this->cache($secondKey, 5, $secondQuery, 'get');
     if (!is_api_request()) {
         event(new ArticleConsumed($article));
     }
     return $this->respondItem($article, $commentsCollection, $cacheKey . $secondKey);
 }
Esempio n. 14
0
 /**
  * Constructor
  */
 public function __construct()
 {
     if (!is_api_request()) {
         $this->setSharedVariables();
     }
 }