Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Override Login JSP using Liferay OSGI module

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 3.28k
    Comment on it

    Dear Programmers,

     

    Liferay 7 has been modularized to a great extend, so the most of the JSPs you might want to override are no longer in Liferay's core. Being modularized, Liferay 7 comes with a modular approach to override the core JSP. In this blog, I'm going to override the Login JSP.

     

    Before override the JSP, you must know the fragment name and the version for that module. I've written this blog to get the detail about Login module.

     

    To override a JSP you have to create a 'Liferay Workspace Project' and a module to override JSP. Follow this blog to create a module project under liferay workspace.

     

    Once done, open the gradle.properties file in your 'Liferay Workspace Project' and configure that for all modules.

     

    #liferay.workspace.bundle.url=https://sourceforge.net/projects/lportal/files/Liferay Portal/7.0.1 GA2/liferay-ce-portal-tomcat-7.0-ga2-20160610113014153.zip
    liferay.workspace.environment=local
    liferay.workspace.home.dir=/home/dinesh/liferay/liferay7/liferay-ce-portal-7.0-ga3
    liferay.home=/home/dinesh/liferay/liferay7/liferay-ce-portal-7.0-ga3
    #liferay.workspace.modules.default.repository.enabled=false
    #liferay.workspace.modules.dir=modules
    #liferay.workspace.plugins.sdk.dir=plugins-sdk
    #liferay.workspace.themes.dir=themes
    
    microsoft.translator.client.id=
    microsoft.translator.client.secret=

     

    Specify your workspace home directory and liferay home directory if you are handling server from other tool like terminal. Specify your environment, as I uncommented the lines and set 'local' environment. You can configure your local environment by updating properties in 'Liferay Workspace Project' by editing file 'evon-module->configs->local->portal-ext.properties'.

     

    The 'Liferay Workspace Project' is configured now. Now open your module under the workspace project. (Check this if any confusion in workspace project and module.) Please follow the steps below to override Login JSP.

    1) Open bnd.bnd file and add the following lines - 

     

    Fragment-Host: com.liferay.login.web;bundle-version="1.0.7"
    -sources: true
    -jsp: *.jsp,*.jspf
    -plugin.jsp: com.liferay.ant.bnd.jsp.JspAnalyzerPlugin

     

    Please make sure, Fragment-Host and bungle-version must match to installed module's actual host and version found in this blog.

     

    Create login.jsp file in src/main/resources/META-INF/resources/ under your directory structure. You can delete the init.jsp and view.jsp because these will not be in used now. Please delete the portlet class or package in src/main/java as well. I suggested to delete the java/jsp files because we are overriding the JSP with a specific name.

     

    Add the following code in your login.jsp - 

     

    <%@page import="com.liferay.portal.kernel.util.PropertiesUtil"%>
    <%@page import="java.util.Properties"%>
    <%@page import="com.liferay.portal.kernel.portlet.PortletURLUtil"%>
    <%@page import="javax.portlet.PortletURL"%>
    <%@page import="javax.portlet.WindowState"%>
    <%@page import="javax.portlet.PortletMode"%>
    <%@page import="com.liferay.portal.kernel.util.PropsKeys"%>
    <%@page import="com.liferay.portal.kernel.util.StringPool"%>
    <%@page import="com.liferay.portal.kernel.model.User"%>
    <%@page import="com.liferay.portal.kernel.util.Validator"%>
    <%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
    <%@page import="com.liferay.portal.kernel.util.HtmlUtil"%>
    <%@page import="javax.portlet.PortletPreferences" %>
    <%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
    <%@page import="com.liferay.portal.kernel.model.CompanyConstants"%>
    <%@page import="com.liferay.portal.kernel.exception.UserScreenNameException"%>
    <%@page import="com.liferay.portal.kernel.exception.UserPasswordException"%>
    <%@page import="com.liferay.portal.kernel.exception.UserLockoutException"%>
    <%@page import="com.liferay.portal.kernel.exception.UserEmailAddressException"%>
    <%@page import="com.liferay.portal.kernel.exception.PasswordExpiredException"%>
    <%@page import="com.liferay.portal.kernel.exception.NoSuchUserException"%>
    <%@page import="com.liferay.portal.kernel.exception.CookieNotSupportedException"%>
    <%@page import="com.liferay.portal.kernel.exception.CompanyMaxUsersException"%>
    <%@page import="com.liferay.portal.kernel.security.auth.AuthException"%>
    <%@page import="com.liferay.portal.kernel.util.PrefsPropsUtil"%>
    <%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>
    <%@page import="com.liferay.portal.kernel.util.PortletKeys"%>
    <%@page import="com.liferay.portal.kernel.util.PropsKeys"%>
    
    
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
    
    <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
    <%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
    <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
    <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
    
    <liferay-theme:defineObjects />
    
    <portlet:defineObjects />
    
    <%
    PortletMode portletMode = liferayPortletRequest.getPortletMode();
    WindowState windowState = liferayPortletRequest.getWindowState();
    
    PortletURL currentURLObj = PortletURLUtil.getCurrent(liferayPortletRequest, liferayPortletResponse);
    
    String currentURL = currentURLObj.toString();
    
    PortletPreferences preferences = portletPreferences;
    
    String authType = preferences.getValue("authType", StringPool.BLANK);
    long companyId = company.getCompanyId();
    
    %>
    
    <p style="color: red">changed</p>
    
    <c:choose>
    	<c:when test="<%= themeDisplay.isSignedIn() %>">
    
    		<%
    		String signedInAs = HtmlUtil.escape(user.getFullName());
    		if (themeDisplay.isShowMyAccountIcon() && (themeDisplay.getURLMyAccount() != null)) {
    			String myAccountURL = String.valueOf(themeDisplay.getURLMyAccount());
    			signedInAs = "<a class=\"signed-in\" href=\"" + HtmlUtil.escape(myAccountURL) + "\">" + signedInAs + "</a>";
    		}
    		%>
    
    		<liferay-ui:message arguments="<%= signedInAs %>" key="you-are-signed-in-as-x" translateArguments="<%= false %>" />
    	</c:when>
    	<c:otherwise>
    
    		<%
    		String redirect = ParamUtil.getString(request, "redirect");
    		//String login = LoginUtil.getLogin(request, "login", company);
    		String password = StringPool.BLANK;
    		boolean rememberMe = ParamUtil.getBoolean(request, "rememberMe");
    		if (Validator.isNull(authType)) {
    			authType = company.getAuthType();
    		}
    		%>
    
    		<portlet:actionURL name="/login/login" secure="<%= PrefsPropsUtil.getBoolean(companyId, PropsKeys.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS) || request.isSecure() %>" var="loginURL" />
    
    		<aui:form action="<%= loginURL %>" autocomplete='<%= PrefsPropsUtil.getBoolean(companyId, PropsKeys.COMPANY_SECURITY_LOGIN_FORM_AUTOCOMPLETE) ? "on" : "off" %>' cssClass="sign-in-form" method="post" name="fm" onSubmit="event.preventDefault();">
    			<aui:input name="saveLastPath" type="hidden" value="<%= false %>" />
    			<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
    			<aui:input name="doActionAfterLogin" type="hidden" value="<%= portletName.equals(PortletKeys.FAST_LOGIN) ? true : false %>" />
    
    			<c:choose>
    				<c:when test='<%= SessionMessages.contains(request, "userAdded") %>'>
    
    					<%
    					String userEmailAddress = (String)SessionMessages.get(request, "userAdded");
    					String userPassword = (String)SessionMessages.get(request, "userAddedPassword");
    					%>
    
    					<div class="alert alert-success">
    						<c:choose>
    							<c:when test="<%= company.isStrangersVerify() || Validator.isNull(userPassword) %>">
    								<liferay-ui:message key="thank-you-for-creating-an-account" />
    
    								<c:if test="<%= company.isStrangersVerify() %>">
    									<liferay-ui:message arguments="<%= userEmailAddress %>" key="your-email-verification-code-has-been-sent-to-x" translateArguments="<%= false %>" />
    								</c:if>
    							</c:when>
    							<c:otherwise>
    								<liferay-ui:message arguments="<%= userPassword %>" key="thank-you-for-creating-an-account.-your-password-is-x" translateArguments="<%= false %>" />
    							</c:otherwise>
    						</c:choose>
    
    						<c:if test="<%= PrefsPropsUtil.getBoolean(companyId, PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED) %>">
    							<liferay-ui:message arguments="<%= userEmailAddress %>" key="your-password-has-been-sent-to-x" translateArguments="<%= false %>" />
    						</c:if>
    					</div>
    				</c:when>
    				<c:when test='<%= SessionMessages.contains(request, "userPending") %>'>
    
    					<%
    					String userEmailAddress = (String)SessionMessages.get(request, "userPending");
    					%>
    
    					<div class="alert alert-success">
    						<liferay-ui:message arguments="<%= userEmailAddress %>" key="thank-you-for-creating-an-account.-you-will-be-notified-via-email-at-x-when-your-account-has-been-approved" translateArguments="<%= false %>" />
    					</div>
    				</c:when>
    			</c:choose>
    
    			<liferay-ui:error exception="<%= AuthException.class %>" message="authentication-failed" />
    			<liferay-ui:error exception="<%= CompanyMaxUsersException.class %>" message="unable-to-log-in-because-the-maximum-number-of-users-has-been-reached" />
    			<liferay-ui:error exception="<%= CookieNotSupportedException.class %>" message="authentication-failed-please-enable-browser-cookies" />
    			<liferay-ui:error exception="<%= NoSuchUserException.class %>" message="authentication-failed" />
    			<liferay-ui:error exception="<%= PasswordExpiredException.class %>" message="your-password-has-expired" />
    			<liferay-ui:error exception="<%= UserEmailAddressException.MustNotBeNull.class %>" message="please-enter-an-email-address" />
    			<liferay-ui:error exception="<%= UserLockoutException.class %>" message="this-account-is-locked" />
    			<liferay-ui:error exception="<%= UserLockoutException.LDAPLockout.class %>" message="this-account-is-locked" />
    
    			<liferay-ui:error exception="<%= UserPasswordException.class %>" message="authentication-failed" />
    			<liferay-ui:error exception="<%= UserScreenNameException.class %>" message="the-screen-name-cannot-be-blank" />
    			<liferay-ui:error exception="<%= UserScreenNameException.MustNotBeNull.class %>" message="the-screen-name-cannot-be-blank" />
    
    			<aui:fieldset>
    
    				<%
    				String loginLabel = null;
    				if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
    					loginLabel = "email-address";
    				}
    				else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
    					loginLabel = "screen-name";
    				}
    				else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
    					loginLabel = "id";
    				}
    				%>
    
    				<aui:input autoFocus="<%= windowState.equals(LiferayWindowState.EXCLUSIVE) || windowState.equals(WindowState.MAXIMIZED) %>" cssClass="clearable" label="<%= loginLabel %>" name="login" showRequiredLabel="<%= false %>" type="text" value="">
    					<aui:validator name="required" />
    				</aui:input>
    
    				<aui:input name="password" showRequiredLabel="<%= false %>" type="password" value="<%= password %>">
    					<aui:validator name="required" />
    				</aui:input>
    
    				<span id="<portlet:namespace />passwordCapsLockSpan" style="display: none;"><liferay-ui:message key="caps-lock-is-on" /></span>
    
    				<c:if test="<%= company.isAutoLogin() && !PrefsPropsUtil.getBoolean(companyId, PropsKeys.SESSION_DISABLED) %>">
    					<aui:input checked="<%= rememberMe %>" name="rememberMe" type="checkbox" />
    				</c:if>
    			</aui:fieldset>
    
    			<aui:button-row>
    				<aui:button type="submit" value="sign-in" />
    			</aui:button-row>
    		</aui:form>
    
    		<%-- <liferay-util:include page="/navigation.jsp" servletContext="<%= application %>" /> --%>
    
    		<aui:script sandbox="<%= true %>">
    			var form = AUI.$(document.<portlet:namespace />fm);
    
    			form.on(
    				'submit',
    				function(event) {
    					var redirect = form.fm('redirect');
    
    					if (redirect) {
    						var redirectVal = redirect.val();
    
    						redirect.val(redirectVal + window.location.hash);
    					}
    
    					submitForm(form);
    				}
    			);
    
    			form.fm('password').on(
    				'keypress',
    				function(event) {
    					Liferay.Util.showCapsLock(event, '<portlet:namespace />passwordCapsLockSpan');
    				}
    			);
    		</aui:script>
    	</c:otherwise>
    </c:choose>

     

    Deploy the code. Just double click on deploy in your 'Gradle Task' window under your module project. It will deploy the module to your liferay server. Change would be applied like below.

     

    All done.

     

    Thanks.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: