LogonActionpublic final class LogonAction extends BaseAction
Fields Summary |
---|
static String | USERNAMEName of username field ["username"]. | static String | PASSWORDName of password field ["password"]. |
Methods Summary |
---|
void | SaveUser(javax.servlet.http.HttpServletRequest request, User user)Store User object in client session.
If user object is null, any existing user object is removed.
HttpSession session = request.getSession();
session.setAttribute(Constants.USER_KEY, user);
if (log.isDebugEnabled()) {
log.debug(
"LogonAction: User '"
+ user.getUsername()
+ "' logged on in session "
+ session.getId());
}
| public org.apache.struts.action.ActionForward | execute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Use "username" and "password" fields from ActionForm to retrieve a User
object from the database. If credentials are not valid, or database
has disappeared, post error messages and forward to input.
// Local variables
// UserDatabase database = getUserDatabase(request);
UserDatabase database = getUserDatabase();
String username = (String) PropertyUtils.getSimpleProperty(form,
USERNAME);
String password = (String) PropertyUtils.getSimpleProperty(form,
PASSWORD);
ActionMessages errors = new ActionMessages();
// Retrieve user
User user = getUser(database,username,password,errors);
// Save (or clear) user object
SaveUser(request,user);
// Report back any errors, and exit if any
if (!errors.isEmpty()) {
this.saveErrors(request, errors);
return (mapping.getInputForward());
}
// Otherwise, return "success"
return (findSuccess(mapping));
| User | getUser(UserDatabase database, java.lang.String username, java.lang.String password, org.apache.struts.action.ActionMessages errors)Confirm user credentials. Post any errors and return User object
(or null).
// ------------------------------------------------------ Protected Methods
User user = null;
if (database == null){
errors.add(
ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.database.missing"));
}
else {
user = database.findUser(username);
if ((user != null) && !user.getPassword().equals(password)) {
user = null;
}
if (user == null) {
errors.add(
ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.password.mismatch"));
}
}
return user;
|
|