em = JpaConnection.getInstance().getCurrentEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("from Site site where systemRecord = 'Y' and siteId != '_system'");
Site siteDefault = (Site) query.getSingleResult();
IdSecurity idSecurity = new IdSecurity(siteDefault, form.getUserId(), true);
if (idSecurity.isSuspened()) {
errors.add("error", new ActionMessage("content.error.login.suspended.temporary", Constants.ID_SUSPEND_TIME / 1000 / 60));
saveMessages(request, errors);
form.setUserPassword("");
idSecurity.fail();
return actionMapping.findForward("error");
}
query = em.createQuery("from User user where userId = :userId");
query.setParameter("userId", form.getUserId());
List<?> list = query.getResultList();
if (list.size() == 0) {
errors.add("error", new ActionMessage("error.login.invalid"));
saveMessages(request, errors);
form.setUserPassword("");
idSecurity.fail();
em.getTransaction().commit();
return actionMapping.findForward("error");
}
user = (User) list.get(0);
if (user.getActive() != Constants.VALUE_YES) {
errors.add("error", new ActionMessage("error.login.invalid"));
saveMessages(request, errors);
form.setUserPassword("");
idSecurity.fail();
em.getTransaction().commit();
return actionMapping.findForward("error");
}
String userPassword = AESEncoder.getInstance().decode(user.getUserPassword());
if (!userPassword.equals(form.getUserPassword())) {
errors.add("error", new ActionMessage("error.login.invalid"));
saveMessages(request, errors);
form.setUserPassword("");
idSecurity.fail();
em.getTransaction().commit();
return actionMapping.findForward("error");
}
Site site = null;
if (!Format.isNullOrEmpty(user.getUserLastVisitSiteId())) {
site = SiteDAO.load(user.getUserLastVisitSiteId());
}
if (site == null) {
site = SiteDAO.getDefaultSite(user);
}
if (site == null) {
errors.add("error", new ActionMessage("error.login.access"));
saveMessages(request, errors);
form.setUserPassword("");
return actionMapping.findForward("error");
}
user.setUserLastVisitSiteId(site.getSiteId());
HttpSession httpSession = request.getSession();
AdminBean adminBean = new AdminBean();
adminBean.init(user.getUserId(), site.getSiteId());
httpSession.setAttribute("adminBean", adminBean);
Cookie cookie = new Cookie("user", user.getUserName());
cookie.setMaxAge(-1);
response.addCookie(cookie);
httpSession.setAttribute("userLastLoginDatetime", user.getUserLastLoginDatetime());
user.setUserLastLoginDatetime(new Date());
idSecurity.reset();
em.getTransaction().commit();
}
catch (Throwable e) {
logger.error(e);
return actionMapping.findForward("exception");