88 */
99package biz .netcentric .cq .tools .actool .helper ;
1010
11- import static biz .netcentric .cq .tools .actool .history .PersistableInstallationLogger .msHumanReadable ;
12-
13- import java .util .Iterator ;
14- import java .util .Set ;
15-
16- import javax .jcr .AccessDeniedException ;
1711import javax .jcr .Node ;
1812import javax .jcr .NodeIterator ;
19- import javax .jcr .PathNotFoundException ;
2013import javax .jcr .RepositoryException ;
2114import javax .jcr .Session ;
22- import javax .jcr .UnsupportedRepositoryOperationException ;
23- import javax .jcr .lock .LockException ;
2415import javax .jcr .query .Query ;
2516import javax .jcr .query .QueryResult ;
26- import javax .jcr .security .AccessControlEntry ;
27- import javax .jcr .security .AccessControlException ;
2817import javax .jcr .security .AccessControlManager ;
2918import javax .jcr .security .AccessControlPolicy ;
30- import javax .jcr .version .VersionException ;
3119
3220import org .apache .commons .lang3 .StringUtils ;
33- import org .apache .commons .lang3 .time .StopWatch ;
34- import org .apache .jackrabbit .api .security .JackrabbitAccessControlList ;
35- import org .apache .sling .api .resource .Resource ;
36- import org .apache .sling .api .resource .ResourceResolver ;
3721import org .slf4j .Logger ;
3822import org .slf4j .LoggerFactory ;
3923
4024public class PurgeHelper {
4125 public static final Logger LOG = LoggerFactory .getLogger (PurgeHelper .class );
4226
43-
44- public static String purgeACLs (final Session session , final String path )
45- throws Exception {
27+ public static String purgeACLs (final Session session , final String path ) throws RepositoryException {
4628
4729 StringBuilder message = new StringBuilder ();
4830 if (StringUtils .isNotBlank (path )) {
4931 String queryString = "/jcr:root" + path .trim () + "//rep:policy" ;
50- Query query = session .getWorkspace ().getQueryManager ()
51- .createQuery (queryString , Query .XPATH );
32+ Query query = session .getWorkspace ().getQueryManager ().createQuery (queryString , Query .XPATH );
5233 QueryResult result = query .execute ();
5334 NodeIterator nodeIterator = result .getNodes ();
5435
55- AccessControlManager accessManager = session
56- .getAccessControlManager ();
36+ AccessControlManager accessManager = session .getAccessControlManager ();
5737
5838 while (nodeIterator .hasNext ()) {
5939 Node res = nodeIterator .nextNode ().getParent ();
6040 if (res != null ) {
61- AccessControlPolicy [] policies = accessManager
62- .getPolicies (res .getPath ());
41+ AccessControlPolicy [] policies = accessManager .getPolicies (res .getPath ());
6342 for (int j = 0 ; j < policies .length ; j ++) {
6443 accessManager .removePolicy (res .getPath (), policies [j ]);
6544 }
66- message .append ("Removed all policies from node "
67- + res .getPath () + ".\n " );
45+ message .append ("Removed all policies from node " + res .getPath () + ".\n " );
6846 }
6947 }
70- message .append ("\n \n Completed removing ACLs from path: " + path
71- + " and it's subpaths!" );
48+ message .append ("\n \n Completed removing ACLs from path: " + path + " and it's subpaths!" );
7249 }
7350
7451 session .save ();
@@ -77,98 +54,18 @@ public static String purgeACLs(final Session session, final String path)
7754 }
7855
7956 public static void purgeAcl (final Session session , final String path )
80- throws Exception {
57+ throws RepositoryException {
8158
8259 if (StringUtils .isNotBlank (path )) {
83- AccessControlManager accessManager = session
84- .getAccessControlManager ();
60+ AccessControlManager accessManager = session .getAccessControlManager ();
8561 Node node = session .getNode (path );
8662
87- AccessControlPolicy [] policies = accessManager .getPolicies (node
88- .getPath ());
63+ AccessControlPolicy [] policies = accessManager .getPolicies (node .getPath ());
8964 for (int i = 0 ; i < policies .length ; i ++) {
9065 accessManager .removePolicy (node .getPath (), policies [i ]);
91- AcHelper .LOG .info ("Removed all policies from node "
92- + node .getPath () + ".\n " );
66+ LOG .info ("Removed all policies from node {}" , node .getPath ());
9367 }
9468 }
9569 }
9670
97- public static void purgeACLs (final ResourceResolver resourceResolver ,
98- final String [] paths ) throws Exception {
99- Session session = resourceResolver .adaptTo (Session .class );
100-
101- for (int i = 0 ; i < paths .length ; i ++) {
102- if (StringUtils .isNotBlank (paths [i ])) {
103- String query = "/jcr:root" + paths [i ].trim () + "//rep:policy" ;
104- Iterator <Resource > results = resourceResolver .findResources (
105- query , Query .XPATH );
106- AccessControlManager accessManager = session
107- .getAccessControlManager ();
108-
109- while (results .hasNext ()) {
110- Resource res = results .next ().getParent ();
111- if (res != null ) {
112- AccessControlPolicy [] policies = accessManager
113- .getPolicies (res .getPath ());
114- for (int j = 0 ; j < policies .length ; j ++) {
115- accessManager .removePolicy (res .getPath (),
116- policies [j ]);
117- }
118- }
119- }
120- }
121- }
122- session .save ();
123- }
124-
125- public static String deleteAcesForPrincipalIds (final Session session ,
126- final Set <String > principalIds , final Set <AclBean > aclBeans )
127- throws UnsupportedRepositoryOperationException ,
128- RepositoryException , AccessControlException , PathNotFoundException ,
129- AccessDeniedException , LockException , VersionException {
130-
131- StopWatch sw = new StopWatch ();
132- sw .start ();
133- StringBuilder message = new StringBuilder ();
134- AccessControlManager aMgr = session .getAccessControlManager ();
135- long aceCounter = 0 ;
136-
137- for (AclBean aclBean : aclBeans ) {
138- if (aclBean == null ) {
139- continue ;
140- }
141-
142- JackrabbitAccessControlList acl = aclBean .getAcl ();
143- for (AccessControlEntry ace : acl .getAccessControlEntries ()) {
144- String principalId = ace .getPrincipal ().getName ();
145- if (principalIds .contains (principalId )) {
146- String parentNodePath = aclBean .getParentPath ();
147- acl .removeAccessControlEntry (ace );
148- boolean aclEmpty = acl .isEmpty ();
149- if (!aclEmpty ) {
150- aMgr .setPolicy (aclBean .getParentPath (), acl );
151- } else {
152- aMgr .removePolicy (aclBean .getParentPath (), acl );
153- }
154-
155- String msg = "Path " + parentNodePath + ": Removed entry for '" + principalId + "' from ACL "
156- + (aclEmpty ? " (and the now emtpy ACL itself)" : "" );
157- LOG .info (msg );
158- message .append (msg + "\n " );
159- aceCounter ++;
160- }
161- }
162-
163- }
164- sw .stop ();
165- String executionTime = msHumanReadable (sw .getTime ());
166- String resultMsg = (aceCounter > 0 )
167- ? "Deleted " + aceCounter + " ACEs for " + principalIds .size () + " principals in " + executionTime
168- : "Did not delete any ACEs" ;
169- message .append (resultMsg + "\n " );
170- LOG .debug (resultMsg );
171-
172- return message .toString ();
173- }
17471}
0 commit comments