/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (Article::whereSlug($request->segment(3))->get()->isEmpty()) { return response()->json(['error' => 'Article not found!'], 404); } return $next($request); }
/** * Update comment in database * * @param string $body * @param string $slug * @param integer $id */ public static function editComment($body, $slug, $id) { $article = Article::whereSlug($slug)->first(); $comment = Comment::find($id); $comment->body = $body; $comment->edited = Auth::user()->username; $article->comments()->save($comment); return $article; }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { /** * Check article exsistance. */ if (Article::whereSlug($request->segment(3))->get()->isEmpty()) { return response()->json(['error' => 'Article not found!'], 404); } /** * Check if user is authenticated. */ if (!($token = $this->auth->setRequest($request)->getToken())) { return $this->respond('tymon.jwt.absent', 'token_not_provided', 400); } try { $user = $this->auth->authenticate($token); } catch (TokenExpiredException $e) { return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]); } catch (JWTException $e) { return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]); } if (!$user) { return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404); } $this->events->fire('tymon.jwt.valid', $user); /** * Check if user have admin permissions or this article is user's */ $articleOwner = Article::whereSlug($request->segment(3))->get()[0]->user_id; if ($articleOwner != $user->id) { if ($user->group_id > 1) { return response()->json(['error' => 'You do not have premission, to access this page!'], 403); } } return $next($request); }
/** * Syncing categories * * @param Article $article Article model * @param Array $categories */ private function syncCategories(Article $article, array $categories) { $article->categories()->sync($categories); }