Guest User

Untitled

a guest
Jul 20th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. /*
  2. * Created on Oct 7, 2004
  3. *
  4. */
  5. package com.dotmarketing.cms.login.factories;
  6.  
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11.  
  12. import com.dotcms.enterprise.BaseAuthenticator;
  13. import com.dotcms.enterprise.LDAPImpl;
  14. import com.dotmarketing.business.APILocator;
  15. import com.dotmarketing.business.CacheLocator;
  16. import com.dotmarketing.cms.factories.PublicEncryptionFactory;
  17. import com.dotmarketing.cms.login.struts.LoginForm;
  18. import com.dotmarketing.portal.struts.DotCustomLoginPostAction;
  19. import com.dotmarketing.util.Config;
  20. import com.dotmarketing.util.Logger;
  21. import com.dotmarketing.util.UtilMethods;
  22. import com.dotmarketing.util.WebKeys;
  23. import com.liferay.portal.NoSuchUserException;
  24. import com.liferay.portal.auth.Authenticator;
  25. import com.liferay.portal.model.Company;
  26. import com.liferay.portal.model.User;
  27. import com.liferay.portal.util.PropsUtil;
  28.  
  29. /**
  30. * @author will
  31. *
  32. */
  33. public class LoginFactory {
  34.  
  35. public static String PRE_AUTHENTICATOR = PropsUtil.get("auth.pipeline.pre");
  36.  
  37. public static boolean doLogin(LoginForm form, HttpServletRequest request, HttpServletResponse response) throws NoSuchUserException {
  38. return doLogin(form.getUserName(), form.getPassword(), form.isRememberMe(), request, response);
  39.  
  40. }
  41.  
  42. public static boolean doCookieLogin(String encryptedId, HttpServletRequest request, HttpServletResponse response) {
  43.  
  44. try {
  45. String decryptedId = PublicEncryptionFactory.decryptString(encryptedId);
  46. User user = APILocator.getUserAPI().loadUserById(decryptedId,APILocator.getUserAPI().getSystemUser(),false);
  47. try {
  48. String userName = user.getEmailAddress();
  49. Company comp = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany();
  50. if (comp.getAuthType().equals(Company.AUTH_TYPE_ID)) {
  51. userName = user.getUserId();
  52. }
  53.  
  54. return doLogin(userName, user.getPassword(), true, request, response);
  55. } catch (Exception e) {
  56. return false;
  57. }
  58. } catch (Exception e) {
  59. Logger.error(LoginFactory.class, "AutoLogin Failed" + e);
  60.  
  61. }
  62.  
  63. doLogout(request, response);
  64.  
  65. return false;
  66. }
  67.  
  68. /**
  69. *
  70. * @param userName
  71. * @param password
  72. * @param rememberMe
  73. * @param request
  74. * @param response
  75. * @return
  76. */
  77. public static boolean doLogin(String userName, String password, boolean rememberMe, HttpServletRequest request, HttpServletResponse response) throws NoSuchUserException {
  78. try {
  79. User user = null;
  80. boolean match = false;
  81. Company comp = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany();
  82.  
  83. if (comp.getAuthType().equals(Company.AUTH_TYPE_EA)) {
  84. if(userName.equalsIgnoreCase(APILocator.getUserAPI().getSystemUser().getEmailAddress())){
  85. return false;
  86. }
  87. } else {
  88. if(userName.equalsIgnoreCase(APILocator.getUserAPI().getSystemUser().getUserId())){
  89. return false;
  90. }
  91. }
  92.  
  93. if ((PRE_AUTHENTICATOR != null) &&
  94. (0 < PRE_AUTHENTICATOR.length()) &&
  95. PRE_AUTHENTICATOR.equals(Config.getStringProperty("LDAP_FRONTEND_AUTH_IMPLEMENTATION"))) {
  96. Class ldap_auth_impl_class = Class.forName(Config.getStringProperty("LDAP_FRONTEND_AUTH_IMPLEMENTATION"));
  97. Authenticator ldap_auth_impl = (Authenticator) ldap_auth_impl_class.newInstance();
  98. int auth = 0;
  99.  
  100. if (comp.getAuthType().equals(Company.AUTH_TYPE_EA)) {
  101. auth = ldap_auth_impl.authenticateByEmailAddress(comp.getCompanyId(), userName, password);
  102. } else {
  103. auth = ldap_auth_impl.authenticateByUserId(comp.getCompanyId(), userName, password);
  104. }
  105.  
  106. if (comp.getAuthType().equals(Company.AUTH_TYPE_EA)) {
  107. user = APILocator.getUserAPI().loadByUserByEmail(userName, APILocator.getUserAPI().getSystemUser(), false);
  108. } else {
  109. user = APILocator.getUserAPI().loadUserById(userName, APILocator.getUserAPI().getSystemUser(), false);
  110. }
  111.  
  112. try{
  113. boolean SYNC_PASSWORD = BaseAuthenticator.SYNC_PASSWORD;
  114. if(!SYNC_PASSWORD){
  115. String roleName = LDAPImpl.LDAP_USER_ROLE;
  116. if(com.dotmarketing.business.APILocator.getRoleAPI().doesUserHaveRole(user, roleName)){
  117. user.setPassword(DotCustomLoginPostAction.FAKE_PASSWORD);
  118. APILocator.getUserAPI().save(user,APILocator.getUserAPI().getSystemUser(),false);
  119. }
  120. }
  121. }catch (Exception e) {
  122. Logger.debug(LoginFactory.class, "syncPassword not set or unable to load user", e);
  123. }
  124.  
  125. match = auth == Authenticator.SUCCESS;
  126. } else {
  127. if (comp.getAuthType().equals(Company.AUTH_TYPE_EA)) {
  128. user = APILocator.getUserAPI().loadByUserByEmail(userName, APILocator.getUserAPI().getSystemUser(), false);
  129. } else {
  130. user = APILocator.getUserAPI().loadUserById(userName, APILocator.getUserAPI().getSystemUser(), false);
  131. }
  132.  
  133. if ((user == null) || (!UtilMethods.isSet(user.getEmailAddress()))) {
  134. throw new NoSuchUserException();
  135. }
  136.  
  137. if (user.isNew() ||
  138. (!Config.getBooleanProperty("ALLOW_INACTIVE_ACCOUNTS_TO_LOGIN", false) && !user.isActive())) {
  139. return false;
  140. }
  141.  
  142. match = user.getPassword().equals(password) || user.getPassword().equals(PublicEncryptionFactory.digestString(password));
  143.  
  144. if (match) {
  145. user.setLastLoginDate(new java.util.Date());
  146. APILocator.getUserAPI().save(user,APILocator.getUserAPI().getSystemUser(),false);
  147. } else {
  148. user.setFailedLoginAttempts(user.getFailedLoginAttempts()+1);
  149. APILocator.getUserAPI().save(user,APILocator.getUserAPI().getSystemUser(),false);
  150. }
  151. }
  152.  
  153. // if passwords match
  154. if (match) {
  155. HttpSession ses = request.getSession();
  156.  
  157. // session stuff
  158. ses.setAttribute(WebKeys.CMS_USER, user);
  159.  
  160. //set personalization stuff on session
  161.  
  162. // set id cookie
  163. Cookie autoLoginCookie = UtilMethods.getCookie(request.getCookies(), WebKeys.CMS_USER_ID_COOKIE);
  164.  
  165. if(autoLoginCookie == null && rememberMe) {
  166. autoLoginCookie = new Cookie(WebKeys.CMS_USER_ID_COOKIE, APILocator.getUserAPI().encryptUserId(user.getUserId()));
  167. }
  168.  
  169. if (rememberMe) {
  170. autoLoginCookie.setMaxAge(60 * 60 * 24 * 356);
  171. } else if (autoLoginCookie != null) {
  172. autoLoginCookie.setMaxAge(0);
  173. }
  174.  
  175. if (autoLoginCookie != null) {
  176. autoLoginCookie.setPath("/");
  177. response.addCookie(autoLoginCookie);
  178. }
  179.  
  180. boolean useCASPostLoginSyncLDAPGroups = Config.getBooleanProperty("CAS_POST_SYNC_LDAP_GROUPS");
  181. boolean useFrontendCASFilter = Config.getBooleanProperty("FRONTEND_CAS_FILTER_ON");
  182.  
  183. if(useFrontendCASFilter){
  184. if(UtilMethods.isSet(ses.getAttribute("edu.yale.its.tp.cas.client.filter.user")))
  185. ses.setAttribute("edu.yale.its.tp.cas.client.filter.user", user.getUserId());
  186. //flush users roles cache
  187.  
  188. if(useCASPostLoginSyncLDAPGroups){
  189. CacheLocator.getCmsRoleCache().clearCache();
  190. CacheLocator.getUserCache().clearCache();
  191. //sync ldap groups, add to session
  192. if(UtilMethods.isSet(ses.getAttribute("CMS_USER_GROUPS_ROLES")))
  193. ses.setAttribute("CMS_USER_GROUPS_ROLES", APILocator.getRoleAPI().loadRolesForUser(user.getUserId()));
  194. }
  195.  
  196. }
  197.  
  198. return true;
  199. }
  200. } catch (NoSuchUserException e) {
  201. throw e;
  202. } catch (Exception e) {
  203. Logger.error(LoginFactory.class, "Login Failed" + e);
  204. }
  205.  
  206. return false;
  207. }
  208.  
  209. public static void doLogout(HttpServletRequest request, HttpServletResponse response) {
  210.  
  211. //request.getSession().invalidate();
  212. /*
  213. * request.getSession().removeAttribute(WebKeys.SESSION_USER);
  214. * request.getSession().removeAttribute(com.liferay.portal.util.WebKeys.USER_ID);
  215. * request.getSession().removeAttribute(com.liferay.portal.util.WebKeys.USER);
  216. */
  217.  
  218. request.getSession().removeAttribute("PENDING_ALERT_SEEN");
  219. request.getSession().removeAttribute("createAccountForm");
  220. request.getSession().removeAttribute("checkoutForm");
  221. request.getSession().removeAttribute(WebKeys.CMS_USER);
  222. request.getSession().removeAttribute(WebKeys.REDIRECT_AFTER_LOGIN);
  223. request.getSession().removeAttribute(WebKeys.LOGGED_IN_USER_CATS);
  224. request.getSession().removeAttribute(WebKeys.LOGGED_IN_USER_TAGS);
  225. request.getSession().removeAttribute(WebKeys.USER_FAVORITES);
  226.  
  227. Cookie idCookie = new Cookie(WebKeys.CMS_USER_ID_COOKIE, null);
  228. idCookie.setMaxAge(0);
  229. idCookie.setPath("/");
  230. response.addCookie(idCookie);
  231.  
  232. }
  233.  
  234.  
  235. }
Add Comment
Please, Sign In to add comment