Chapter 7. Nuxeo RCP extension points

Table of Contents

7.1. Customize user attributes in the add/edit/view user forms
7.1.1. Different configurations
7.1.2. Contribute to the extension point

7.1. Customize user attributes in the add/edit/view user forms

A user form in Nuxeo is defined by a specific schema with specific attributes. Most of the time, this schema won't changed but in some situation, Nuxeo may have been customised with other attributes or new ones.

The easy and current way to customize this schema in the RCP so that it reflects with the one in the Server is to use the extension point "org.nuxeo.ecm.rcp.usermanager.userFormsContributor".

7.1.1. Different configurations

At the moment, the only attributes that won't be touched are username and password. It has been told that these attribute are must be present in every ECM application for a user. For the other attributes, it's really up to you and the existing customized Nuxeo user schema on the server side.

If you just want to add attributes to the default schema, you'll just have to create a new plugin that extend the extension point and contribute new attributes from there.

If you don't want to use the default configuration, you can disable the fragment "org.nuxeo.ecm.rcp.usermanager.userformcontribution" which contain an extension with the default attributes. You'll will then create brain new plugin that contains ALL the attributes of the user (except username and password of course).

7.1.2. Contribute to the extension point

Each "org.nuxeo.ecm.rcp.usermanager.userFormsContributor" extension will provide a class that implements "org.nuxeo.ecm.usermanager.utils.IUserFormContributor". This class will define the way forms will retrieve data, display data and update data:

  • viewFormAddFields() will provide Fields associated with their key to the view form.

  • editFormAddFields() will provide Fields associated with their key to the edit and add form.

  • loadFromObject() will load data and provide it to the form.

  • saveToObject() will save data from the form.

  • validate() will return a message if there is a problem in the filled form

You may look at the javadoc comments of IUserFormContributor for more details. Here is the default configuration you can use as a template for new contributor:

package org.nuxeo.ecm.rcp.usermanager.userformcontribution;

import java.util.ArrayList; import java.util.Arrays;
import java.util.List; import org.nuxeo.ecm.core.api.NuxeoPrincipal;
import org.nuxeo.ecm.usermanager.utils.FieldItem;
import org.nuxeo.ecm.usermanager.utils.IUserFormContributor;
import org.nuxeo.ecm.usermanager.utils.UserManagerHelper;
import org.nuxeo.forms.utils.Form;
import org.nuxeo.forms.utils.LabelField;
import org.nuxeo.forms.utils.ListField;
import org.nuxeo.forms.utils.TextField;

public class DefaultUserFormContributor implements IUserFormContributor {
 public static final String FIRSTNAME = "firstname"; //$NON-NLS-1$ 
 public static final String LASTNAME = "lastname"; //$NON-NLS-1$ 
 public static final String COMPANY = "company"; //$NON-NLS-1$ 
 public static final String EMAIL = "email"; //$NON-NLS-1$ 
 public static final String GROUPS = "groups"; //$NON-NLS-1$ 

 public List<FieldItem> viewFormAddFields() { 
  ArrayList<FieldItem> list = new ArrayList<FieldItem>(); 
  list.add(new FieldItem(LASTNAME, new LabelField( Messages.DefaultUserFormContributor_lastname))); 
  list.add(new FieldItem(FIRSTNAME, new LabelField( Messages.DefaultUserFormContributor_firstname))); 
  list.add(new FieldItem(COMPANY, new LabelField( Messages.DefaultUserFormContributor_company))); 
  list.add(new FieldItem(EMAIL, new LabelField( Messages.DefaultUserFormContributor_email))); 
  list.add(new FieldItem(GROUPS, new LabelField( Messages.DefaultUserFormContributor_groups, UserManagerHelper.getGroups()))); 
  return list;
 } 

 public List<FieldItem> editFormAddFields() {
  ArrayList<FieldItem> list = new ArrayList<FieldItem>(); 
  list.add(new FieldItem(LASTNAME, new TextField( Messages.DefaultUserFormContributor_lastname))); 
  list.add(new FieldItem(FIRSTNAME, new TextField( Messages.DefaultUserFormContributor_firstname))); 
  list.add(new FieldItem(COMPANY, new TextField( Messages.DefaultUserFormContributor_company))); 
  list.add(new FieldItem(EMAIL, new TextField( Messages.DefaultUserFormContributor_email))); 
  list.add(new FieldItem(GROUPS, new ListField( Messages.DefaultUserFormContributor_groups, UserManagerHelper.getGroups())));
  return list;
 } 

 public void loadFromObject(NuxeoPrincipal principal, Form form) { 
  form.set(LASTNAME, principal.getLastName()); 
  form.set(FIRSTNAME, principal.getFirstName()); 
  form.set(COMPANY, principal.getCompany()); 
  form.set(EMAIL, principal.getModel().getProperty("user", EMAIL)); //$NON-NLS-1$
  List<String> listGroup = principal.getGroups(); 
  String s[] = listGroup.toArray(new String[listGroup.size()]);
  form.set(GROUPS, s);
 } 

 public void saveToObject(NuxeoPrincipal principal, Form form) {
  principal.setLastName((String) form.get(LASTNAME)); 
  principal.setFirstName((String) form.get(FIRSTNAME)); 
  principal.setCompany((String) form.get(COMPANY)); 
  principal.getModel().setProperty( "user", EMAIL, (String) form.get(EMAIL)); //$NON-NLS-1$ 
  String[] strings = (String[]) form.get(GROUPS); 
  if (strings != null) { 
   principal.setGroups(Arrays.asList(strings));
  } 
 } 

 public String validate(Form form) { 
  String email = (String) form.get(EMAIL); 
  if (email == null || email.trim().length() == 0) {
   return Messages.DefaultUserFormContributor_validationEmail;
  }
  return null; 
 }
}