Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.92 KB | None | 0 0
  1. public class LoginAction extends AbstractSessionAction
  2. {
  3.     private final UserService service;
  4.     private String emailAddress;
  5.     private String password;
  6.    
  7.     /**
  8.      *
  9.      * @param service
  10.      */
  11.     public LoginAction(UserService service)
  12.     {
  13.         this.service = service;
  14.     }
  15.    
  16.     @Override
  17.     public String execute() throws Exception
  18.     {
  19.         if(service.authenticateUser(emailAddress, password))
  20.         {
  21.             IUser user = service.getUser(emailAddress);
  22.             if(user.getUserType().equals(UserType.LANDLORD))
  23.             {
  24.                 session.put(LANDLORD, user);
  25.                 return LANDLORD_RESULT;
  26.             }
  27.             else if(user.getUserType().equals(UserType.TENANT))
  28.             {
  29.                 session.put(TENANT, user);
  30.                 return TENANT_RESULT;
  31.             }
  32.         }
  33.        
  34.         addActionError("Invalid user credentials");
  35.         return INPUT;
  36.     }
  37.  
  38.     /**
  39.      * @return the emailAddress
  40.      */
  41.     public final String getEmailAddress()
  42.     {
  43.         return emailAddress;
  44.     }
  45.  
  46.     /**
  47.      * @param emailAddress the emailAddress to set
  48.      */
  49.     public final void setEmailAddress(String emailAddress)
  50.     {
  51.         this.emailAddress = emailAddress;
  52.     }
  53.  
  54.     /**
  55.      * @return the password
  56.      */
  57.     public final String getPassword()
  58.     {
  59.         return password;
  60.     }
  61.  
  62.     /**
  63.      * @param password the password to set
  64.      */
  65.     public final void setPassword(String password)
  66.     {
  67.         this.password = password;
  68.     }
  69. }
  70.  
  71. public class UserService
  72. {
  73.     private final UserDao userDao;
  74.     private final EmailService emailService;
  75.    
  76.     /**
  77.      * Default constructor
  78.      * @param dao
  79.      * @param emailService
  80.      */
  81.     public UserService(UserDao dao, EmailService emailService)
  82.     {
  83.         userDao = dao;
  84.         this.emailService = emailService;
  85.     }
  86.    
  87.     /**
  88.      *
  89.      * @param username
  90.      * @param password
  91.      * @return true if the username/password is correct
  92.      */
  93.     public boolean authenticateUser(String username, String password)
  94.     {
  95.         return userDao.authenticateUser(username, Md5Util.getHash(password));
  96.     }
  97.    
  98. }
  99.  
  100. public class UserDao implements UserSql, Columns
  101. {
  102.     // Logger
  103.     private static final Logger LOG = Logger.getLogger(UserSql.class);
  104.     /** Database connection pool */
  105.     private static final DBConnectionPool connectionPool = new DBConnectionPool(50);
  106.    
  107.     /**
  108.      * Authenticate the user
  109.      * @param emailAddress
  110.      * @param password
  111.      * @return true if the user is authenticated
  112.      */
  113.     public boolean authenticateUser(String emailAddress, String password)
  114.     {
  115.         boolean isValid = false;
  116.         Connection conn = null;
  117.         try
  118.         {
  119.             conn = connectionPool.getConnection();
  120.             PreparedStatement ps = conn.prepareStatement(AUTHENTICATE_USER);
  121.             ps.setString(1, emailAddress);
  122.             ps.setString(2, password);
  123.             ResultSet results = ps.executeQuery();
  124.             if(results.first())
  125.             {
  126.                 int count = results.getInt(1);
  127.                 if(count == 1)
  128.                     isValid = true;
  129.             }
  130.            
  131.             ps.close();
  132.         }
  133.         catch (SQLException e)
  134.         {
  135.             LOG.error("Unable to authenticate user", e);
  136.         }
  137.         finally
  138.         {
  139.             if(conn != null)
  140.                 connectionPool.returnConnection(conn);
  141.         }
  142.         return isValid;
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement