Manage role

Some WEB Application must display fields swith user (connected) role. That's to say, in according to user role, some fields must be READ-WRITE, READ-ONLY, INVISIBLE... With FormView you can manage behaviour fields according to role.

Role defintion

Before integrate role with FormView, you must design your role strategy. What do you want to do, when user has role or not?

Take an example. Classic case is to have :

  • READ-WRITE field, if user has role ADMIN_ROLE.
  • READ-ONLY field, if user has role USER_ROLE
  • INVISIBLE field, if user has NO role.

To design this role definition, you must describe role definition into your XML form-view config like this :

  ....
  <!-- Roles defintion -->
  <roles-definition>  
        <role-definition name="RW_RO_I_ROLE" 
                         behaviours="READ-WRITE,READ-ONLY,INVISIBLE" />                                  
  </roles-definition>  
  ...

After you use this role definition for a managerId field, you do like this :

  ....  
  <form name="/project" >
      <!-- Manager is READ-WRITE if user has role ADMIN_ROLE
           or READ-ONLY if user has role USER_ROLE otherwise it is INVISIBLE -->
      <field property="managerId" roles="ADMIN_ROLE,USER_ROLE" roleDefinitionName="RW_RO_I_ROLE" />
  </form>
  ....

To avoid, declare roleDefinitionName for each field, you can set default="true" into your role defintion, like this :

  ....
  <!-- Roles defintion -->
  <roles-definition>  
        <role-definition name="RW_RO_I_ROLE" 
                         behaviours="READ-WRITE,READ-ONLY,INVISIBLE" 
                         default="true"/>                                        
  </roles-definition>  
  ...

So in your form description, you can use role definition without declare roleDefinitionName each time you want map field with role defintion :

  ....  
  <form name="/project" >
      <!-- Manager is READ-WRITE if user has role ADMIN_ROLE
           or READ-ONLY if user has role USER_ROLE otherwise it is INVISIBLE -->
      <field property="managerId" roles="ADMIN_ROLE,USER_ROLE" />
  </form>
  ....

At end, you must define which behaviour must be overloaded between role behaviour and field behaviour. Imagine, you have form on READ state. All your fields must be READ-ONLY, but user has role ADMIN_ROLE. managerId field must be READ-WRITE (because user has role ADMIN_ROLE) or READ-ONLY (because form is on READ state) ?

If you want, that your field must be READ-ONLY whatever user role, you must overload behaviour with field. Use overloaded-by="field" in your role defintion, like this :

  ....
  <!-- Roles defintion -->
  <roles-definition>  
        <role-definition name="RW_RO_I_ROLE" 
                         behaviours="READ-WRITE,READ-ONLY,INVISIBLE" 
                         overloaded-by="field"
                         default="true"/>                                        
  </roles-definition>  
  ...

If you want, that your field must be READ-WRITE whatever behaviour of your field, you must overload behaviour with role. Use overloaded-by="role" in your role defintion, like this :

  ....
  <!-- Roles defintion -->
  <roles-definition>  
        <role-definition name="RW_RO_I_ROLE" 
                         behaviours="READ-WRITE,READ-ONLY,INVISIBLE" 
                         overloaded-by="role"
                         default="true"/>                                        
  </roles-definition>  
  ...

By default default="true", role defintion used by FormView is the role defintion with behaviours READ-WRITE,READ-ONLY,INVISIBLE, and overloaded-by="field". So if you want use this role definition, you have not to declare role defintion in your XML form-view config.

Permission Adapter

To manage your role, you must implement method getBehaviour of interface IPermissionsAdapter. This method must return behaviour swith roles field, default behaviour and role defintion.

public interface IPermissionsAdapter {
        
        public String getBehaviour(FieldView field, String defaultBehaviour, RoleDefinition roleDefinition);
}

FormView has abstract class AbstractRolesPermissionsAdapter which implements this interface, to manage role. Method hasRole, must be implement :

public abstract class AbstractRolesPermissionsAdapter implements IPermissionsAdapter {
  
  public abstract boolean hasRole(String role);
  
}

By default, FormView implements this abstract class, RolesPermissionsAdapter which search role into request (see UserPrincipal).

  public boolean hasRole(String role) {
        return request.isUserInRole(role);
  }    
  

If you want use your own implementation of your Permision Adapter, implement interface IPermissionsAdapter and to use it, do like this :

  MyRolePermisionAdapter permission = new MyRolePermisionAdapter(request);
  // Save Role Permission Adapter into request
  WEBFormViewUtil.savePermissionAdapter(request, permission);