Monday, November 21, 2016

Setting XPages ACL Dynamically

The ACL on the XPage can be set from the XPage's - All Poperties - ACL property manually, by defining each ACLEntry directly on the XPage, but there's no way ( as far a s I know ) to bind this to a java bean or to have this computed on the fly.

A workaround is to add this to the XPage's view root.
This is done by calling a function in java from the beforePageLoad event of the XPage you want to restrict...

 public void setACLOnXPage() {
  ACLEntry aclEntry = null;
  ArrayList arList = null;
  ACL aclObject = new ACL();
  try {
   
   arList = new ArrayList();
   
   // USER
   aclEntry = new ACLEntry();
   aclEntry.setFullName("My UserName"); // FirstName SurName
   aclEntry.setName("My User Cannonical Name"); // CN=FirstName SurName/O=Organization
   aclEntry.setType("USER");
   aclEntry.setRight("EDITOR");
   arList.add(aclEntry);
   
   // DEFAULT
   aclEntry = new ACLEntry();
   aclEntry.setName("DEFAULT");
   aclEntry.setFullName("DEFAULT");
   aclEntry.setType("DEFAULT");
   aclEntry.setRight("NOACCESS");
   arList.add(aclEntry);

   // ANONYMOUS
   aclEntry = new ACLEntry();
   aclEntry.setName("ANONYMOUS");
   aclEntry.setFullName("ANONYMOUS");
   aclEntry.setType("ANONYMOUS");
   aclEntry.setRight("NOACCESS");
   arList.add(aclEntry);

   // ROLES
   aclEntry = new ACLEntry();
   aclEntry.setName("[ADMINISTRATION]");
   aclEntry.setFullName("ADMINISTRATION");
   aclEntry.setType("ROLE");
   aclEntry.setRight("EDITOR");
   arList.add(aclEntry);
   
   // GROUP
   aclEntry = new ACLEntry();
   aclEntry.setName("MY GROUP");
   aclEntry.setFullName("MY GROUP");
   aclEntry.setType("GROUP");
   aclEntry.setRight("EDITOR");
   arList.add(aclEntry);
   
   for (ACLEntry entry : arList) {
    aclObject.addEntry(entry);
   }
  
   aclObject.setTransient(false);
   aclObject.saveState(FacesContext.getCurrentInstance());
   
   UIViewRootEx2 view = (com.ibm.xsp.component.UIViewRootEx2) FacesContext.getCurrentInstance().getViewRoot();
   view.setAcl(aclObject);
   
   
  }catch(Exception ex) {
   System.out.print(ex);
  }
 }
Hope this helps! ;-)