11,103 → 11,127 |
import org.mentawai.validation.ValidationInterceptor; |
import org.mentawai.validation.Validator; |
|
/** |
* Action class UserAction. |
* |
* @author Sergio Oliveira |
* |
*/ |
public class UserAction extends BaseAction implements Validatable, AuthenticationFree, ValidationInterceptor { |
|
private final UserDAO userDAO; |
|
public UserAction(UserDAO userDAO) { |
this.userDAO = userDAO; |
} |
|
// You cannot log to register... |
@Override |
public boolean bypassAuthentication(String method) { |
|
if (method == null) return false; |
|
if (method.equals("add")) return true; |
if (method.equals("check")) return true; |
|
return false; |
} |
|
// 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]$"; |
|
if (method != null && method.equals("add") && isPost()) { |
|
val.requiredFields("Required Field", "username", "password", "groupId"); |
|
val.requiredLists("Required Field", "groupId"); |
|
val.add("username", RegexRule.getInstance(username_regex), "Invalid Username"); |
|
val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!"); |
|
val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!"); |
} |
} |
|
@Override |
public boolean beforeValidation(String method) { |
return true; // go ahead with validation... |
} |
|
@Override |
public void afterValidation(String method, boolean wasOk) { |
|
if (method == null) return; |
|
if (method.equals("add") && !wasOk) { |
|
output.setValue("showForm", "addNewUser"); |
} |
} |
|
boolean checkUsernameAdd(String username) { |
|
return userDAO.findByUsername(username) == null; |
} |
|
public String check() { |
|
if (!isPost()) return ERROR; |
|
String username = input.getString("username"); |
|
String sessionUsername = input.getString("sessionUsername"); |
|
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; |
|
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); |
|
userDAO.insert(u); |
|
addMessage("User " + u.getUsername() + " added!", true); |
|
return CREATED; |
} |
} |
} |
/** Attribute userDAO of UserAction. */ |
private final UserDAO userDAO; |
|
/** |
* Parametric constructor. |
* @param userDAO UserDAO |
*/ |
public UserAction(UserDAO userDAO) { |
this.userDAO = userDAO; |
} |
|
/** |
* {@inheritDoc} |
*/ |
// You cannot log to register... |
@Override |
public boolean bypassAuthentication(String method) { |
|
if (method == null) return false; |
|
if (method.equals("add")) return true; |
if (method.equals("check")) return true; |
|
return false; |
} |
|
/** |
* {@inheritDoc} |
*/ |
// 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]$"; |
|
if (method != null && method.equals("add") && isPost()) { |
|
val.requiredFields("Required Field", "username", "password", "groupId"); |
|
val.requiredLists("Required Field", "groupId"); |
|
val.add("username", RegexRule.getInstance(username_regex), "Invalid Username"); |
|
val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!"); |
|
val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!"); |
} |
} |
|
/** |
* {@inheritDoc} |
*/ |
@Override |
public boolean beforeValidation(String method) { |
return true; // go ahead with validation... |
} |
|
/** |
* {@inheritDoc} |
*/ |
@Override |
public void afterValidation(String method, boolean wasOk) { |
|
if (method == null) return; |
|
if (method.equals("add") && !wasOk) { |
|
output.setValue("showForm", "addNewUser"); |
} |
} |
|
boolean checkUsernameAdd(String username) { |
|
return userDAO.findByUsername(username) == null; |
} |
|
public String check() { |
|
if (!isPost()) return ERROR; |
|
String username = input.getString("username"); |
|
String sessionUsername = input.getString("sessionUsername"); |
|
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; |
|
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); |
|
userDAO.insert(u); |
|
addMessage("User " + u.getUsername() + " added!", true); |
|
return CREATED; |
} |
} |
} |