. */ namespace SP\Forms; use SP\Core\ActionsInterface; use SP\Core\Exceptions\ValidationException; use SP\Core\Session; use SP\DataModel\UserData; use SP\Http\Request; use SP\Mgmt\Users\UserUtil; use SP\Util\Checks; /** * Class UserForm * * @package SP\Forms */ class UserForm extends FormBase implements FormInterface { /** * @var UserData */ protected $UserData; /** * Validar el formulario * * @param $action * @return bool * @throws \SP\Core\Exceptions\ValidationException */ public function validate($action) { switch ($action) { case ActionsInterface::ACTION_USR_USERS_NEW: $this->checkCommon(); $this->checkPass(); break; case ActionsInterface::ACTION_USR_USERS_EDIT: $this->checkCommon(); break; case ActionsInterface::ACTION_USR_USERS_EDITPASS: $this->checkPass(); break; case ActionsInterface::ACTION_USR_USERS_DELETE: $this->checkDelete(); break; } return true; } /** * @throws ValidationException */ protected function checkCommon() { $isLdap = Request::analyze('isLdap', 0); if (!$isLdap && !$this->UserData->getUserName()) { throw new ValidationException(__('Es necesario un nombre de usuario', false)); } elseif (!$isLdap && !$this->UserData->getUserLogin()) { throw new ValidationException(__('Es necesario un login', false)); } elseif (!$this->UserData->getUserProfileId()) { throw new ValidationException(__('Es necesario un perfil', false)); } elseif (!$this->UserData->getUserGroupId()) { throw new ValidationException(__('Es necesario un grupo', false)); } elseif (!$isLdap && !$this->UserData->getUserEmail()) { throw new ValidationException(__('Es necesario un email', false)); } elseif (Checks::demoIsEnabled() && !Session::getUserData()->isUserIsAdminApp() && $this->UserData->getUserLogin() === 'demo') { throw new ValidationException(__('Ey, esto es una DEMO!!', false)); } } /** * @throws ValidationException */ protected function checkPass() { $userPassR = Request::analyzeEncrypted('passR'); if (Checks::demoIsEnabled() && UserUtil::getUserLoginById($this->UserData->getUserId()) === 'demo') { throw new ValidationException(__('Ey, esto es una DEMO!!', false)); } elseif (!$userPassR || !$this->UserData->getUserPass()) { throw new ValidationException(__('La clave no puede estar en blanco', false)); } elseif ($this->UserData->getUserPass() !== $userPassR) { throw new ValidationException(__('Las claves no coinciden', false)); } } /** * @throws ValidationException */ protected function checkDelete() { if (Checks::demoIsEnabled() && UserUtil::getUserLoginById($this->UserData->getUserId()) === 'demo') { throw new ValidationException(__('Ey, esto es una DEMO!!', false)); } elseif ($this->UserData->getUserId() === Session::getUserData()->getUserId()) { throw new ValidationException(__('No es posible eliminar, usuario en uso', false)); } } /** * Analizar los datos de la petición HTTP * * @return void */ protected function analyzeRequestData() { $this->UserData = new UserData(); $this->UserData->setUserId($this->itemId); $this->UserData->setUserName(Request::analyze('name')); $this->UserData->setUserLogin(Request::analyze('login')); $this->UserData->setUserEmail(Request::analyze('email')); $this->UserData->setUserNotes(Request::analyze('notes')); $this->UserData->setUserGroupId(Request::analyze('groupid', 0)); $this->UserData->setUserProfileId(Request::analyze('profileid', 0)); $this->UserData->setUserIsAdminApp(Request::analyze('adminapp', 0, false, 1)); $this->UserData->setUserIsAdminAcc(Request::analyze('adminacc', 0, false, 1)); $this->UserData->setUserIsDisabled(Request::analyze('disabled', 0, false, 1)); $this->UserData->setUserIsChangePass(Request::analyze('changepass', 0, false, 1)); $this->UserData->setUserPass(Request::analyzeEncrypted('pass')); } /** * @return UserData */ public function getItemData() { return $this->UserData; } }