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).

Configuration

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 :

  • ObjectName: <your_custom_name>
  • Extend of Code: SimpleUser
  • Table: m_user (it will NOT respect the syntax rule, you can ignore the warning in this case)
  • Tray Menu: no
  • add a “type” enum attribute (you can do that via using the template editor, as you’d do normally)

Custom code

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, add and adapt the following code to your use case:

  • the package name
  • the class name
  • the group / type correspondance in the postSave
See Code
package com.simplicite.objects.Demo;

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

/**
 * Business object DemoUser
 */
public class DemoUser extends com.simplicite.objects.System.SimpleUser {
	private static final long serialVersionUID = 1L;
	
	@Override
	public void postLoad() {
		super.postLoad();
		// hide most of the SimpleUser fields, keeping only email & login
		getField("usr_first_name").setVisible(ObjectField.VIS_HIDDEN);
		getField("usr_last_name").setVisible(ObjectField.VIS_HIDDEN);
		getField("usr_image_id").setVisible(ObjectField.VIS_HIDDEN);
		//getField("usr_email").setVisible(ObjectField.VIS_HIDDEN);
		getField("usr_lang").setVisible(ObjectField.VIS_HIDDEN);
		getField("usr_cell_num").setVisible(ObjectField.VIS_HIDDEN);
		getField("usr_active").setVisible(ObjectField.VIS_HIDDEN);
		getField("usr_home_id").setVisible(ObjectField.VIS_HIDDEN);
		getField("row_module_id").setVisible(ObjectField.VIS_HIDDEN);
		
		// hide all users that were not created throught this object
		setDefaultSearchSpec("demo_user_type is not null");

		//hide states menu
		setMenuStates(false);
	}
	
	@Override
	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();
	}
	
	@Override
	public String postSave() {
		List<String> groups = new ArrayList();
		switch(getFieldValue("demoUserType")){
			case "ADMIN":		groups.add("DEMO_ADMIN"); break;
			case "USER":		groups.add("DEMO_USER"); break;
		}
		setRespList(getRowId(),groups);
		
		// 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)
			if(!newGroupsList.contains(oldGroup))
				Grant.removeResponsibility(userId, oldGroup);
		// add new missing groups
		for(String newGroup : newGroupsList)
			if(!oldGroupsList.contains(newGroup))
				Grant.addResponsibility(userId, newGroup, Tool.getCurrentDate(), null, true, "ApplicationUsers");
	}
	
	private static List<String> getRespList(String userId){
		if(Tool.isEmpty(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>();
	}
}

Hiding states menu

To hide the states menu, we used setMenuStates(false); in the postLoad hook.

Pasted_Image_08_03_2022_11_56

Password Reset

To give your user administrators the ability to manually reset a user’s password:

  • CustomUser
    • “Functions” panel > create
      • name: resetPassword
      • type: action
      • actionName > select existant > “reserPassword”
3 Likes