. */ namespace SP\Modules\Web\Forms; use SP\Core\Acl\ActionsInterface; use SP\Core\Exceptions\ValidationException; use SP\DataModel\AuthTokenData; use SP\Services\AuthToken\AuthTokenService; /** * Class ApiTokenForm * * @package SP\Modules\Web\Forms */ final class AuthTokenForm extends FormBase implements FormInterface { /** * @var AuthTokenData */ protected $authTokenData; /** * @var bool */ protected $refresh = false; /** * Validar el formulario * * @param $action * * @return AuthTokenForm * @throws ValidationException */ public function validate($action) { switch ($action) { case ActionsInterface::AUTHTOKEN_CREATE: case ActionsInterface::AUTHTOKEN_EDIT: $this->analyzeRequestData(); $this->checkCommon(); break; } return $this; } /** * Analizar los datos de la petición HTTP * * @return void */ protected function analyzeRequestData() { $this->refresh = $this->request->analyzeBool('refreshtoken', false); $this->authTokenData = new AuthTokenData(); $this->authTokenData->setId($this->itemId); $this->authTokenData->setUserId($this->request->analyzeInt('users')); $this->authTokenData->setActionId($this->request->analyzeInt('actions')); $this->authTokenData->setHash($this->request->analyzeEncrypted('pass')); } /** * @throws ValidationException */ protected function checkCommon() { if (0 === $this->authTokenData->getUserId()) { throw new ValidationException(__u('User not set')); } if (0 === $this->authTokenData->getActionId()) { throw new ValidationException(__u('Action not set')); } if ((AuthTokenService::isSecuredAction($this->authTokenData->getActionId()) || $this->isRefresh()) && empty($this->authTokenData->getHash()) ) { throw new ValidationException(__u('Password cannot be blank')); } } /** * @return bool */ public function isRefresh() { return $this->refresh; } /** * @return AuthTokenData */ public function getItemData() { return $this->authTokenData; } }