Custom Login Page

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
(frontendlogin.jsp)
 
(15 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 +
[[Category:Extending OpenCms]]
 +
 
== Frontend login ==
 
== Frontend login ==
 
A usual requirement for extension is to add an additional login form that allows for "frontend login". Frontend login means, that a user can login directly on the website without being brought to the OpenCms workplace. This is usually combined with [[Frontend Editing]], where the user can edit pages directly from the website view, without having to enter the workplace.
 
A usual requirement for extension is to add an additional login form that allows for "frontend login". Frontend login means, that a user can login directly on the website without being brought to the OpenCms workplace. This is usually combined with [[Frontend Editing]], where the user can edit pages directly from the website view, without having to enter the workplace.
Line 13: Line 15:
 
==== frontendlogin.jsp====
 
==== frontendlogin.jsp====
  
<nowiki>
+
<code lang="java">
<%@  
+
<%@ page session="true" import="org.opencms.main.*, org.opencms.jsp.*,org.opencms.file.*, java.lang.String" %>
  page session="true"  
+
<% CmsJspActionElement cms = new CmsJspActionElement(pageContext,request,response);
  import="org.opencms.main.*, org.opencms.jsp.*,org.opencms.file.*, java.lang.String"  
+
%><%
+
        CmsJspActionElement cms = new CmsJspActionElement(pageContext,request,response);
+
  
        String user = request.getParameter("user");
+
String user = request.getParameter("user");
        String password = request.getParameter("password");
+
String password = request.getParameter("password");
        String url = request.getParameter("url");
+
String url = request.getParameter("url");
  
        boolean loginFailed = false;
+
if(url==null || url.length()==0 || url.equalsIgnoreCase("null")) url = "";
  
        //form was submitted => try to log in and redirect to given URl
+
boolean loginFailed = false;
        if((user!=null)&&(user.length()!=0)) {
+
          try{
+
            CmsObject cmsObject = cms.getCmsObject();
+
            cmsObject.loginUser(user, password);
+
            CmsProject cmsproject = cmsObject.readProject("Offline");
+
            cmsObject.getRequestContext().setCurrentProject(cmsproject);
+
            cmsObject.getRequestContext().setSiteRoot("/sites/default");
+
  
            //login successful - redirect to given URL
+
//form was submitted => try to log in and redirect to given URl
            if( url!=null && url.length()>0 )
+
if((user != null) && (user.length() != 0)) {
            response.sendRedirect(url);
+
try{
            else
+
CmsObject cmsObject = cms.getCmsObject();
            response.sendRedirect(cms.link("/index.html"));
+
cmsObject.loginUser(user, password);
 +
CmsProject cmsproject = cmsObject.readProject("Offline");
 +
cmsObject.getRequestContext().setCurrentProject(cmsproject);
 +
cmsObject.getRequestContext().setSiteRoot("/sites/default/");
  
          } catch ( CmsException e ) {
+
//login successful - redirect to given URL
            loginFailed = true;
+
response.sendRedirect(url);
          }
+
        }
+
  
        //no user submitted or login failed => show login form
+
} catch ( CmsException e ) {
        if((user==null)||(user.length()==0)||loginFailed) {
+
loginFailed = true;
          //send login form
+
}
          %><!DOCTYPE html PUBLIC
+
}
              "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
%>
            <html><head><title>Frontend login</title></head>
+
            <body onload="document.forms[0].elements[0].focus()">
+
 
+
            <%if (loginFailed) { %><em>Login failed!</em><% } %>
+
  
            <h1>please login:</h1>
+
<%
            <form method="post" action="<%=cms.info("opencms.url")%>">
+
//no user submitted or login failed => show login form
            <input type="hidden" name="url" value="<%=url%>" />
+
if(user==null || user.length()==0 || loginFailed) {
            <p>username: <input name="user" /></p>
+
        %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
            <p>password: <input name="password" type="password" /></p>
+
<head>
            <p><input type="submit" /></p>
+
<title>Frontend login</title>
            </form></body></html><%
+
</head>
        }
+
<body onload="document.forms[0].elements[0].focus()">
 +
<%
 +
if (loginFailed) {
 
%>
 
%>
</nowiki>
+
<em>Login failed!</em>
 
+
<%
'''Note:''' For Java 1.5, you will need to alter your null check in the "login successful - redirect to given URL" to:
+
}
if( url!=null && !url.equalsIgnoreCase("null") ) {
+
%>
 
+
<h1>please login:</h1>
=== Javascript code for convenience ===
+
<form method="post" action="<%=cms.info("opencms.url")%>">
In order to simplify the login process a simple Javascript file can be created inside of OpenCms and then referenced from the header template of each page of the website. This script can be activated by pressing CTRL+spacebar and will call the frontend login form. For some browsers, it is necessary to press SHIFT in addition for the event to come through to the page, so that it becomes SHIFT+CTRL+spacebar.
+
<input type="hidden" name="url" value="<%=url%>" />
 
+
<p>username: <input name="user" /></p>
The login form will be notified, from which page the login was issued, so that after a successful login the user can be re-directed back to the page where they initially triggered the login process. Note that, depending on the users' browser settings, the page may have been cached and a reload may be necessary, before the user sees the actual online state of the page instead of the online state that they had looked at before.
+
<p>password: <input name="password" type="password" /></p>
 
+
<p><input type="submit" /></p>
The following file must be created with the file type ''JSP'' inside of OpenCms, because it contains some small processing. It assumes that the page for frontend login resides in the main folder of the default website (usuallly, ''/sites/default'') and that it has the file name ''frontendlogin.jsp''.
+
</form>
 
+
</body>
==== frontendlogin.js ====
+
</html>
<nowiki>
+
<%
<%@ page session="false" %>
+
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
+
document.onkeyup=function(e){
+
if(!e)e=window.event;
+
if(e&&(e.type=="keyup")&&(e.keyCode==32)&&e.ctrlKey) 
+
  location.href="<cms:link>/frontendlogin.jsp</cms:link>?url="+escape(location.href);
+
 
}
 
}
</nowiki>
+
%>
 +
</code>

Latest revision as of 12:39, 8 January 2010


Frontend login

A usual requirement for extension is to add an additional login form that allows for "frontend login". Frontend login means, that a user can login directly on the website without being brought to the OpenCms workplace. This is usually combined with Frontend Editing, where the user can edit pages directly from the website view, without having to enter the workplace.

A login form for this purpose will ask for login information, log the user into the system and bring him or her back to the page where they were before.


Sample Jsp Login File

The following JSP file will ask a user to log in.

If the form is submitted, the JSP code will attempt to log the user into the system. If that has been successful, the current project of that user is set to the "Offline" project and the site of the user is set to the default site ("/sites/default"). If all of this has been successful the user will be directed to a URL which was handed over to the login form by a URL parameter called url. If such a parameter was not handed over to the login form, then the user will be redirected to the index page.

frontendlogin.jsp

<%@ page session="true" import="org.opencms.main.*, org.opencms.jsp.*,org.opencms.file.*, java.lang.String" %>
<% CmsJspActionElement cms = new CmsJspActionElement(pageContext,request,response);
 
String user = request.getParameter("user");
String password = request.getParameter("password");
String url = request.getParameter("url");
 
if(url==null || url.length()==0 || url.equalsIgnoreCase("null")) url = "";
 
boolean loginFailed = false;
 
//form was submitted => try to log in and redirect to given URl
if((user != null) && (user.length() != 0)) {
	try{
		CmsObject cmsObject = cms.getCmsObject();
		cmsObject.loginUser(user, password);
		CmsProject cmsproject = cmsObject.readProject("Offline");
		cmsObject.getRequestContext().setCurrentProject(cmsproject);
		cmsObject.getRequestContext().setSiteRoot("/sites/default/");
 
		//login successful - redirect to given URL
		response.sendRedirect(url);
 
	} catch ( CmsException e ) {
		loginFailed = true;
	}
}
%>
 
<%
//no user submitted or login failed => show login form
if(user==null || user.length()==0 || loginFailed) {
        %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
	<head>
		<title>Frontend login</title>
	</head>
	<body onload="document.forms[0].elements[0].focus()">
<%
	if (loginFailed) {
%>
		<em>Login failed!</em>
<%
	}
%>
	<h1>please login:</h1>
	<form method="post" action="<%=cms.info("opencms.url")%>">
		<input type="hidden" name="url" value="<%=url%>" />
		<p>username: <input name="user" /></p>
		<p>password: <input name="password" type="password" /></p>
		<p><input type="submit" /></p>
	</form>
	</body>
	</html>
<%
}
%>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox