MentaTutorials

Compare Revisions

Ignore whitespace Rev 54 → Rev 55

/MentaRefApp/trunk/src/main/java/org/menta/action/UserAction.java
14,165 → 14,185
import org.mentawai.validation.Validator;
 
public class UserAction extends BaseAction implements Validatable {
 
private final UserDAO userDAO;
public UserAction(UserDAO userDAO) {
 
public UserAction(final UserDAO userDAO) {
this.userDAO = userDAO;
}
 
// Validate the fields for the user form...
@Override
public void prepareValidator(Validator val, String method) {
String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
public void prepareValidator(final Validator val, final String method) {
 
final String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
 
if (method != null && method.equals("add") && isPost()) {
 
val.requiredFields("required_field", "username", "password", "email", "groupId", "languageId");
 
val.requiredLists("required_field", "groupId", "languageId");
 
val.add("username", RegexRule.getInstance(username_regex), "bad_username");
 
val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "username_already_exists");
 
val.add("email", EmailRule.getInstance(), "bad_email");
 
val.add("password", EqualRule.getInstance("password", "passconf"), "pass_no_match");
} else if (method != null && method.equals("edit") && isPost()) {
 
}
else if (method != null && method.equals("edit") && isPost()) {
 
val.requiredFields("required_field", "username", "email", "groupId");
 
val.requiredLists("required_field", "groupId");
 
val.add("username", RegexRule.getInstance(username_regex), "bad_username");
 
val.add("username", MethodRule.getInstance(this, "checkUsernameEdit"), "username_already_exists");
 
val.add("email", EmailRule.getInstance(), "bad_email");
}
 
}
boolean checkUsernameAdd(String username) {
 
boolean checkUsernameAdd(final String username) {
 
return userDAO.findByUsername(username) == null;
}
boolean checkUsernameEdit(String username) {
User currentUser = getSessionObj();
 
boolean checkUsernameEdit(final String username) {
 
final User currentUser = getSessionObj();
 
// first check if he is actually changing his username...
 
if (!currentUser.getUsername().equals(username)) {
 
return userDAO.findByUsername(username) == null;
}
 
return true;
}
public String check(String username) {
if (!isPost()) return ERROR;
User sessionUser = getSessionObj();
 
public String check(final String username) {
 
if (!isPost()) {
return ERROR;
}
 
final User sessionUser = getSessionObj();
 
String sessionUsername = null;
if (sessionUser != null) sessionUsername = sessionUser.getUsername();
if (isEmpty(username)) return ERROR;
User u = userDAO.findByUsername(username);
if (u == null) return SUCCESS; // username does not exist
if (sessionUsername != null && u.getUsername().equals(sessionUsername)) return SUCCESS;
 
if (sessionUser != null) {
sessionUsername = sessionUser.getUsername();
}
 
if (isEmpty(username)) {
return ERROR;
}
 
final User u = userDAO.findByUsername(username);
 
if (u == null) {
return SUCCESS; // username does not exist
}
 
if (sessionUsername != null && u.getUsername().equals(sessionUsername)) {
return SUCCESS;
}
 
return ALREADY;
}
 
public String add() {
 
if (!isPost()) {
 
// we only want to allow post to add an user...
 
return ERROR;
} else {
User u = input.getObject(User.class);
 
}
else {
 
final User u = input.getObject(User.class);
 
userDAO.insert(u);
 
setSessionObj(u);
 
setSessionGroup(u.getGroup());
 
setSessionLocale(u.getLocale());
 
// send email asynchronous to user with password
Letter welcome = new TextLetter("welcome.txt");
 
final Letter welcome = new TextLetter("welcome.txt");
welcome.setAttribute("username", u.getUsername());
welcome.setAttribute("password", u.getPassword());
 
try {
String subject = welcome.getSubject(getSessionLocale());
String body = welcome.getText(getSessionLocale());
 
final String subject = welcome.getSubject(getSessionLocale());
final String body = welcome.getText(getSessionLocale());
 
SimpleEmail.sendLater(u.getUsername(), u.getEmail(), subject, body);
} catch(Exception e) {
 
}
catch (final Exception e) {
 
System.err.println("Error sending email to: " + u.getEmail());
 
e.printStackTrace();
}
 
addMessage("registration_ok");
 
return CREATED;
}
}
 
public String edit() {
 
if (!isPost()) {
 
// display user for update...
User u = getSessionObj();
 
final User u = getSessionObj();
 
output.setValue("user", u);
 
return SHOW;
} else {
int id = input.getInt("id");
User newUser = userDAO.load(id);
 
}
else {
 
final int id = input.getInt("id");
 
final User sessionUser = getSessionObj();
 
if (id != sessionUser.getId()) {
throw new SecurityException("Cannot change id smart ass!");
}
 
final User newUser = userDAO.load(id);
 
input.inject(newUser);
 
userDAO.update(newUser);
 
replaceSessionObj(newUser);
 
setSessionGroup(newUser.getGroup());
 
setSessionLocale(newUser.getLocale());
 
addMessage("edit_ok");
 
return UPDATED;
}
}
}
}