use of org.akaza.openclinica.control.form.Validator in project OpenClinica by OpenClinica.
the class EditUserAccountServlet method processRequest.
@Override
protected void processRequest() throws Exception {
FormProcessor fp = new FormProcessor(request);
// because we need to use this in the confirmation and error parts too
ArrayList studies = getAllStudies();
request.setAttribute("studies", studies);
int userId = fp.getInt(ARG_USERID);
UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
UserAccountBean user = (UserAccountBean) udao.findByPK(userId);
int stepNum = fp.getInt(ARG_STEPNUM);
if (!fp.isSubmitted()) {
addEntityList("userTypes", getUserTypes(), respage.getString("the_user_could_not_be_edited_because_no_user_types"), Page.ADMIN_SYSTEM);
loadPresetValuesFromBean(fp, user);
fp.addPresetValue(ARG_STEPNUM, EDIT_STEP);
setPresetValues(fp.getPresetValues());
// addEntityList("userTypes", getUserTypes(),
// "The user could not be edited because there are no user types
// available.",
// Page.ADMIN_SYSTEM);
request.setAttribute("userName", user.getName());
forwardPage(Page.EDIT_ACCOUNT);
} else if (stepNum == EDIT_STEP) {
Validator v = new Validator(request);
v.addValidation(INPUT_FIRST_NAME, Validator.NO_BLANKS);
v.addValidation(INPUT_LAST_NAME, Validator.NO_BLANKS);
v.addValidation(INPUT_FIRST_NAME, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 50);
v.addValidation(INPUT_LAST_NAME, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 50);
v.addValidation(INPUT_EMAIL, Validator.NO_BLANKS);
v.addValidation(INPUT_EMAIL, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 120);
v.addValidation(INPUT_EMAIL, Validator.IS_A_EMAIL);
v.addValidation(INPUT_INSTITUTION, Validator.NO_BLANKS);
v.addValidation(INPUT_INSTITUTION, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 255);
HashMap errors = v.validate();
if (errors.isEmpty()) {
loadPresetValuesFromForm(fp);
fp.addPresetValue(ARG_STEPNUM, CONFIRM_STEP);
setPresetValues(fp.getPresetValues());
request.setAttribute("userName", user.getName());
forwardPage(Page.EDIT_ACCOUNT_CONFIRM);
} else {
loadPresetValuesFromForm(fp);
fp.addPresetValue(ARG_STEPNUM, EDIT_STEP);
setInputMessages(errors);
setPresetValues(fp.getPresetValues());
addEntityList("userTypes", getUserTypes(), respage.getString("the_user_could_not_be_edited_because_no_user_types"), Page.ADMIN_SYSTEM);
addPageMessage(respage.getString("there_were_some_errors_submission") + respage.getString("see_below_for_details"));
forwardPage(Page.EDIT_ACCOUNT);
}
} else if (stepNum == CONFIRM_STEP) {
String button = fp.getString(INPUT_CONFIRM_BUTTON);
if (button.equals(resword.getString("back"))) {
loadPresetValuesFromForm(fp);
fp.addPresetValue(ARG_STEPNUM, EDIT_STEP);
addEntityList("userTypes", getUserTypes(), respage.getString("the_user_could_not_be_edited_because_no_user_types"), Page.ADMIN_SYSTEM);
setPresetValues(fp.getPresetValues());
request.setAttribute("userName", user.getName());
forwardPage(Page.EDIT_ACCOUNT);
} else if (button.equals(resword.getString("confirm"))) {
user.setFirstName(fp.getString(INPUT_FIRST_NAME));
user.setLastName(fp.getString(INPUT_LAST_NAME));
user.setEmail(fp.getString(INPUT_EMAIL));
user.setInstitutionalAffiliation(fp.getString(INPUT_INSTITUTION));
user.setUpdater(ub);
user.setRunWebservices(fp.getBoolean(INPUT_RUN_WEBSERVICES));
user.setEnableApiKey(true);
String apiKey = null;
do {
apiKey = getRandom32ChApiKey();
} while (isApiKeyExist(apiKey));
user.setApiKey(apiKey);
UserType ut = UserType.get(fp.getInt(INPUT_USER_TYPE));
if (ut.equals(UserType.SYSADMIN)) {
user.addUserType(ut);
} else if (ut.equals(UserType.TECHADMIN)) {
user.addUserType(ut);
} else {
user.addUserType(UserType.USER);
}
if (fp.getBoolean(INPUT_RESET_PASSWORD)) {
SecurityManager sm = ((SecurityManager) SpringServletAccess.getApplicationContext(context).getBean("securityManager"));
String password = sm.genPassword();
String passwordHash = sm.encrytPassword(password, getUserDetails());
user.setPasswd(passwordHash);
user.setPasswdTimestamp(null);
udao.update(user);
if ("no".equalsIgnoreCase(fp.getString(INPUT_DISPLAY_PWD))) {
logger.info("displayPwd is no");
try {
sendResetPasswordEmail(user, password);
} catch (Exception e) {
addPageMessage(respage.getString("there_was_an_error_sending_reset_email_try_reset"));
}
} else {
addPageMessage(respage.getString("new_user_password") + ":<br/> " + password + "<br/>" + respage.getString("please_write_down_the_password_and_provide"));
}
} else {
udao.update(user);
}
addPageMessage(respage.getString("the_user_account") + " \"" + user.getName() + "\" " + respage.getString("was_updated_succesfully"));
forwardPage(Page.LIST_USER_ACCOUNTS_SERVLET);
} else {
throw new InconsistentStateException(Page.ADMIN_SYSTEM, resexception.getString("an_invalid_submit_button_was_clicked"));
}
} else {
throw new InconsistentStateException(Page.ADMIN_SYSTEM, resexception.getString("an_invalid_step_was_specified"));
}
}
use of org.akaza.openclinica.control.form.Validator in project OpenClinica by OpenClinica.
the class CreateStudyServlet method confirmStudy1.
/**
* Validates the first section of study and save it into study bean
*
* @param request
* @param response
* @throws Exception
*/
private void confirmStudy1() throws Exception {
Validator v = new Validator(request);
FormProcessor fp = new FormProcessor(request);
v.addValidation("name", Validator.NO_BLANKS);
v.addValidation("uniqueProId", Validator.NO_BLANKS);
v.addValidation("description", Validator.NO_BLANKS);
v.addValidation("prinInvestigator", Validator.NO_BLANKS);
v.addValidation("sponsor", Validator.NO_BLANKS);
v.addValidation("secondProId", Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 255);
v.addValidation("collaborators", Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 1000);
v.addValidation("protocolDescription", Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 1000);
errors = v.validate();
// check to see if name and uniqueProId are unique, tbh
StudyDAO studyDAO = new StudyDAO(sm.getDataSource());
ArrayList<StudyBean> allStudies = (ArrayList<StudyBean>) studyDAO.findAll();
for (StudyBean thisBean : allStudies) {
if (fp.getString("name").trim().equals(thisBean.getName())) {
MessageFormat mf = new MessageFormat("");
mf.applyPattern(respage.getString("brief_title_existed"));
Object[] arguments = { fp.getString("name").trim() };
Validator.addError(errors, "name", mf.format(arguments));
}
if (fp.getString("uniqueProId").trim().equals(thisBean.getIdentifier())) {
Validator.addError(errors, "uniqueProId", resexception.getString("unique_protocol_id_existed"));
}
}
if (fp.getString("name").trim().length() > 100) {
Validator.addError(errors, "name", resexception.getString("maximum_lenght_name_100"));
}
if (fp.getString("uniqueProId").trim().length() > 30) {
Validator.addError(errors, "uniqueProId", resexception.getString("maximum_lenght_unique_protocol_30"));
}
if (fp.getString("description").trim().length() > 255) {
Validator.addError(errors, "description", resexception.getString("maximum_lenght_brief_summary_255"));
}
if (fp.getString("prinInvestigator").trim().length() > 255) {
Validator.addError(errors, "prinInvestigator", resexception.getString("maximum_lenght_principal_investigator_255"));
}
if (fp.getString("sponsor").trim().length() > 255) {
Validator.addError(errors, "sponsor", resexception.getString("maximum_lenght_sponsor_255"));
}
if (fp.getString("officialTitle").trim().length() > 255) {
Validator.addError(errors, "officialTitle", resexception.getString("maximum_lenght_official_title_255"));
}
StudyBean studyBean = createStudyBean();
if (errors.isEmpty()) {
logger.info("no errors in the first section");
request.setAttribute("studyPhaseMap", studyPhaseMap);
request.setAttribute("statuses", Status.toActiveArrayList());
logger.info("setting arrays to request, size of list: " + Status.toArrayList().size());
if (request.getParameter("Save") != null && request.getParameter("Save").length() > 0) {
StudyDAO sdao = new StudyDAO(sm.getDataSource());
studyBean.setOwner(ub);
studyBean.setCreatedDate(new Date());
studyBean.setStatus(Status.PENDING);
studyBean = (StudyBean) sdao.create(studyBean);
StudyBean newstudyBean = (StudyBean) sdao.findByName(studyBean.getName());
UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
String selectedUserIdStr = fp.getString("selectedUser");
int selectedUserId = 0;
if (selectedUserIdStr != null && selectedUserIdStr.length() > 0) {
selectedUserId = Integer.parseInt(fp.getString("selectedUser"));
}
if (selectedUserId > 0) {
UserAccountBean user = (UserAccountBean) udao.findByPK(selectedUserId);
StudyUserRoleBean sub = new StudyUserRoleBean();
sub.setRole(Role.COORDINATOR);
sub.setStudyId(newstudyBean.getId());
sub.setStatus(Status.AVAILABLE);
sub.setOwner(ub);
udao.createStudyUserRole(user, sub);
if (ub.getId() != selectedUserId) {
sub = new StudyUserRoleBean();
sub.setRole(Role.COORDINATOR);
sub.setStudyId(newstudyBean.getId());
sub.setStatus(Status.AVAILABLE);
sub.setOwner(ub);
udao.createStudyUserRole(ub, sub);
}
} else {
StudyUserRoleBean sub = new StudyUserRoleBean();
sub.setRole(Role.COORDINATOR);
sub.setStudyId(newstudyBean.getId());
sub.setStatus(Status.AVAILABLE);
sub.setOwner(ub);
udao.createStudyUserRole(ub, sub);
}
// response.sendRedirect(request.getContextPath() +
// Page.MANAGE_STUDY_MODULE);
addPageMessage(respage.getString("the_new_study_created_succesfully_current"));
forwardPage(Page.STUDY_LIST_SERVLET);
} else {
session.setAttribute("newStudy", studyBean);
forwardPage(Page.CREATE_STUDY2);
}
} else {
session.setAttribute("newStudy", studyBean);
logger.info("has validation errors in the first section");
request.setAttribute("formMessages", errors);
// request.setAttribute("facRecruitStatusMap", facRecruitStatusMap);
UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
Collection users = udao.findAllByRole("coordinator", "director");
request.setAttribute("users", users);
forwardPage(Page.CREATE_STUDY1);
}
}
use of org.akaza.openclinica.control.form.Validator in project OpenClinica by OpenClinica.
the class UpdateProfileServlet method confirmProfile.
private void confirmProfile(UserAccountBean userBean1, UserAccountDAO udao) throws Exception {
Validator v = new Validator(request);
FormProcessor fp = new FormProcessor(request);
v.addValidation("firstName", Validator.NO_BLANKS);
v.addValidation("lastName", Validator.NO_BLANKS);
v.addValidation("email", Validator.IS_A_EMAIL);
if (!userBean1.isLdapUser()) {
v.addValidation("passwdChallengeQuestion", Validator.NO_BLANKS);
v.addValidation("passwdChallengeAnswer", Validator.NO_BLANKS);
// old password
v.addValidation("oldPasswd", Validator.NO_BLANKS);
String password = fp.getString("passwd").trim();
ConfigurationDao configurationDao = SpringServletAccess.getApplicationContext(context).getBean(ConfigurationDao.class);
org.akaza.openclinica.core.SecurityManager sm = (org.akaza.openclinica.core.SecurityManager) SpringServletAccess.getApplicationContext(context).getBean("securityManager");
String newDigestPass = sm.encrytPassword(password, getUserDetails());
List<String> pwdErrors = new ArrayList<String>();
if (!StringUtils.isBlank(password)) {
// new password
v.addValidation("passwd", Validator.IS_A_PASSWORD);
// confirm
v.addValidation("passwd1", Validator.CHECK_SAME, "passwd");
// password
PasswordRequirementsDao passwordRequirementsDao = new PasswordRequirementsDao(configurationDao);
Locale locale = LocaleResolver.getLocale(request);
ResourceBundle resexception = ResourceBundleProvider.getExceptionsBundle(locale);
pwdErrors = PasswordValidator.validatePassword(passwordRequirementsDao, udao, userBean1.getId(), password, newDigestPass, resexception);
}
v.addValidation("phone", Validator.NO_BLANKS);
errors = v.validate();
for (String err : pwdErrors) {
v.addError(errors, "passwd", err);
}
userBean1.setFirstName(fp.getString("firstName"));
userBean1.setLastName(fp.getString("lastName"));
userBean1.setEmail(fp.getString("email"));
userBean1.setInstitutionalAffiliation(fp.getString("institutionalAffiliation"));
userBean1.setPasswdChallengeQuestion(fp.getString("passwdChallengeQuestion"));
userBean1.setPasswdChallengeAnswer(fp.getString("passwdChallengeAnswer"));
userBean1.setPhone(fp.getString("phone"));
userBean1.setActiveStudyId(fp.getInt("activeStudyId"));
StudyDAO sdao = new StudyDAO(this.sm.getDataSource());
StudyBean newActiveStudy = (StudyBean) sdao.findByPK(userBean1.getActiveStudyId());
request.setAttribute("newActiveStudy", newActiveStudy);
if (errors.isEmpty()) {
logger.info("no errors");
session.setAttribute("userBean1", userBean1);
String oldPass = fp.getString("oldPasswd").trim();
if (!userBean1.isLdapUser() && !sm.isPasswordValid(ub.getPasswd(), oldPass, getUserDetails())) {
Validator.addError(errors, "oldPasswd", resexception.getString("wrong_old_password"));
request.setAttribute("formMessages", errors);
// addPageMessage("Wrong old password. Please try again.");
forwardPage(Page.UPDATE_PROFILE);
} else {
if (!StringUtils.isBlank(fp.getString("passwd"))) {
userBean1.setPasswd(newDigestPass);
userBean1.setPasswdTimestamp(new Date());
}
session.setAttribute("userBean1", userBean1);
forwardPage(Page.UPDATE_PROFILE_CONFIRM);
}
} else {
logger.info("has validation errors");
session.setAttribute("userBean1", userBean1);
request.setAttribute("formMessages", errors);
forwardPage(Page.UPDATE_PROFILE);
}
}
}
use of org.akaza.openclinica.control.form.Validator in project OpenClinica by OpenClinica.
the class RequestAccountServlet method confirmAccount.
/**
*
* @param request
* @param response
*/
private void confirmAccount() throws Exception {
Validator v = new Validator(request);
v.addValidation("name", Validator.NO_BLANKS);
v.addValidation("firstName", Validator.NO_BLANKS);
v.addValidation("lastName", Validator.NO_BLANKS);
v.addValidation("email", Validator.IS_A_EMAIL);
v.addValidation("email2", Validator.CHECK_SAME, "email");
v.addValidation("institutionalAffiliation", Validator.NO_BLANKS);
v.addValidation("activeStudyId", Validator.IS_AN_INTEGER);
v.addValidation("activeStudyRole", Validator.IS_VALID_TERM, TermType.ROLE);
HashMap errors = v.validate();
FormProcessor fp = new FormProcessor(request);
UserAccountBean ubForm = getUserBean();
request.setAttribute("otherStudy", fp.getString("otherStudy"));
session.setAttribute("newUserBean", ubForm);
if (!errors.isEmpty()) {
logger.info("after processing form,error is not empty");
request.setAttribute("formMessages", errors);
forwardPage(Page.REQUEST_ACCOUNT);
} else {
logger.info("after processing form,no errors");
sm = new SessionManager(null, ubForm.getName());
// see whether this user already in the DB
UserAccountBean ubDB = sm.getUserBean();
if (StringUtil.isBlank(ubDB.getName())) {
StudyDAO sdao = new StudyDAO(sm.getDataSource());
StudyBean study = (StudyBean) sdao.findByPK(ubForm.getActiveStudyId());
String studyName = study.getName();
request.setAttribute("studyName", studyName);
forwardPage(Page.REQUEST_ACCOUNT_CONFIRM);
} else {
addPageMessage(respage.getString("your_user_name_used_by_other_try_another"));
forwardPage(Page.REQUEST_ACCOUNT);
}
}
}
use of org.akaza.openclinica.control.form.Validator in project OpenClinica by OpenClinica.
the class RequestPasswordServlet method confirmPassword.
/**
*
* @param request
* @param response
*/
private void confirmPassword() throws Exception {
Validator v = new Validator(request);
FormProcessor fp = new FormProcessor(request);
v.addValidation("name", Validator.NO_BLANKS);
v.addValidation("email", Validator.IS_A_EMAIL);
v.addValidation("passwdChallengeQuestion", Validator.NO_BLANKS);
v.addValidation("passwdChallengeAnswer", Validator.NO_BLANKS);
errors = v.validate();
// user bean from web
UserAccountBean ubForm = new UserAccountBean();
// form
ubForm.setName(fp.getString("name"));
ubForm.setEmail(fp.getString("email"));
ubForm.setPasswdChallengeQuestion(fp.getString("passwdChallengeQuestion"));
ubForm.setPasswdChallengeAnswer(fp.getString("passwdChallengeAnswer"));
sm = new SessionManager(null, ubForm.getName(), SpringServletAccess.getApplicationContext(context));
UserAccountDAO uDAO = new UserAccountDAO(sm.getDataSource());
// see whether this user in the DB
UserAccountBean ubDB = (UserAccountBean) uDAO.findByUserName(ubForm.getName());
UserAccountBean updater = ubDB;
request.setAttribute("userBean1", ubForm);
if (!errors.isEmpty()) {
logger.info("after processing form,has errors");
request.setAttribute("formMessages", errors);
forwardPage(Page.REQUEST_PWD);
} else {
logger.info("after processing form,no errors");
// whether this user's email is in the DB
if (ubDB.getEmail() != null && ubDB.getEmail().equalsIgnoreCase(ubForm.getEmail())) {
logger.info("ubDB.getPasswdChallengeQuestion()" + ubDB.getPasswdChallengeQuestion());
logger.info("ubForm.getPasswdChallengeQuestion()" + ubForm.getPasswdChallengeQuestion());
logger.info("ubDB.getPasswdChallengeAnswer()" + ubDB.getPasswdChallengeAnswer());
logger.info("ubForm.getPasswdChallengeAnswer()" + ubForm.getPasswdChallengeAnswer());
// if this user's password challenge can be verified
if (ubDB.getPasswdChallengeQuestion().equals(ubForm.getPasswdChallengeQuestion()) && ubDB.getPasswdChallengeAnswer().equalsIgnoreCase(ubForm.getPasswdChallengeAnswer())) {
SecurityManager sm = ((SecurityManager) SpringServletAccess.getApplicationContext(context).getBean("securityManager"));
String newPass = sm.genPassword();
OpenClinicaJdbcService ocService = ((OpenClinicaJdbcService) SpringServletAccess.getApplicationContext(context).getBean("ocUserDetailsService"));
String newDigestPass = sm.encrytPassword(newPass, ocService.loadUserByUsername(ubForm.getName()));
ubDB.setPasswd(newDigestPass);
// passwdtimestamp should be null ,fix
// PrepareStatementFactory
Calendar cal = Calendar.getInstance();
//Date date = local_df.parse("01/01/1900");
//cal.setTime(date);
//ubDB.setPasswdTimestamp(cal.getTime());
ubDB.setPasswdTimestamp(null);
ubDB.setUpdater(updater);
ubDB.setLastVisitDate(new Date());
logger.info("user bean to be updated:" + ubDB.getId() + ubDB.getName() + ubDB.getActiveStudyId());
uDAO.update(ubDB);
sendPassword(newPass, ubDB);
} else {
addPageMessage(respage.getString("your_password_not_verified_try_again"));
forwardPage(Page.REQUEST_PWD);
}
} else {
addPageMessage(respage.getString("your_email_address_not_found_try_again"));
forwardPage(Page.REQUEST_PWD);
}
}
}
Aggregations