Tuesday, February 11, 2014

Anonymous user sign up (part 1 of 2)

A lot of applications developed with Mendix require functionality where anonymous users can sign up to the website. In this article I'll explain how to achieve this. This article will be fairly long and that's why I will split it up in two parts. What now follows is part 1.

Ingredients

Grab your notebook!
  • A non-persistable registration entity
  • A persistable Participant entity
  • A request handler (Not familiar with request handlers?
    Definitely read Request handlers at your service!)
  • URL Redirector widget
  • Role based home page 
  • Pages
  • Microflows

Directions

First we start with  having an anonymous user to open a pop-up form with a registration dataview where he/she can fill in his/her profile data .

Sign up microflow
The user submits this data by triggering a microflow where the registration object is passed to as a parameter. This microflow takes care of populating the following entities with data entered by the user:
  •  a newly created Administration.Account object
  •  a newly created Participant object
URL Redirector
Additionaly the URL attribute in the registration entity object is set. This URL will lead to the request handler we're going to construct and will contain a parameter to uniquely identify the request.
The URL itself will be used by the URL redirector widget, which is located on the form called by the current microflow.

Request handler
The requested URL will be something like http://localhost:8080/registration/?emailaddress=[entered e-mailaddress] and a request handler has to be created in order to handle requests to '/registration/'. Register your handler with a java action:

public class RegistrationHandler extends CustomJavaAction<Boolean>
{
public RegistrationHandler(IContext context)
{
super(context);
}

@Override
public Boolean executeAction() throws Exception
{
// BEGIN USER CODE
Core.addRequestHandler("registration/", new RegistrationRequestHandler());
return true;
// END USER CODE
}
}
In this java action you can also add the RegistrationRequestHandler which is registered in the code example above.

public class RegistrationRequestHandler extends RequestHandler { 

private static final String XAS_ID = "XASID";

@Override
protected void processRequest(IMxRuntimeRequest request,
IMxRuntimeResponse response, String arg2) throws Exception {
    String curSession = request.getCookie(XAS_SESSION_ID);
   String emailaddress = request.getParameter("emailaddress");
   
   if(emailaddress != null && emailaddress!="")
   {
   ISession currentMxSession = null;
   for(ISession s : Core.getActiveSessions()) {
if(s.getUser().isAnonymous())
{
if (s.getId().toString().equals(curSession)) {
currentMxSession = s;
break;
}
}
}
   
   if(currentMxSession.getId().toString().equals(curSession)){
       IContext systemContext = Core.createSystemContext();
       ISession newSession = Core.initializeSession(Core.getUser(systemContext, emailaddress), curSession);
       response.addCookie(XAS_SESSION_ID, newSession.getId().toString(),"/" ,"" ,-1 );
       response.addCookie(XAS_ID, "0." + Core.getXASId(),"/" ,"" ,-1);
                 
   }
   response.setStatus(IMxRuntimeResponse.SEE_OTHER);
   response.addHeader("location", "../index.html");
}
}
}

That's it for part 1!

No comments:

Post a Comment