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.'); } }
/** * Presenter startup * * @throws BadRequestException */ protected function startup() { parent::startup(); $this->autoCanonicalize = FALSE; try { // Create resource object $this->resource = $this->resourceFactory->create(); // calls $this->validate<Action>() $validationProcessed = $this->tryCall($this->formatValidateMethod($this->action), $this->params); // Check if input is validate if (!$this->getInput()->isValid() && $validationProcessed === TRUE) { $errors = $this->getInput()->validate(); throw BadRequestException::unprocessableEntity($errors, 'Validation Failed: ' . $errors[0]->message); } } catch (BadRequestException $e) { if ($e->getCode() === 422) { $this->sendErrorResource($e); return; } throw $e; } catch (InvalidStateException $e) { $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); }