Custom Login Page
(some formatting correction; tweaked Javascript code) |
m (→frontendlogin.js) |
||
Line 79: | Line 79: | ||
<%@ page session="false" %> | <%@ page session="false" %> | ||
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %> | <%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %> | ||
− | |||
document.onkeyup=function(e){ | document.onkeyup=function(e){ | ||
if(!e)e=window.event; | if(!e)e=window.event; |
Revision as of 00:57, 18 December 2006
Contents |
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 or - if such a parameter was handed over to the login form - 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"); 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 if( url!=null && url.length()>0 ) response.sendRedirect(url); else response.sendRedirect(cms.link("/index.html")); } catch ( CmsException e ) { loginFailed = true; } } //no user submitted or login failed => show login form if((user==null)||(user.length()==0)||loginFailed) { //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")%>"> <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><% } %>
Javascript code for convenience
In order to simplify the login process a simple Javascript file can be created inside of OpenCms and then referenced inside of 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.
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.
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.
frontendlogin.js
<%@ 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); }