Custom Login Page

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
(frontendlogin.jsp)
 
(5 intermediate revisions by 5 users not shown)
Line 16: Line 16:
  
 
<code lang="java">
 
<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");
Line 52: Line 48:
 
//no user submitted or login failed => show login form
 
//no user submitted or login failed => show login form
 
if(user==null || user.length()==0 || loginFailed) {
 
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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
<html>
+
 
<head>
 
<head>
 
<title>Frontend login</title>
 
<title>Frontend login</title>

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