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

Za ta izdelek je bila izdana neprenosljiva licenca za priznanje avtorstva, nekomercialno uporabo in skupno rabo pod enakimi pogoji 3.0  Objav v storitvah Twitter™ in Facebook ne urejajo pogoji pogodbe Creative Commons.

Pravna obvestila   |   Pravilnik o zasebnosti v spletu