List of usage examples for java.text MessageFormat MessageFormat
public MessageFormat(String pattern, Locale locale)
From source file:org.sakaiproject.user.tool.UsersAction.java
/** * Read the user form and update the user in state. * // w w w. j a v a 2 s . c o m * @return true if the form is accepted, false if there's a validation error (an alertMessage will be set) */ private boolean readUserForm(RunData data, SessionState state) { // boolean parameters and values // --------------Mode----singleUser-createUser-typeEnable // Admin New-----new-----false------false------true // Admin Update--edit----false------false------true // Admin Delete--remove--false------false------false // Gateway New---null----false------true-------false // Account Edit--edit----true-------false------false // read the form String id = StringUtils.trimToNull(data.getParameters().getString("id")); String eid = StringUtils.trimToNull(data.getParameters().getString("eid")); state.setAttribute("valueEid", eid); String firstName = StringUtils.trimToNull(data.getParameters().getString("first-name")); state.setAttribute("valueFirstName", firstName); String lastName = StringUtils.trimToNull(data.getParameters().getString("last-name")); state.setAttribute("valueLastName", lastName); String email = StringUtils.trimToNull(data.getParameters().getString("email")); state.setAttribute("valueEmail", email); String pw = StringUtils.trimToNull(data.getParameters().getString("pw")); String pwConfirm = StringUtils.trimToNull(data.getParameters().getString("pw0")); String pwcur = StringUtils.trimToNull(data.getParameters().getString("pwcur")); Integer disabled = Integer .valueOf(StringUtils.trimToNull(data.getParameters().getString("disabled")) != null ? "1" : "0"); String mode = (String) state.getAttribute("mode"); boolean singleUser = ((Boolean) state.getAttribute("single-user")).booleanValue(); boolean createUser = ((Boolean) state.getAttribute("create-user")).booleanValue(); // SAK-29182 - enforce invalid domains when creating a user through Gateway -> New Account boolean isEidEditable = isEidEditable(state); if (createUser && !isEidEditable) { for (String domain : getInvalidEmailDomains()) { if (email.toLowerCase().endsWith(domain.toLowerCase())) { String defaultMsg = rb.getFormattedMessage("email.invalid.domain", new Object[] { domain }); String customMsg = ServerConfigurationService .getString(SAK_PROP_INVALID_EMAIL_DOMAINS_CUSTOM_MESSAGE, ""); if (!customMsg.isEmpty()) { String institution = ServerConfigurationService.getString("ui.institution", ""); customMsg = new MessageFormat(customMsg, rb.getLocale()) .format(new Object[] { institution, domain }, new StringBuffer(), null).toString(); } addAlert(state, customMsg.isEmpty() ? defaultMsg : customMsg); return false; } } } boolean typeEnable = false; String type = null; if ((mode != null) && (mode.equalsIgnoreCase("new"))) { typeEnable = true; } else if ((mode != null) && (mode.equalsIgnoreCase("edit")) && (!singleUser)) { typeEnable = true; } if (typeEnable) { // for the case of Admin User tool creating new user type = StringUtils.trimToNull(data.getParameters().getString("type")); state.setAttribute("valueType", type); } else { if (createUser) { // for the case of Gateway Account tool creating new user type = (String) state.getAttribute("create-type"); } } if ((Boolean) state.getAttribute("user.recaptcha-enabled")) { String challengeField = data.getParameters().getString("recaptcha_challenge_field"); String responseField = data.getParameters().getString("recaptcha_response_field"); if (challengeField == null) challengeField = ""; if (responseField == null) responseField = ""; ReCaptcha captcha = ReCaptchaFactory.newReCaptcha( (String) state.getAttribute("user.recaptcha-public-key"), (String) state.getAttribute("user.recaptcha-private-key"), false); ReCaptchaResponse response = captcha.checkAnswer(data.getRequest().getRemoteAddr(), challengeField, responseField); if (!response.isValid()) { addAlert(state, rb.getString("useact.capterr")); state.setAttribute("recaptcha-error", response.getErrorMessage()); return false; } } //Ensure valid email address. Empty emails are invalid iff email validation is required. For non-empty email Strings, use EmailValidator. //email.matches(".+@.+\\..+") boolean validateWithAccountValidator = isValidatedWithAccountValidator(state); boolean emailInvalid = StringUtils.isEmpty(email) ? validateWithAccountValidator : !EmailValidator.getInstance().isValid(email); if (emailInvalid) { addAlert(state, rb.getString("useact.invemail")); return false; } // get the user UserEdit user = (UserEdit) state.getAttribute("user"); //process any additional attributes //we continue processing these until we get an empty attribute KEY //counter starts at 1 //data is of the form: // optionalAttr_1:att1 // optionalAttrValue_1:value1 // optionalAttr_2:att2 // optionalAttrValue_2:value2 int count = 1; boolean continueProcessingOptionalAttributes = true; ResourcePropertiesEdit properties; if (user == null) { properties = new BaseResourcePropertiesEdit(); } else { properties = user.getPropertiesEdit(); } //remove all properties that are in the confugred list //then add back in only the ones that were sent //this allows us to remove items via javascript and they get persisted to the db on form save Map<String, String> configuredProperties = getOptionalAttributes(); for (String cp : configuredProperties.keySet()) { properties.removeProperty(cp); } while (continueProcessingOptionalAttributes) { //this stores the key String optionalAttributeKey = data.getParameters().getString("optionalAttr_" + count); if (StringUtils.isBlank(optionalAttributeKey)) { continueProcessingOptionalAttributes = false; break; } String optionalAttributeValue = data.getParameters().getString("optionalAttrValue_" + count); //only single values properties //any null ones will wipe out existing ones //and any duplicate ones will override previous ones (currently) properties.addProperty(optionalAttributeKey, optionalAttributeValue); //System.out.println("optionalAttributeKey: " + optionalAttributeKey + ", optionalAttributeValue: " + optionalAttributeValue); count++; } // add if needed if (user == null) { // make sure we have eid if (isEidEditable) { if (eid == null) { addAlert(state, rb.getString("usecre.eidmis")); return false; } } else { // eid is not editable, so we're using the email as the eid if (email == null) { addAlert(state, rb.getString("useact.invemail")); return false; } eid = email; } // if we validate through email, passwords will be handled in AccountValidator TempUser tempUser = new TempUser(eid, null, null, null, eid, pw, null); if (!validateWithAccountValidator) { // if in create mode, make sure we have a password if (createUser) { if (pw == null) { addAlert(state, rb.getString("usecre.pasismis")); return false; } } // make sure we have matching password fields if (StringUtil.different(pw, pwConfirm)) { addAlert(state, rb.getString("usecre.pass")); return false; } // SAK-23568 - make sure password meets policy requirements if (!validatePassword(pw, tempUser, state)) { return false; } } //Check if the email is duplicated boolean allowEmailDuplicates = ServerConfigurationService.getBoolean("user.email.allowduplicates", true); if (!allowEmailDuplicates && UserDirectoryService.checkDuplicatedEmail(tempUser)) { addAlert(state, rb.getString("useact.theuseemail1")); return false; } try { // add the user in one step so that all you need is add not update permission // (the added might be "anon", and anon has add but not update permission) //SAK-18209 only an admin user should be able to specify a ID if (!SecurityService.isSuperUser()) { id = null; } User newUser; if (validateWithAccountValidator) { // the eid is their email address. The password is random newUser = UserDirectoryService.addUser(id, eid, firstName, lastName, email, PasswordCheck.generatePassword(), type, properties); // Invoke AccountValidator to send an email to the user containing a link to a form on which they can set their name and password ValidationLogic validationLogic = (ValidationLogic) ComponentManager.get(ValidationLogic.class); validationLogic.createValidationAccount(newUser.getId(), ValidationAccount.ACCOUNT_STATUS_REQUEST_ACCOUNT); } else { newUser = UserDirectoryService.addUser(id, eid, firstName, lastName, email, pw, type, properties); if (SecurityService.isSuperUser()) { if (disabled == 1) { try { UserEdit editUser = UserDirectoryService.editUser(newUser.getId()); editUser.getProperties().addProperty("disabled", "true"); newUser = editUser; } catch (UserNotDefinedException e) { addAlert(state, rb.getString("usecre.disableFailed")); return false; } catch (UserLockedException e) { addAlert(state, rb.getString("usecre.disableFailed")); return false; } } } } // put the user in the state state.setAttribute("newuser", newUser); } catch (UserAlreadyDefinedException e) { addAlert(state, rb.getString("useact.theuseid1")); return false; } catch (UserIdInvalidException e) { addAlert(state, rb.getString("useact.theuseid2")); return false; } catch (UserPermissionException e) { addAlert(state, rb.getString("useact.youdonot3")); return false; } } // update else { if (!user.isActiveEdit()) { try { // add the user in one step so that all you need is add not update permission // (the added might be "anon", and anon has add but not update permission) user = UserDirectoryService.editUser(user.getId()); // put the user in the state state.setAttribute("user", user); } catch (UserLockedException e) { addAlert(state, rb.getString("useact.somels")); return false; } catch (UserNotDefinedException e) { Object[] params = new Object[] { id }; addAlert(state, rb.getFormattedMessage("useact.use_notfou", params)); return false; } catch (UserPermissionException e) { addAlert(state, rb.getString("useact.youdonot3")); return false; } } // Still needs super user to change super user password // If the current user isn't a super user but is trying to change the password or email of a super user print an error if (!SecurityService.isSuperUser() && SecurityService.isSuperUser(user.getId())) { addAlert(state, rb.getString("useact.youdonot4")); return false; } // eid, pw, type might not be editable if (eid != null) user.setEid(eid); user.setFirstName(firstName); user.setLastName(lastName); user.setEmail(email); if (type != null) user.setType(type); //add in the updated props user.getPropertiesEdit().addAll(properties); if (SecurityService.isSuperUser()) { if (disabled == 1) { user.getProperties().addProperty("disabled", "true"); } else { user.getProperties().removeProperty("disabled"); } } //validate the password only for local users if (!isProvidedType(user.getType())) { // make sure the old password matches, but don't check for super users if (!SecurityService.isSuperUser()) { if (!user.checkPassword(pwcur)) { addAlert(state, rb.getString("usecre.curpass")); return false; } } if (mode == null || !mode.equalsIgnoreCase("remove")) { // make sure we have matching password fields if (StringUtil.different(pw, pwConfirm)) { addAlert(state, rb.getString("usecre.pass")); return false; } // SAK-23568 - make sure password meets policy requirements if (!validatePassword(pw, user, state)) { return false; } if (pw != null) user.setPassword(pw); } } } return true; }