تاريخ آخر تحديث
6 مايو 2021
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
Applies to
CQ5.1, CQ5.2