Custom consent page after first logon

The following example will show you one of the ways to implement a custom temporary page after the user has logged on for the first time.

One of the use-cases is : The user needs to agree to the terms and conditions before using the application.

1 - How it works

A newly created user will be automatically added to a temporary Group. This Group has a specific homepage that displays a form in which the connected user will be able to agree to the Terms and Conditions.
The form displayed corresponds to a record created after the user’s creation, in which we have stored the user’s id.
After clicking a button that submits the form, the user will be removed from the temporary Group and added to a relevant one.

2 - Pre-requisites

  • A Group that the users will initially be put in - e.g. MYAPP_TERMS
  • A business object MyappUsrTerms with the following fields :
  • Id of the user (link)
  • Boolean “I agree”
  • Read-only text field that will hold the Terms’ text
  • An External Object MyappTermsHome that will display the Terms acceptance form
  • An action MyappUsrTermsAccept that will call the method acceptTerms declared in the MyappUsrTerms object
  • A view MyappUsrTermsHome that displays the MyappTermsHome External Object and serves as the homepage of the MYAPP_TERMS Group

3 - Adding the user to the MYAPP_TERMS Group and creating the Terms form

Grant.addResponsibility(userId, "MYAPP_TERMS", null, null, true, "ApplicationUsers");

try {

	ObjectDB o = getGrant().getTmpObject("MyappUsrTerms");
	BusinessObjectTool ot = o.getTool();

	// Get an existing record or an empty record based on functional keys filters
	// False means no record was found => creation
	if (!ot.getForCreateOrUpdate​(new JSONObject() // or its alias getForUpsert 
		.put("myappTermsUserId", userId))) 
	{
		// Set functional keys fields
		o.setFieldValue("myappTermsUserId", userId);
	}
	
	// Set Terms text value
	o.setFieldValue("myappTermsText", "...");

	ot.validateAndSave();

} catch (Exception e) {
	AppLog.error(e, getGrant());
	// Do something with the exception
}

4 - External object MyappTermsHome code

@Override
public Object display(Parameters params) {

    try {
    	//Get the rowId of the form to display
    	String idTerms = getGrant().simpleQuery("select row_id from myapp_usr_terms where myapp_terms_usr_id="+getGrant().getUserUniqueId());
        return HTMLTool.javascriptStatement(
                "$ui.displayForm(null, 'MyappUsrTerms', " + idTerms + ", null);");
    } catch (Exception e) {
        AppLog.error(getClass(), "display", null, e, getGrant());
        return e.getMessage();
    }
}

5 - acceptTerms method

public String acceptTerms(){	
	
	boolean accepted = getField("myappTermsAccept").getBoolean();

	if(accepted){
		//The save button is hidden on the form - clicking the "Accept" button saves the record
		this.save();

		String userId = getGrant().getUserUniqueId();	
		Grant.addResponsibility(userId, MYAPP_NEW_GROUP, null, null, true, "ApplicationUsers");

		//Remove old group
		Grant.removeResponsibility(userId, "MYAPP_TERMS");

		//Reset cache 
		String sessionId = getGrant().getSessionId();
		String login = getGrant().getLogin();
		SystemTool.resetCache(login, false, false);

		//Reinit rights
		getGrant().reinit(login, sessionId, getGrant().getEndpoint(), true, "", getGrant().getSessionInfo());

		//Reload page
		return HTMLTool.javascriptStatement("parent.document.location.reload(true);");
	}
	else{
		return "Please tick the field \""+getField("myappTermsAccept").getDisplay()+"\"";
	}
	
}

6 - Screenshot MYAPP_TERMS Group’s Homepage

5 Likes