AEM 6.5 - Adding JCR access policy NOT CUG | Community
Skip to main content
Level 4
May 21, 2020
Solved

AEM 6.5 - Adding JCR access policy NOT CUG

  • May 21, 2020
  • 1 reply
  • 5229 views

I got stuck on CUG and this is NOT what I am looking for. 

 

We require to add jcr:write access for a single user to a new node that we create also through code.  All I can find is code examples prior 6.3 change to CUG, but these are no longer working

 

How can we still use AccessControlList.addEntry(principal, privileges, true) in AEM 6.5 or is this no longer allowed and should we user CUG?

 

(I understand this only is used to grant Read access to nodes)

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Vijayalakshmi_S

Hi @eric_stricker,

Use AccessControlList.addAccessControlEntry(principal, privileges) followed by setPolicy using AccessControlManager API.

Code snippet:

try { Authorizable authorizable = userMgr.getAuthorizable(userIdStr); Principal userPrincipal = authorizable.getPrincipal(); // prinicipal object from user/group id Privilege[] writePrivileges = new Privilege[] { acmMgr.privilegeFromName(Privilege.JCR_WRITE) }; // JCR_WRITE privilege object AccessControlPolicyIterator itr = acmMgr.getApplicablePolicies(pageNode.getPath()); // pageNode -> node for which we are trying to set policy while (itr.hasNext()) { AccessControlPolicy policy = itr.nextAccessControlPolicy(); if (policy instanceof AccessControlList) { AccessControlList acl = (AccessControlList) policy; acl.addAccessControlEntry(userPrincipal, writePrivileges); // creates ACE acmMgr.setPolicy(pageNode.getPath(), acl); // adds ACL to the desired node } } session.save(); } catch (RepositoryException e) { LOG.error("Repository Exception={}", e.getMessage()); }

Reference:

https://docs.adobe.com/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/security/AccessControlList.html#addAccessControlEntry(java.security.Principal,%20javax.jcr.security.Privilege[])

1 reply

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
May 21, 2020

Hi @eric_stricker,

Use AccessControlList.addAccessControlEntry(principal, privileges) followed by setPolicy using AccessControlManager API.

Code snippet:

try { Authorizable authorizable = userMgr.getAuthorizable(userIdStr); Principal userPrincipal = authorizable.getPrincipal(); // prinicipal object from user/group id Privilege[] writePrivileges = new Privilege[] { acmMgr.privilegeFromName(Privilege.JCR_WRITE) }; // JCR_WRITE privilege object AccessControlPolicyIterator itr = acmMgr.getApplicablePolicies(pageNode.getPath()); // pageNode -> node for which we are trying to set policy while (itr.hasNext()) { AccessControlPolicy policy = itr.nextAccessControlPolicy(); if (policy instanceof AccessControlList) { AccessControlList acl = (AccessControlList) policy; acl.addAccessControlEntry(userPrincipal, writePrivileges); // creates ACE acmMgr.setPolicy(pageNode.getPath(), acl); // adds ACL to the desired node } } session.save(); } catch (RepositoryException e) { LOG.error("Repository Exception={}", e.getMessage()); }

Reference:

https://docs.adobe.com/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/security/AccessControlList.html#addAccessControlEntry(java.security.Principal,%20javax.jcr.security.Privilege[])

Level 4
May 22, 2020

Was getting stuck on 

...

if (policy instanceof AccessControlList) {

..

The only value I could in online samples are "instanceof PrincipalSetPolicy".

 

Question: Do you know if there is a complete list of possible values for "policy instanceof ????"