Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginAction extends AbstractSessionAction
- {
- private final UserService service;
- private String emailAddress;
- private String password;
- /**
- *
- * @param service
- */
- public LoginAction(UserService service)
- {
- this.service = service;
- }
- @Override
- public String execute() throws Exception
- {
- if(service.authenticateUser(emailAddress, password))
- {
- IUser user = service.getUser(emailAddress);
- if(user.getUserType().equals(UserType.LANDLORD))
- {
- session.put(LANDLORD, user);
- return LANDLORD_RESULT;
- }
- else if(user.getUserType().equals(UserType.TENANT))
- {
- session.put(TENANT, user);
- return TENANT_RESULT;
- }
- }
- addActionError("Invalid user credentials");
- return INPUT;
- }
- /**
- * @return the emailAddress
- */
- public final String getEmailAddress()
- {
- return emailAddress;
- }
- /**
- * @param emailAddress the emailAddress to set
- */
- public final void setEmailAddress(String emailAddress)
- {
- this.emailAddress = emailAddress;
- }
- /**
- * @return the password
- */
- public final String getPassword()
- {
- return password;
- }
- /**
- * @param password the password to set
- */
- public final void setPassword(String password)
- {
- this.password = password;
- }
- }
- public class UserService
- {
- private final UserDao userDao;
- private final EmailService emailService;
- /**
- * Default constructor
- * @param dao
- * @param emailService
- */
- public UserService(UserDao dao, EmailService emailService)
- {
- userDao = dao;
- this.emailService = emailService;
- }
- /**
- *
- * @param username
- * @param password
- * @return true if the username/password is correct
- */
- public boolean authenticateUser(String username, String password)
- {
- return userDao.authenticateUser(username, Md5Util.getHash(password));
- }
- }
- public class UserDao implements UserSql, Columns
- {
- // Logger
- private static final Logger LOG = Logger.getLogger(UserSql.class);
- /** Database connection pool */
- private static final DBConnectionPool connectionPool = new DBConnectionPool(50);
- /**
- * Authenticate the user
- * @param emailAddress
- * @param password
- * @return true if the user is authenticated
- */
- public boolean authenticateUser(String emailAddress, String password)
- {
- boolean isValid = false;
- Connection conn = null;
- try
- {
- conn = connectionPool.getConnection();
- PreparedStatement ps = conn.prepareStatement(AUTHENTICATE_USER);
- ps.setString(1, emailAddress);
- ps.setString(2, password);
- ResultSet results = ps.executeQuery();
- if(results.first())
- {
- int count = results.getInt(1);
- if(count == 1)
- isValid = true;
- }
- ps.close();
- }
- catch (SQLException e)
- {
- LOG.error("Unable to authenticate user", e);
- }
- finally
- {
- if(conn != null)
- connectionPool.returnConnection(conn);
- }
- return isValid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement