Custom Login Page

From OpenCms Wiki
Revision as of 11:23, 8 November 2006 by 84.191.134.174 (Talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

frontendlogin.jsp

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.

<%@ 
  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">
             <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 added to each page of the website. This script can be activated by pressing CTRL+spacebar and will call the frontend login form.

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.

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.

frontendlogin.js

<%@ page session="false" %>
<%@ 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;
ba_initFontSize();

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox