How do I check user privileges?

Question / Symptoms

How do I check if a user has privilege to perform a specific action?

Answer / Resolution

To check if a user has write access to a page:

public boolean hasWriteAccess(Node pageNode, Session userSession) throws RepositoryException { 
  String path = pageNode.getPath(); 
  try { 
    userSession.checkPermission(path, "add_node,set_property"); 
    if(pageNode.hasNode(JcrConstants.JCR_CONTENT)) { 
      String contentNodePath = path + "/" + "jcr:content"; 
      userSession.checkPermission(contentNodePath, "add_node,set_property"); 
    } 
  } catch(java.security.AccessControlException e) { 
    return false; 
  } 
  return true; 
} 

To check if a user can activate pages:

final User u = resourceResolver.adaptTo(User.class); 
// alternatively: final User u = SecurityUtil.resolveUser(session); 
return u.hasPermission(Replicator.REPLICATION_PERMISSION); 

Reference

See 5.2 API for User

Applies to

CQ5.1, CQ5.2

 Adobe

Get help faster and easier

New user?