Custom Login Page

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
 
 
(22 intermediate revisions by 16 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 6: Line 8:
  
 
=== Sample Jsp Login File ===
 
=== Sample Jsp Login File ===
 
==== frontendlogin.jsp====
 
  
 
The following JSP file will ask a user to log in.  
 
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 size ("/sites/default"). If all of this has been successful the user will be directed to a URL which was specified by a URL parameter called ''url'' or - if such a parameter was not appended to the URL when calling the login form - the user will be redirected to the index page.
+
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.
  
<nowiki>
+
==== 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");
+
<code lang="java">
        String password = request.getParameter("password");
+
<%@ page session="true" import="org.opencms.main.*, org.opencms.jsp.*,org.opencms.file.*, java.lang.String" %>
        String url = request.getParameter("url");
+
<% CmsJspActionElement cms = new CmsJspActionElement(pageContext,request,response);
  
        boolean loginFailed = false;
+
String user = request.getParameter("user");
 +
String password = request.getParameter("password");
 +
String url = request.getParameter("url");
  
        //form was submitted => try to log in and redirect to given URl
+
if(url==null || url.length()==0 || url.equalsIgnoreCase("null")) 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
+
boolean loginFailed = false;
            if( url!=null && url.length()>0 )
+
            response.sendRedirect(url);
+
            else
+
            response.sendRedirect(cms.link("/index.html"));
+
  
          } catch ( CmsException e ) {
+
//form was submitted => try to log in and redirect to given URl
            loginFailed = true;
+
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/");
  
        //no user submitted or login failed => show login form
+
//login successful - redirect to given URL
        if((user==null)||(user.length()==0)||loginFailed) {
+
response.sendRedirect(url);
          //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><% } %>
+
} catch ( CmsException e ) {
 +
loginFailed = true;
 +
}
 +
}
 +
%>
  
            <h1>please login:</h1>
+
<%
            <form method="post">
+
//no user submitted or login failed => show login form
            <p>username: <input name="user" /></p>
+
if(user==null || user.length()==0 || loginFailed) {
            <p>password: <input name="password" type="password" /></p>
+
        %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
            <p><input type="submit" /></p>
+
<head>
            </form></body></html><%
+
<title>Frontend login</title>
        }
+
</head>
 +
<body onload="document.forms[0].elements[0].focus()">
 +
<%
 +
if (loginFailed) {
 
%>
 
%>
</nowiki>
+
<em>Login failed!</em>
 
+
<%
 
+
}
=== Javascript code for convenience ===
+
%>
In order to simplify the login process a simple Javascript file can be added to each page of the website. This script can be activated by pressing CTRL+spacebar and will call the frontend login form.
+
<h1>please login:</h1>
 
+
<form method="post" action="<%=cms.info("opencms.url")%>">
The login form will be informed, 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. 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 former state that may have cached by the browser.
+
<input type="hidden" name="url" value="<%=url%>" />
 
+
<p>username: <input name="user" /></p>
This 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 default website under the name ''/frontendlogin.jsp''.
+
<p>password: <input name="password" type="password" /></p>
 
+
<p><input type="submit" /></p>
=== frontendlogin.js ===
+
</form>
<nowiki>
+
</body>
<%@ page session="false" %>
+
</html>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
+
<%
function frontLogin(evt) {
+
if(!evt)evt=window.event;
+
if(evt&&(evt.type== "keyup")&&(evt.keyCode == 32)&&evt.ctrlKey)
+
    //if ctrl+space is pressed: redirect to the frontend login page;
+
    // give the current URL as parameter so that the user comes back afterwards
+
    location.href"<cms:link>/frontendlogin.jsp</cms:link>?url="+escape(location.href);
+
 
}
 
}
 
+
%>
ocument.onkeyup=frontLogin;
+
</code>
ba_initFontSize();
+
</nowiki>
+

Latest revision as of 13: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