27
27
import com .cognifide .apm .api .status .Status ;
28
28
import com .cognifide .apm .main .utils .MessagingUtils ;
29
29
import com .cognifide .apm .main .utils .PathUtils ;
30
+ import java .util .HashSet ;
31
+ import java .util .List ;
32
+ import java .util .Set ;
30
33
import javax .jcr .Node ;
31
34
import javax .jcr .NodeIterator ;
32
35
import javax .jcr .RepositoryException ;
36
+ import javax .jcr .security .AccessControlPolicy ;
33
37
import org .apache .commons .lang3 .StringUtils ;
34
38
import org .apache .jackrabbit .api .JackrabbitSession ;
39
+ import org .apache .jackrabbit .api .security .JackrabbitAccessControlEntry ;
40
+ import org .apache .jackrabbit .api .security .JackrabbitAccessControlManager ;
35
41
import org .apache .jackrabbit .api .security .user .Authorizable ;
42
+ import org .apache .jackrabbit .oak .api .Type ;
43
+ import org .apache .jackrabbit .oak .spi .security .authorization .accesscontrol .ACE ;
44
+ import org .apache .jackrabbit .oak .spi .security .authorization .accesscontrol .AbstractAccessControlList ;
36
45
import org .apache .jackrabbit .oak .spi .security .authorization .permission .PermissionConstants ;
46
+ import org .apache .jackrabbit .oak .spi .security .authorization .restriction .Restriction ;
37
47
import org .slf4j .Logger ;
38
48
import org .slf4j .LoggerFactory ;
39
49
@@ -45,7 +55,7 @@ public class Purge implements Action {
45
55
46
56
private final String path ;
47
57
48
- public Purge (final String path ) {
58
+ public Purge (String path ) {
49
59
this .path = path ;
50
60
}
51
61
@@ -55,19 +65,19 @@ public ActionResult simulate(Context context) {
55
65
}
56
66
57
67
@ Override
58
- public ActionResult execute (final Context context ) {
68
+ public ActionResult execute (Context context ) {
59
69
return process (context , true );
60
70
}
61
71
62
- private ActionResult process (final Context context , boolean execute ) {
72
+ private ActionResult process (Context context , boolean execute ) {
63
73
ActionResult actionResult = context .createActionResult ();
64
74
try {
65
75
Authorizable authorizable = context .getCurrentAuthorizable ();
66
76
actionResult .setAuthorizable (authorizable .getID ());
67
77
if (context .isCompositeNodeStore () && PathUtils .isAppsOrLibsPath (path )) {
68
78
actionResult .changeStatus (Status .SKIPPED , "Skipped purging privileges for " + authorizable .getID () + " on " + path );
69
79
} else {
70
- LOGGER .info (String .format ("Purging privileges for authorizable with id = %s under path = %s" ,
80
+ LOGGER .info (String .format ("Purging privileges for authorizable with id= %s under path= %s" ,
71
81
authorizable .getID (), path ));
72
82
if (execute ) {
73
83
purge (context , actionResult );
@@ -81,23 +91,18 @@ private ActionResult process(final Context context, boolean execute) {
81
91
return actionResult ;
82
92
}
83
93
84
- private void purge (final Context context , final ActionResult actionResult )
94
+ private void purge (Context context , ActionResult actionResult )
85
95
throws RepositoryException , ActionExecutionException {
86
- NodeIterator iterator = getPermissions (context );
96
+ Set < String > accessControlledPaths = getAccessControlledPaths (context );
87
97
String normalizedPath = normalizePath (path );
88
- while (iterator != null && iterator .hasNext ()) {
89
- Node node = iterator .nextNode ();
90
- if (node .hasProperty (PermissionConstants .REP_ACCESS_CONTROLLED_PATH )) {
91
- String parentPath = node .getProperty (PermissionConstants .REP_ACCESS_CONTROLLED_PATH )
92
- .getString ();
93
- String normalizedParentPath = normalizePath (parentPath );
94
- boolean isUsersPermission = parentPath .startsWith (context .getCurrentAuthorizable ().getPath ());
95
- if (StringUtils .startsWith (normalizedParentPath , normalizedPath ) && !isUsersPermission ) {
96
- RemoveAll removeAll = new RemoveAll (parentPath );
97
- ActionResult removeAllResult = removeAll .execute (context );
98
- if (Status .ERROR .equals (removeAllResult .getStatus ())) {
99
- copyErrorMessages (removeAllResult , actionResult );
100
- }
98
+ for (String parentPath : accessControlledPaths ) {
99
+ String normalizedParentPath = normalizePath (parentPath );
100
+ boolean isUsersPermission = parentPath .startsWith (context .getCurrentAuthorizable ().getPath ());
101
+ if (StringUtils .startsWith (normalizedParentPath , normalizedPath ) && !isUsersPermission ) {
102
+ RemoveAll removeAll = new RemoveAll (parentPath );
103
+ ActionResult removeAllResult = removeAll .execute (context );
104
+ if (Status .ERROR .equals (removeAllResult .getStatus ())) {
105
+ copyErrorMessages (removeAllResult , actionResult );
101
106
}
102
107
}
103
108
}
@@ -111,14 +116,33 @@ private void copyErrorMessages(ActionResult from, ActionResult to) {
111
116
}
112
117
}
113
118
114
- private NodeIterator getPermissions (Context context )
119
+ private Set < String > getAccessControlledPaths (Context context )
115
120
throws ActionExecutionException , RepositoryException {
121
+ Set <String > result = new HashSet <>();
116
122
JackrabbitSession session = context .getSession ();
117
123
String path = PERMISSION_STORE_PATH + context .getCurrentAuthorizable ().getID ();
118
- NodeIterator result = null ;
119
124
if (session .nodeExists (path )) {
120
125
Node node = session .getNode (path );
121
- result = node .getNodes ();
126
+ NodeIterator nodes = node .getNodes ();
127
+ while (nodes .hasNext ()) {
128
+ node = nodes .nextNode ();
129
+ if (node .hasProperty (PermissionConstants .REP_ACCESS_CONTROLLED_PATH )) {
130
+ result .add (node .getProperty (PermissionConstants .REP_ACCESS_CONTROLLED_PATH ).getString ());
131
+ }
132
+ }
133
+ } else {
134
+ JackrabbitAccessControlManager accessControlManager = (JackrabbitAccessControlManager ) session .getAccessControlManager ();
135
+ AccessControlPolicy [] accessControlPolicies = accessControlManager .getPolicies (context .getCurrentAuthorizable ().getPrincipal ());
136
+ for (AccessControlPolicy accessControlPolicy : accessControlPolicies ) {
137
+ AbstractAccessControlList abstractAccessControlList = (AbstractAccessControlList ) accessControlPolicy ;
138
+ List <? extends JackrabbitAccessControlEntry > jackrabbitAccessControlEntries = abstractAccessControlList .getEntries ();
139
+ for (JackrabbitAccessControlEntry jackrabbitAccessControlEntry : jackrabbitAccessControlEntries ) {
140
+ Set <Restriction > restrictions = ((ACE ) jackrabbitAccessControlEntry ).getRestrictions ();
141
+ for (Restriction restriction : restrictions ) {
142
+ result .add (restriction .getProperty ().getValue (Type .STRING ));
143
+ }
144
+ }
145
+ }
122
146
}
123
147
return result ;
124
148
}
0 commit comments