public function my() { if (!($userId = $this->getUserId())) { return ApiResponse::forbidden(); } return ApiResponse::responseData($this->repository->my()); }
public function create($data) { if (!($resource = $this->repository->create($data))) { return ApiResponse::badRequest(trans('messages.resource.create.failed', ['resource' => $this->repository->resource])); } return ApiResponse::responseData($resource, trans('messages.resource.create.success', ['resource' => $this->repository->resource])); }
public function delete($id) { $resource = $this->repository->findOrThrowResourceNotFoundException($id); if (!$resource->delete()) { return ApiResponse::badRequest(trans('messages.resource.delete.failed', ['resource' => $this->repository->resource])); } return ApiResponse::responseOK(trans('messages.resource.delete.success', ['resource' => $this->repository->resource])); }
public function update($id, $data) { $data = $this->parseRequestData($data); $role = $this->repository->findOrThrowResourceNotFoundException($id); if (!($role = $this->repository->update($role, $data))) { return ApiResponse::badRequest(trans('messages.resource.update.failed', ['resource' => $this->repository->resource])); } return ApiResponse::responseData($role, trans('messages.resource.update.success', ['resource' => $this->repository->resource])); }
public function reset($data) { $user = $this->getUserByToken($data['token']); if (!$user->update(['password' => $data['password']])) { return ApiResponse::badRequest(trans('messages.resource.update.failed', ['resource' => $this->repository->resource])); } $this->clearToken($user['email']); return ApiResponse::responseOK(trans('messages.resource.update.password_success')); }
public static function groupByDateYM(&$resources, $filter, $inData = true) { if ($filter['group_by'] == 'year_month') { $ApiResponse = new ApiResponse(); $i = 0; foreach ($resources->groupBy('year_month') as $yearMonth => $resource) { $humanYearMonth = Carbon::parse($yearMonth)->format('F, Y'); $ApiResponse->appendData($i, ['human_year_month' => $humanYearMonth, 'data' => $resource, 'year_month' => $yearMonth, 'data' => $resource]); $i += 1; } return $ApiResponse->response(); } if ($inData) { return ApiResponse::responseData($resources); } $ApiResponse = new ApiResponse(); return $ApiResponse->setResponse($resources)->response(); }
public function update($id, $data) { $resource = $this->repository->find($id); if (!$resource) { throw new ResourceNotFoundException(trans_choice('messages.resource.empty', 1, ['resource' => $this->repository->resource])); } if (!$resource->update($data)) { return ApiResponse::badRequest(trans('messages.resource.update.failed', ['resource' => $this->repository->resource])); } return ApiResponse::responseData($resource, trans('messages.resource.update.success', ['resource' => $this->repository->resource])); }
/** * 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 ($e instanceof ResourceNotFoundException) { return ApiResponse::resourceNotFound(trans_choice('messages.resource.empty', 1, ['resource' => $e->resource])); } elseif ($e instanceof NoResourceFoundException) { return ApiResponse::resourceNotFound(trans_choice('messages.resource.empty', 2, ['resource' => $e->resource])); } elseif ($e instanceof ForbiddenException) { return ApiResponse::forbidden(trans('messages.forbidden')); } return parent::render($request, $e); }
public function updatePassword($data) { $user = $this->getUser(); $oldPassword = $user->password; if (!\Hash::check($data['old_password'], $user->password)) { return ApiResponse::badRequest(trans('messages.old_password_not_match')); } if (!$user->update($data)) { return ApiResponse::badRequest(trans('messages.resource.update.password_failed')); } return ApiResponse::responseOK(trans('messages.resource.update.password_success')); }
public function authenticate($request) { $credentials = $request->only('email', 'password'); $apiResponse = new ApiResponse(); if ($lockedOut = $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); $msg = $this->getLockoutErrorMessage($this->secondsRemainingOnLockout($request)); return $apiResponse->appendError('auth', $msg)->statusBadRequest()->setMessage($msg)->response(); } try { if (!($token = JWTAuth::attempt($credentials))) { $this->incrementLoginAttempts($request); return $apiResponse->appendError('auth', trans('user::errors.authenticate'))->statusUnauthorized()->setMessage(trans('user::errors.authenticate'))->response(); } } catch (JWTException $e) { return $apiResponse->appendError('auth', trans('user::errors.could_not_create_token'))->statusInternalServerError()->setMessage(trans('user::errors.could_not_create_token'))->response(); } $modules = $this->module->my(); $permissions = $this->module->permissions(); return $apiResponse->setData(Auth::user()->load('group'))->append('token', $token)->appendData('modules', $modules)->appendData('modules_flat', $modules->pluck('slug'))->appendData('permissions', $permissions)->appendData('permissions_flat', $permissions->pluck('slug'))->response(); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { try { if (!($user = JWTAuth::parseToken()->authenticate())) { return ApiResponse::forbidden(trans('user::errors.invalid_token')); } } catch (JWTException $e) { if ($e instanceof TokenExpiredException) { return ApiResponse::forbidden(trans('user::errors.expired_token')); } else { return ApiResponse::forbidden(trans('user::errors.invalid_token')); } return $apiResponse->response(); } return $next($request); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { try { if (!($user = JWTAuth::parseToken()->authenticate())) { return ApiResponse::forbidden(trans('user::errors.invalid_token')); } if (!$user->can($request->route()->getName(), $this->router->current()->parameters())) { return ApiResponse::forbidden('Access denied.'); } } catch (JWTException $e) { if ($e instanceof TokenExpiredException) { return ApiResponse::forbidden(trans('user::errors.expired_token')); } else { return ApiResponse::forbidden(trans('user::errors.invalid_token')); } return $apiResponse->response(); } return $next($request); }
public function all() { $resources = $this->repository->allOrThrowNoResourceFoundException(); return ApiResponse::responseData($resources); }
public function get($id) { $resource = $this->repository->findOrThrowResourceNotFoundException($id); return ApiResponse::responseData($resource); }