Custom User Object

Sometimes, using the default User object is enough for an app, but most of the time a custom User object is needed, which has specialized fields and does not necessarily need most of the fields existant on User (address, phone number etc etc).

In order to use a custom user object, it is necessary to create an inheritor of the platform SimpleUser object. Create an object, like you would to normally, but then specify the following :

  • Extend of Code: SimpleUSer
  • Table: m_user
  • Tray Menu: no
  • add a “type” enum attribute (you can do that via using the template editor, as you’d do normally)

Then, we add the following code to the object, to make the object really minimalistic, by hiding most of SimpleUser’s fields, and automating group attribution:

package com.simplicite.objects.Demo;

import java.util.*;
import com.simplicite.util.*;

 * Business object DemoUser
public class DemoUser extends com.simplicite.objects.System.SimpleUser {
	private static final long serialVersionUID = 1L;
	public void postLoad() {
		// hide most of the SimpleUser fields, keeping only email & login
		// hide all users that were not created throught this object
		setDefaultSearchSpec("demo_user_type is not null");
	public List<String> preValidate() {
		// set some mandatory SimpleUser fields
		setFieldValue("row_module_id", ModuleDB.getModuleId("ApplicationUsers"));
		//following does not work because usr_menu is not part of SimpleUser
		// we manage it in a postSave query to avoid adding a useless object attribute
		//setFieldValue("usr_menu", "1");
		setFieldValue("usr_active", Grant.USER_ACTIVE);
		return super.preValidate();
	public String postSave() {
		List<String> groups = new ArrayList();
			case "ADMIN":		groups.add("DEMO_ADMIN"); break;
			case "USER":		groups.add("DEMO_USER"); break;
		// meh practice... query instead of adding usr_menu attribute to objet
		getGrant().update("update m_user set usr_menu='1' where row_id="+getRowId());
		return super.postSave();
	private static void setRespList(String userId, List<String> newGroupsList){
		List<String> oldGroupsList = getRespList(userId);
		// remove old unused groups
		for(String oldGroup : oldGroupsList)
				Grant.removeResponsibility(userId, oldGroup);
		// add new missing groups
		for(String newGroup : newGroupsList)
				Grant.addResponsibility(userId, newGroup, Tool.getCurrentDate(), null, true, "ApplicationUsers");
	private static List<String> getRespList(String userId){
			return null;
		Grant g = Grant.getSystemAdmin();
		String[] groups = g.queryFirstColumn("select distinct g.grp_name from m_resp r inner join m_group as g on r.rsp_group_id=g.row_id where r.rsp_login_id="+userId);
		return groups!=null && groups.length>0 ? Arrays.asList(groups) : new ArrayList<String>();

L’enregistrement m’indique que je ne respecte pas la syntaxe. Est-ce un problème?

Bonjour Ophélie,

Non ce n’est pas un problème, effectivement dans le cas des héritages la syntaxe n’est pas respectée.

1 Like

Erreur de compilation de notre côté :’(
on a enregistré une première fois sans adapter à notre objet (qui ne s’appelle pas DemoUser). Est-ce l’origine du problème ? Il ne semble pas prendre en compte notre modif