. */ namespace SP\Modules\Web\Forms; use SP\Core\Acl\ActionsInterface; use SP\Core\Exceptions\ValidationException; use SP\DataModel\AuthTokenData; use SP\Http\Request; /** * Class ApiTokenForm * * @package SP\Modules\Web\Forms */ class AuthTokenForm extends FormBase implements FormInterface { /** * @var AuthTokenData */ protected $authTokenData; /** * @var bool */ protected $refresh = false; /** * Validar el formulario * * @param $action * @return AuthTokenForm * @throws \SP\Core\Exceptions\ValidationException */ public function validate($action) { switch ($action) { case ActionsInterface::APITOKEN_CREATE: case ActionsInterface::APITOKEN_EDIT: $this->analyzeRequestData(); $this->checkCommon(); break; } return $this; } /** * Analizar los datos de la petición HTTP * * @return void */ protected function analyzeRequestData() { $this->refresh = Request::analyzeBool('refreshtoken', false); $this->authTokenData = new AuthTokenData(); $this->authTokenData->setId($this->itemId); $this->authTokenData->setUserId(Request::analyzeInt('users')); $this->authTokenData->setActionId(Request::analyzeInt('actions')); $this->authTokenData->setHash(Request::analyzeEncrypted('pass')); } /** * @throws ValidationException */ protected function checkCommon() { if ($this->authTokenData->getUserId() === 0) { throw new ValidationException(__u('Usuario no indicado')); } if ($this->authTokenData->getActionId() === 0) { throw new ValidationException(__u('Acción no indicada')); } $action = $this->authTokenData->getActionId(); if (($action === ActionsInterface::ACCOUNT_VIEW_PASS || $action === ActionsInterface::ACCOUNT_CREATE || $this->isRefresh()) && $this->authTokenData->getHash() === '' ) { throw new ValidationException(__u('La clave no puede estar en blanco')); } } /** * @return bool */ public function isRefresh() { return $this->refresh; } /** * @return AuthTokenData */ public function getItemData() { return $this->authTokenData; } }