. */ namespace SP\Modules\Web\Forms; use SP\Domain\Auth\Models\AuthToken; use SP\Domain\Auth\Services\AuthToken; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Exceptions\ValidationException; /** * Class ApiTokenForm * * @package SP\Modules\Web\Forms */ final class AuthTokenForm extends FormBase implements FormInterface { protected ?AuthToken $authTokenData = null; protected bool $refresh = false; /** * Validar el formulario * * @param int $action * @param int|null $id * * @return AuthTokenForm|FormInterface * @throws ValidationException */ public function validateFor(int $action, ?int $id = null): FormInterface { if ($id !== null) { $this->itemId = $id; } switch ($action) { case AclActionsInterface::AUTHTOKEN_CREATE: case AclActionsInterface::AUTHTOKEN_EDIT: $this->analyzeRequestData(); $this->checkCommon(); break; } return $this; } /** * Analizar los datos de la petición HTTP * * @return void */ protected function analyzeRequestData(): void { $this->refresh = $this->request->analyzeBool('refreshtoken', false); $this->authTokenData = new AuthToken(); $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(): void { 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 (empty($this->authTokenData->getHash()) && (AuthToken::isSecuredAction($this->authTokenData->getActionId()) || $this->isRefresh())) { throw new ValidationException(__u('Password cannot be blank')); } } public function isRefresh(): bool { return $this->refresh; } public function getItemData(): ?AuthToken { return $this->authTokenData; } }