Forums de discussion

Changes to LoginActionClass in Liferay 5.2.3

thumbnail
A. Ki, modifié il y a 14 années.

Changes to LoginActionClass in Liferay 5.2.3

New Member Publications: 22 Date d'inscription: 19/12/07 Publications récentes
Hi,
I wanted to upgrade my portal to 5.2.3, now it´s running in 5.1.2.
In my old version I´ve got a customized login portlet, but now, it doesn´t work anymore because of chages to teh LoginAction Class.
Could maybe someone tell my, whats the recommend way?
Should I change my login portlet to act with the new login action, but I dont realy know what to change, because of the compicated login procedure in liferay --- or shoul I put the old Class to the portlet (its a standalone) or to the ext?
Hope for Help.

thats my old login view.jsp

<div id="wpe-login-message">
		&lt;%
		String login = GetterUtil.getString((String)PortalClassInvoker.invoke("com.liferay.portal.action.LoginAction", "getLogin", request, "login", company, false));
		boolean rememberMe = ParamUtil.getBoolean(request, "rememberMe");
		%&gt;

		<form action="<portlet:actionURL><portlet:param name=" <%="Constants.CMD" %>" value="&lt;%= Constants.UPDATE %&gt;" /&gt;" method="post" name="<portlet:namespace />fm"&gt;
		<input name="save_last_path" type="hidden" value="0">
		<input name="<portlet:namespace />rememberMe" type="hidden" value="<%= rememberMe %>">

		<br><br><br><br><pre><code>

public class LoginAction extends Action {

	public static String getLogin(
			HttpServletRequest request, String paramName, Company company)
		throws PortalException, SystemException {

		String login = request.getParameter(paramName);

		if ((login == null) || (login.equals(StringPool.NULL))) {
			login = GetterUtil.getString(
				CookieKeys.getCookie(request, CookieKeys.LOGIN));

			if (PropsValues.COMPANY_LOGIN_PREPOPULATE_DOMAIN &amp;&amp;
				Validator.isNull(login) &amp;&amp;
				company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {

				login = "@" + company.getMx();
			}
		}

		return login;
	}

	public static void login(
			HttpServletRequest request, HttpServletResponse response,
			String login, String password, boolean rememberMe)
		throws Exception {

		CookieKeys.validateSupportCookie(request);

		HttpSession session = request.getSession();

		long userId = GetterUtil.getLong(login);

		int authResult = Authenticator.FAILURE;

		Company company = PortalUtil.getCompany(request);

		Map<string, string[]> headerMap = new HashMap<string, string[]>();

		Enumeration<string> enu1 = request.getHeaderNames();

		while (enu1.hasMoreElements()) {
			String name = enu1.nextElement();

			Enumeration<string> enu2 = request.getHeaders(name);

			List<string> headers = new ArrayList<string>();

			while (enu2.hasMoreElements()) {
				String value = enu2.nextElement();

				headers.add(value);
			}

			headerMap.put(name, headers.toArray(new String[headers.size()]));
		}

		Map<string, string[]> parameterMap = request.getParameterMap();

		if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
			authResult = UserLocalServiceUtil.authenticateByEmailAddress(
				company.getCompanyId(), login, password, headerMap,
				parameterMap);

			userId = UserLocalServiceUtil.getUserIdByEmailAddress(
				company.getCompanyId(), login);
		}
		else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
			authResult = UserLocalServiceUtil.authenticateByScreenName(
				company.getCompanyId(), login, password, headerMap,
				parameterMap);

			userId = UserLocalServiceUtil.getUserIdByScreenName(
				company.getCompanyId(), login);
		}
		else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
			authResult = UserLocalServiceUtil.authenticateByUserId(
				company.getCompanyId(), userId, password, headerMap,
				parameterMap);
		}

		if (authResult == Authenticator.SUCCESS) {
			if (PropsValues.SESSION_ENABLE_PHISHING_PROTECTION) {

				// Invalidate the previous session to prevent phishing

				Boolean httpsInitial = (Boolean)session.getAttribute(
					WebKeys.HTTPS_INITIAL);

				LastPath lastPath = (LastPath)session.getAttribute(
					WebKeys.LAST_PATH);

				try {
					session.invalidate();
				}
				catch (IllegalStateException ise) {

					// This only happens in Geronimo

					if (_log.isWarnEnabled()) {
						_log.warn(ise.getMessage());
					}
				}

				session = request.getSession(true);

				if (httpsInitial != null) {
					session.setAttribute(WebKeys.HTTPS_INITIAL, httpsInitial);
				}

				if (lastPath != null) {
					session.setAttribute(WebKeys.LAST_PATH, lastPath);
				}
			}

			// Set cookies

			String domain = CookieKeys.getDomain(request);

			User user = UserLocalServiceUtil.getUserById(userId);

			String userIdString = String.valueOf(userId);

			session.setAttribute("j_username", userIdString);
			session.setAttribute("j_password", user.getPassword());
			session.setAttribute("j_remoteuser", userIdString);

			session.setAttribute(WebKeys.USER_PASSWORD, password);

			Cookie companyIdCookie = new Cookie(
				CookieKeys.COMPANY_ID, String.valueOf(company.getCompanyId()));

			if (Validator.isNotNull(domain)) {
				companyIdCookie.setDomain(domain);
			}

			companyIdCookie.setPath(StringPool.SLASH);

			Cookie idCookie = new Cookie(
				CookieKeys.ID,
				UserLocalServiceUtil.encryptUserId(userIdString));

			if (Validator.isNotNull(domain)) {
				idCookie.setDomain(domain);
			}

			idCookie.setPath(StringPool.SLASH);

			Cookie passwordCookie = new Cookie(
				CookieKeys.PASSWORD,
				Encryptor.encrypt(company.getKeyObj(), password));

			if (Validator.isNotNull(domain)) {
				passwordCookie.setDomain(domain);
			}

			passwordCookie.setPath(StringPool.SLASH);

			Cookie rememberMeCookie = new Cookie(
				CookieKeys.REMEMBER_ME, Boolean.TRUE.toString());

			if (Validator.isNotNull(domain)) {
				rememberMeCookie.setDomain(domain);
			}

			rememberMeCookie.setPath(StringPool.SLASH);

			int loginMaxAge = PropsValues.COMPANY_SECURITY_AUTO_LOGIN_MAX_AGE;

			if (PropsValues.SESSION_DISABLED) {
				rememberMe = true;
			}

			if (rememberMe) {
				companyIdCookie.setMaxAge(loginMaxAge);
				idCookie.setMaxAge(loginMaxAge);
				passwordCookie.setMaxAge(loginMaxAge);
				rememberMeCookie.setMaxAge(loginMaxAge);
			}
			else {

				// This was explicitly changed from 0 to -1 so that the cookie
				// lasts as long as the browser. This allows an external servlet
				// wrapped in AutoLoginFilter to work throughout the client
				// connection. The cookies ARE removed on an actual logout, so
				// there is no security issue. See LEP-4678 and LEP-5177.

				companyIdCookie.setMaxAge(-1);
				idCookie.setMaxAge(-1);
				passwordCookie.setMaxAge(-1);
				rememberMeCookie.setMaxAge(0);
			}

			Cookie loginCookie = new Cookie(CookieKeys.LOGIN, login);

			if (Validator.isNotNull(domain)) {
				loginCookie.setDomain(domain);
			}

			loginCookie.setMaxAge(loginMaxAge);
			loginCookie.setPath(StringPool.SLASH);

			Cookie screenNameCookie = new Cookie(
				CookieKeys.SCREEN_NAME,
				Encryptor.encrypt(company.getKeyObj(), user.getScreenName()));

			if (Validator.isNotNull(domain)) {
				screenNameCookie.setDomain(domain);
			}

			screenNameCookie.setMaxAge(loginMaxAge);
			screenNameCookie.setPath(StringPool.SLASH);

			CookieKeys.addCookie(response, companyIdCookie);
			CookieKeys.addCookie(response, idCookie);
			CookieKeys.addCookie(response, passwordCookie);
			CookieKeys.addCookie(response, rememberMeCookie);
			CookieKeys.addCookie(response, loginCookie);
			CookieKeys.addCookie(response, screenNameCookie);
		}
		else {
			throw new AuthException();
		}
	}

	public static void sendPassword(HttpServletRequest request)
		throws Exception {

		ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
			WebKeys.THEME_DISPLAY);

		Company company = themeDisplay.getCompany();

		if (!company.isSendPassword()) {
			return;
		}

		if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
			CaptchaUtil.check(request);
		}

		String emailAddress = ParamUtil.getString(request, "emailAddress");

		String remoteAddr = request.getRemoteAddr();
		String remoteHost = request.getRemoteHost();
		String userAgent = request.getHeader(HttpHeaders.USER_AGENT);

		UserLocalServiceUtil.sendPassword(
			PortalUtil.getCompanyId(request), emailAddress, remoteAddr,
			remoteHost, userAgent);

		SessionMessages.add(request, "request_processed", emailAddress);
	}

	public ActionForward execute(
			ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response)
		throws Exception {

		if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS &amp;&amp;
			!request.isSecure()) {

			ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
				WebKeys.THEME_DISPLAY);

			StringBuilder sb = new StringBuilder();

			sb.append(PortalUtil.getPortalURL(request, true));
			sb.append(themeDisplay.getURLSignIn());

			response.sendRedirect(sb.toString());

			return null;
		}

		HttpSession session = request.getSession();

		ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
			WebKeys.THEME_DISPLAY);

		if (session.getAttribute("j_username") != null &amp;&amp;
			session.getAttribute("j_password") != null) {

			if (PropsValues.PORTAL_JAAS_ENABLE) {
				return mapping.findForward("/portal/touch_protected.jsp");
			}
			else {
				response.sendRedirect(themeDisplay.getPathMain());

				return null;
			}
		}

		String cmd = ParamUtil.getString(request, Constants.CMD);

		if (cmd.equals("already-registered")) {
			try {
				login(request, response);

				if (PropsValues.PORTAL_JAAS_ENABLE) {
					return mapping.findForward("/portal/touch_protected.jsp");
				}
				else {
					String redirect = ParamUtil.getString(request, "redirect");

					if (Validator.isNotNull(redirect)) {
						response.sendRedirect(redirect);
					}
					else {
						response.sendRedirect(themeDisplay.getPathMain());
					}

					return null;
				}
			}
			catch (Exception e) {
				if (e instanceof AuthException) {
					Throwable cause = e.getCause();

					if (cause instanceof PasswordExpiredException ||
						cause instanceof UserLockoutException) {

						SessionErrors.add(request, cause.getClass().getName());
					}
					else {
						SessionErrors.add(request, e.getClass().getName());
					}

					return mapping.findForward("portal.login");
				}
				else if (e instanceof CookieNotSupportedException ||
						 e instanceof NoSuchUserException ||
						 e instanceof PasswordExpiredException ||
						 e instanceof UserEmailAddressException ||
						 e instanceof UserIdException ||
						 e instanceof UserLockoutException ||
						 e instanceof UserPasswordException ||
						 e instanceof UserScreenNameException) {

					SessionErrors.add(request, e.getClass().getName());

					return mapping.findForward("portal.login");
				}
				else {
					PortalUtil.sendError(e, request, response);

					return null;
				}
			}
		}
		else if (cmd.equals("forgot-password")) {
			try {
				sendPassword(request);

				return mapping.findForward("portal.login");
			}
			catch (Exception e) {
				if (e instanceof CaptchaTextException ||
					e instanceof NoSuchUserException ||
					e instanceof SendPasswordException ||
					e instanceof UserEmailAddressException) {

					SessionErrors.add(request, e.getClass().getName());

					return mapping.findForward("portal.login");
				}
				else {
					PortalUtil.sendError(e, request, response);

					return null;
				}
			}
		}
		else {
			String authLoginURL = PortalUtil.getCommunityLoginURL(themeDisplay);

			if (Validator.isNull(authLoginURL)) {
				authLoginURL = PropsValues.AUTH_LOGIN_URL;
			}

			if (Validator.isNotNull(authLoginURL)) {
				response.sendRedirect(authLoginURL);

				return null;
			}
			else {
				return mapping.findForward("portal.login");
			}
		}
	}

	protected void login(
			HttpServletRequest request, HttpServletResponse response)
		throws Exception {

		String login = ParamUtil.getString(request, "login").toLowerCase();
		String password = ParamUtil.getString(
			request, SessionParameters.get(request, "password"));
		boolean rememberMe = ParamUtil.getBoolean(request, "rememberMe");

		login(request, response, login, password, rememberMe);
	}

	private static Log _log = LogFactory.getLog(LoginAction.class);



</string,></string></string></string></string></string,></string,></code></pre><br><br><br><br><br><img alt="emoticon" src="@theme_images_path@/emoticons/happy.gif"><table style="clear: right; padding-top: 10px;">
		<tbody><tr>
			<td style="padding-right: 10px; padding-bottom: 5px;">
				<liferay-ui:message key="login" />
			</td>
			<td>
				<input name="<portlet:namespace />login" style="width: 120px; height: 18px; padding: 0px 1px; border: 1px solid #CCCCCC; background: #BFD4F2;" type="text" value="<%= HtmlUtil.escape(login) %>">
			</td>
			<td style="padding-left: 10px; ">
				<input type="submit" style="width: 120px; height: 20px; padding-bottom: 5px;" value="<liferay-ui:message key=" sign-in">" /&gt;
			</td>
		</tr>
		<tr>
			<td style="padding-right: 10px; padding-bottom: 5px;">
				<liferay-ui:message key="password" />
			</td>
			<td>
				<input id="<portlet:namespace />password" name="<portlet:namespace />password" style="width: 120px; height: 18px; padding: 0px 1px; border: 1px solid #CCCCCC; background: #BFD4F2;" type="password" value="">

				<span id="<portlet:namespace />passwordCapsLockSpan" style="display: none;"><liferay-ui:message key="caps-lock-is-on" /></span>
			</td>
			<td style="padding-left: 10px;">
				<input type="button" value="Registrieren" style="width: 120px; height: 20px;" onclick="location.href='/web/wp_energie/register'">
			</td>
		</tr>
		<tr>
			<td colspan="2">
			
				<liferay-ui:error exception="<%= AuthException.class %>" message="authentication-failed" />
				<liferay-ui:error exception="<%= CookieNotSupportedException.class %>" message="authentication-failed-please-enable-browser-cookies" />
				<liferay-ui:error exception="<%= NoSuchUserException.class %>" message="please-enter-a-valid-login" />
				<liferay-ui:error exception="<%= PasswordExpiredException.class %>" message="your-password-has-expired" />
				<liferay-ui:error exception="<%= UserEmailAddressException.class %>" message="please-enter-a-valid-login" />
				<liferay-ui:error exception="<%= UserLockoutException.class %>" message="this-account-has-been-locked" />
				<liferay-ui:error exception="<%= UserPasswordException.class %>" message="please-enter-a-valid-password" />
				<liferay-ui:error exception="<%= UserScreenNameException.class %>" message="please-enter-a-valid-screen-name" />
			
			</td>
		</tr>and the login action classps. Its don´t possible to me to use the liferay login.Thanks for fast answers </tbody></table></form></div>
thumbnail
Chris Kauffman, modifié il y a 14 années.

RE: Changes to LoginActionClass in Liferay 5.2.3

New Member Publications: 21 Date d'inscription: 18/11/08 Publications récentes
One change of note is that the call to LoginAction.getLogin (from 5.1.2) appears to have moved to LoginUtil.getLogin (in 5.2.3). I'm not sure what other changes liferay made which broke compatibility with your code. Generally you simply have to dig through the sources long enough until you can reverse engineer any updates.