RollerSession rollerSession =
RollerSession.getRollerSession(request);
if (rollerSession.isGlobalAdminUser() )
{
UserManager mgr = RollerFactory.getRoller().getUserManager();
UserAdminForm userForm = (UserAdminForm)actionForm;
if (userForm.isNewUser()) {
UserData user = new UserData();
userForm.copyTo(user, request.getLocale());
user.setId(null);
user.setDateCreated(new java.util.Date());
user.setEnabled(Boolean.TRUE);
// Check username and email addresses
msgs = validate(userForm, msgs);
// Must have matching passwords and confirm passwords
if ( !StringUtils.isEmpty(userForm.getPasswordText())
&& !StringUtils.isEmpty(userForm.getPasswordConfirm()))
{
try {
user.resetPassword(RollerFactory.getRoller(),
userForm.getPasswordText(),
userForm.getPasswordConfirm());
} catch (RollerException e) {
msgs.add(ActionErrors.GLOBAL_ERROR,
new ActionError("userSettings.passwordResetError"));
}
} else {
msgs.add(ActionErrors.GLOBAL_ERROR,
new ActionError("userSettings.needPasswordTwice"));
}
// If no error messages, then add user
if (msgs.isEmpty()) {
try {
// Save new user to database
mgr.addUser(user);
RollerFactory.getRoller().flush();
msgs.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("userSettings.saved"));
saveMessages(request, msgs);
// Operation complete, return to edit action
userForm.setUserName(null);
userForm.setNewUser((false));
} catch (RollerException e) {
// Add and commit failed, so show nice error message
msgs.add(ActionErrors.GLOBAL_ERROR,
new ActionError(e.getMessage()));
saveErrors(request, msgs);
}
} else {
saveErrors(request, msgs);
}
return edit(mapping, actionForm, request, response);
} else {
UserData user = mgr.getUser(userForm.getId());
userForm.copyTo(user, request.getLocale());
// Check username and email addresses
msgs = validate(userForm, msgs);
// If user set both password and passwordConfirm then reset
if ( !StringUtils.isEmpty(userForm.getPasswordText())
&& !StringUtils.isEmpty(userForm.getPasswordConfirm()))
{
try {
user.resetPassword(RollerFactory.getRoller(),
userForm.getPasswordText(),
userForm.getPasswordConfirm());
} catch (RollerException e) {
msgs.add(ActionErrors.GLOBAL_ERROR,
new ActionMessage(
"userSettings.passwordResetError"));
}
} else if (!StringUtils.isEmpty(userForm.getPasswordText())
|| !StringUtils.isEmpty(userForm.getPasswordConfirm())) {
// But it's an error to specify only one of the two
msgs.add(ActionErrors.GLOBAL_ERROR,
new ActionMessage(
"userSettings.needPasswordTwice"));
}
if (msgs.isEmpty()) {
try {
// Persist changes to user
mgr.saveUser(user);
RollerFactory.getRoller().flush();
msgs.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("userSettings.saved"));
saveMessages(request, msgs);
// Operation complete, return to edit action
userForm.setUserName(null);
} catch (RollerException e) {
msgs.add(ActionErrors.GLOBAL_ERROR,
new ActionMessage(e.getMessage()));
saveErrors(request, msgs);