[opencms-dev] Create a user through a jsp page with OpenCms6

Thomas Kiesl thomas.kiesl at bluemars.net
Mon Oct 23 16:16:18 CEST 2006


Hi Michele,

sorry the late answer. I hope this code will help you:

<%@ page session="false" %>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>

<%@ page import="org.opencms.jsp.CmsJspActionElement,
	org.opencms.file.CmsObject,
	org.opencms.file.CmsUser,
	java.sql.*,
	java.io.*,
	java.util.Hashtable"
%>
<%
String firstName = request.getParameter("fname");
String lastName = request.getParameter("lname");
String userName = request.getParameter("uname");
String email = request.getParameter("email");
String password1 = request.getParameter("pw1");
String password2 = request.getParameter("pw2");
String group = "Guests";
CmsJspActionElement cmsjsp =
new CmsJspActionElement( pageContext, request, response );

CmsObject cms = cmsjsp.getCmsObject();

// simple validation
if ( userName == null || "".equals(userName) ) {
throw new Exception( "User Name is required." );
} else if ( email == null || "".equals(email) ) {
throw new Exception( "Email is required." );
} else if ( password1 == null || "".equals( password1 ) ) {
throw new Exception( "Password is required." );
} else if ( password2 == null || "".equals( password2 ) ) {
throw new Exception( "Password Again is required." );
} else if ( email  == null || "".equals( email ) ) {
throw new Exception( "Email address must be valid." );
} else if ( !password1.equals( password2 )) {
throw new Exception( "Passwords do not match." );
} else if ( password1.length() < 7 ) {
throw new Exception(
"Password must be at least 7 characters." );
}
// Set default values for optional params.
if ( firstName == null ) {
firstName = "";
}



group = "Guests";


if ( lastName == null ) {
lastName = "";
}
// Hashtable for custom parameters
Hashtable params = new Hashtable();
params.put("test",new Timestamp(System.currentTimeMillis()));

CmsUser user = null;
try {
	user = cms.addWebUser( userName, // Username
	password1, // Password
	group, // Default Group
	"Web User", // Comment
	params // Params Hashtable
	);
	user.setFirstname( firstName );
	user.setLastname( lastName );
	user.setEmail( email );
	cms.writeWebUser( user ); // Write changes to DB.
}
catch (Exception e) {
	e.printStackTrace(new PrintWriter(out));
}
%>

Best regards

Thomas

Bortolotti Michele wrote:
> Hi everybody,
> does enyone have ever had to create a User using jsp code? What classes 
> do I have to use?
>  
> Many thanks,
> Michele.
> 
> 
> ------------------------------------------------------------------------
> 
> 
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/mailman/listinfo/opencms-dev


-- 
Mit freundlichen Grüßen

Thomas Kiesl

--
BLUE MARS - Gesellschaft fuer digitale Kommunikation mbH

Thomas Kiesl               mailto:thomas.kiesl at bluemars.net
Software Developer         http://www.bluemars.net
Ebersheimstraße 5          T +49.69.469973-0
60320 Frankfurt am Main    F +49.69.469973-99



More information about the opencms-dev mailing list