[opencms-dev] My simple, convenient (and ugly!) sample for frontend login

Christian Steinert christian_steinert at web.de
Sun May 28 15:15:57 CEST 2006


Hi List,

Having just started with frontend login, I wanted to share the code, 
that I use. It's very simple.

There is a JSP called "frontendlogin.jsp", which does the actual work. 
The /system/ folder is a good place for such things, because it can 
always be reached, no matter which site root is currently set, so I 
store it inside my module folder under 
/system/modules/com.my.module/frontendlogin.jsp.

In addition I have written a small javascript file, that gets included 
into every page. This one I store inside a gallery. When the user is not 
yet logged in, he can just press CTRL+SPACE in order to get to the 
frontend login page. After logging in, the frontend login page redirects 
them to the page, where they came from, but now they are in direct edit 
mode and inside the offline project. Of course every user will get this 
piece of javascript, but the code is short and I use some javascript 
anyway for browsers that support it.

Now, if you are in direct edit mode, then opencms *itself* will use the 
key combination CTRL+SPACE in order to allow you to switch the edit 
buttons on and off. If you include frontendlogin.js, BEFORE you insert 
the <cms:editable /> then this feature will still work. In this way the 
users can login with CTRL+SPACE and then toggle editing also with 
CTRL+SPACE.

Inside the "javascript" file (which is actually a JSP with the "export" 
property set to "true"), you of course have to modify the 
<cms:link>/system/modules/com.my.module/frontendlogin.jsp</cms:link> - 
part. This link has to point to the location where the frontendlogin.jsp 
file is stored in your system. The frontendlogin.jsp - file should not 
need any changes, unless you want to make it a bit nicedr

Have fun!
Christian

===frontendlogin.jsp====================================================
 cms.getCmsObject() -->
<%@ 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");

        //form not submitted? => show login form.
        if((user==null)||(user.length()==0)) {
          %><html><head><title>Frontend login</title></head><body 
onload="document.forms[0].elements[0].focus()"><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><%

    } else {

      CmsObject cmsObject = cms.getCmsObject();
      cmsObject.loginUser(user, password);
      CmsProject cmsproject = cmsObject.readProject("Offline");
      cmsObject.getRequestContext().setCurrentProject(cmsproject);
      cmsObject.getRequestContext().setSiteRoot("/sites/default");
 
     if( url!=null && url.length()>0 )
        response.sendRedirect(url);
      else
        response.sendRedirect(cms.link("/index.html"));
    
%>



=== frontendlogin.js (this is actually a JSP file, because I need JSP in 
order to link to the frontendlogin.jsp - page) ===================
<%@ 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>/system/modules/com.my.module/frontendlogin.jsp</cms:link>?url="+escape(location.href);
}

ocument.onkeyup=frontLogin;
ba_initFontSize();




More information about the opencms-dev mailing list