private function validateNewPassword($password) { try { Security\Passwords::validateNew($password); return true; } catch (Nette\UnexpectedValueException $ex) { throw BadRequestException::unprocessableEntity(array($ex->getMessage), 'Bad format of new password.'); } }
/** * Check allowed methods * * @throws BadRequestException If method is not supported but another one can be used */ protected function checkAllowedMethods() { $availableMethods = $this->methods->getOptions($this->request->getUrl()); if (!$availableMethods || in_array($this->request->method, $availableMethods)) { return; } $allow = implode(', ', $availableMethods); $this->response->setHeader('Allow', $allow); throw BadRequestException::methodNotSupported('Method not supported. Available methods: ' . $allow); }
public function startup() { if ($this->getAction() !== 'read') { throw BadRequestException::forbidden('Stat presenter handles GET requests ONLY.'); } ResourcePresenter::startup(); $this->series = $this->getParameter('series'); $this->data = $this->getParameter('data'); $this->dateBegin = $this->getParameter('dateBegin'); $this->dateEnd = $this->getParameter('dateEnd'); }
/** * Parse request body if any * @return array|\Traversable * * @throws BadRequestException */ protected function parseRequestBody() { $requestBody = array(); $input = class_exists('Nette\\Framework') && Nette\Framework::VERSION_ID <= 20200 ? file_get_contents('php://input') : $this->httpRequest->getRawBody(); if ($input) { try { $this->mapper = $this->mapperContext->getMapper($this->httpRequest->getHeader('Content-Type')); $requestBody = $this->mapper->parse($input); } catch (InvalidStateException $e) { throw BadRequestException::unsupportedMediaType('No mapper defined for Content-Type ' . $this->httpRequest->getHeader('Content-Type'), $e); } catch (MappingException $e) { throw new BadRequestException($e->getMessage(), 400, $e); } } return $requestBody; }
/** * Send error resource to output * @param \Exception $e */ protected function sendErrorResource(\Exception $e, $contentType = NULL) { /** @var Request $request */ $request = $this->getHttpRequest(); $this->resource = $this->createErrorResource($e); // if the $contentType is not forced and the user has requested an unacceptable content-type, default to JSON $accept = $request->getHeader('Accept'); if ($contentType === NULL && (!$accept || !$this->responseFactory->isAcceptable($accept))) { $contentType = IResource::JSON; } try { $response = $this->responseFactory->create($this->resource, $contentType); $response = new ErrorResponse($response, $e->getCode() > 99 && $e->getCode() < 600 ? $e->getCode() : 400); $this->sendResponse($response); } catch (InvalidStateException $e) { $this->sendErrorResource(BadRequestException::unsupportedMediaType($e->getMessage(), $e), $contentType); } }
public function actionDelete($id) { $e = BadRequestException::methodNotSupported('Currently not supported'); $this->sendErrorResource($e); }
/** * Returns single database record as associative array * @param int $id * @return array item * @throws Drahak\Restful\Application\BadRequestException */ protected function getItem($id) { if (($row = $this->table->get($id)) === FALSE) { throw BadRequestException::notFound('No record for ID: ' . $id); } $item = $row->toArray(); if ($this->deepListing) { $this->getDeepData($item, $row, $this->deepListing); } if (count($this->metadata) > 0) { $item['metadata'] = $this->metadata; } return $item; }
public function actionDelete($id) { $e = BadRequestException::methodNotSupported('Tap cannot be deleted'); $this->sendErrorResource($e); }
public function actionUpdateTap($id, $relationId) { $tap = $this->db->table('tap')->get($relationId); $keg = $this->db->table('keg')->get($id); $currentState = $keg->state; $newState = $this->inputData['state']; $errors = []; try { // db transaction - no db changes will be stored if error occurs $this->db->beginTransaction(); switch ($keg->state) { case self::KEG_STATE_STOCKED: if ($newState != self::KEG_STATE_TAPPED) { $errors[] = 'New keg can only be tapped, not finished'; } } // check proper tap<->barrel relation if ($keg->state === self::KEG_STATE_STOCKED) { if ($tap->keg !== NULL) { $errors[] = 'Tap already in use.'; } else { if ($this->inputData['state'] === self::KEG_STATE_FINISHED) { $errors[] = 'Cannot finish untapped barrel.'; } } } else { if ($tap->keg != $id) { $errors[] = 'This keg is not assigned to this tap.'; } if ($keg->state === self::KEG_STATE_FINISHED) { $errors[] = 'Cannot change state of finished keg.'; } } if ($keg->state === $this->inputData['state']) { $errors[] = 'No change in state. Other values cannot be modified'; } if (count($errors) > 0) { throw BadRequestException::unprocessableEntity($errors, 'Invalid change in state'); } // currently only keg.state and datetime data can be updated $dataKeg = array('state' => $this->inputData['state']); $dataTap = array('keg' => NULL); switch ($this->inputData['state']) { case self::KEG_STATE_TAPPED: $dataTap['keg'] = $id; if ($keg->date_tap === NULL) { $dataKeg['date_tap'] = new Nette\Utils\DateTime(empty($this->inputData['date_tap']) ? NULL : $this->inputData['date_tap']); } break; case self::KEG_STATE_FINISHED: $dataKeg['date_end'] = new Nette\Utils\DateTime(empty($this->inputData['date_end']) ? NULL : $this->inputData['date_end']); $this->finishAndAccount($keg, $dataKeg['date_end']); } if (count($errors) > 0) { throw BadRequestException::unprocessableEntity($errors, 'Invalid Keg to Tap relation.'); } $keg->update($dataKeg); $tap->update($dataTap); $this->db->commit(); } catch (BadRequestException $ex) { $this->db->rollBack(); $this->sendErrorResource($ex); } $this->resource = $keg->toArray(); $this->getDeepData($this->resource, $keg, $this->listing); $this->sendResource(IResource::JSON); }