anchela commented on a change in pull request #122:
URL: 
https://github.com/apache/sling-org-apache-sling-feature-cpconverter/pull/122#discussion_r768564410



##########
File path: 
src/main/java/org/apache/sling/feature/cpconverter/handlers/slinginitialcontent/readers/JsonReader.java
##########
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package 
org.apache.sling.feature.cpconverter.handlers.slinginitialcontent.readers;
+
+import org.apache.jackrabbit.commons.SimpleValueFactory;
+import 
org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition;
+import org.apache.sling.feature.cpconverter.accesscontrol.RestrictionProvider;
+import org.apache.sling.jcr.contentloader.ContentCreator;
+
+import javax.jcr.*;
+import javax.json.*;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class  JsonReader extends 
org.apache.sling.jcr.contentloader.internal.readers.JsonReader {
+    
+    private static final String SECURITY_PRINCIPLES = "security:principals";
+    private static final String SECURITY_ACL = "security:acl";
+ 
+    private ValueFactory factory = new SimpleValueFactory();
+
+
+    protected boolean handleSecurity(String n, Object o, ContentCreator 
contentCreator) throws RepositoryException {
+        if (SECURITY_PRINCIPLES.equals(n)) {
+            this.createPrincipals(o, contentCreator);
+        } else if (SECURITY_ACL.equals(n)) {
+            this.createAclOverride(o, contentCreator);
+        } else {
+            return false;
+        }
+        return true;
+        
+    }
+
+    private void createAclOverride(Object obj, ContentCreator contentCreator) 
throws RepositoryException {
+        if (obj instanceof JsonObject) {
+            // single ace
+            createAceOverride((JsonObject) obj, contentCreator);
+        } else if (obj instanceof JsonArray) {
+            // array of aces
+            JsonArray jsonArray = (JsonArray) obj;
+            for (int i = 0; i < jsonArray.size(); i++) {
+                Object object = jsonArray.get(i);
+                if (object instanceof JsonObject) {
+                    createAceOverride((JsonObject) object, contentCreator);
+                } else {
+                    throw new JsonException("Unexpected data type in acl 
array: " + object.getClass().getName());
+                }
+            }
+        }
+    }
+    
+    
+    
+    /**
+     * Create or update an access control entry
+     */
+    private void createAceOverride(JsonObject ace, ContentCreator 
contentCreator) throws RepositoryException {
+
+        String principalID = ace.getString("principal");
+
+        String [] grantedPrivileges = null;
+        JsonArray granted = (JsonArray) ace.get("granted");
+        if (granted != null) {
+            grantedPrivileges = new String[granted.size()];
+            for (int a=0; a < grantedPrivileges.length; a++) {
+                grantedPrivileges[a] = granted.getString(a);
+            }
+        }
+
+        String [] deniedPrivileges = null;
+        JsonArray denied = (JsonArray) ace.get("denied");
+        if (denied != null) {
+            deniedPrivileges = new String[denied.size()];
+            for (int a=0; a < deniedPrivileges.length; a++) {
+                deniedPrivileges[a] = denied.getString(a);
+            }
+        }
+
+        String order = ace.getString("order", null);
+
+        Map<String, Value> restrictionsMap = new HashMap<>();
+        Map<String, Value[]> mvRestrictionsMap = new HashMap<>();
+        Set<String> removedRestrictionNames = new HashSet<>();
+        
+        if(ace.containsKey("restrictions")){
+            JsonObject restrictions = (JsonObject) ace.get("restrictions");
+
+            Set<String> keySet = restrictions.keySet();
+            for (String rname : keySet) {
+                if (rname.endsWith("@Delete")) {
+                    //add the key to the 'remove' set.  the value doesn't 
matter and is ignored.
+                    String rname2 = rname.substring(9, rname.length() - 7);
+                    removedRestrictionNames.add(rname2);
+                } else {
+                    RestrictionDefinition rd = RestrictionProvider.get(rname);

Review comment:
       i would just parse the strings and not making any attempt to validate 
the restrictions. this is not the job of the converter and i cannot know which 
restrictions are supported by the system (e.g. AEM comes with additional custom 
restrictionprovider implementations.
   -> drop usage of RestrictionDefinition altogether

##########
File path: 
src/main/java/org/apache/sling/feature/cpconverter/handlers/slinginitialcontent/readers/JsonReader.java
##########
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package 
org.apache.sling.feature.cpconverter.handlers.slinginitialcontent.readers;
+
+import org.apache.jackrabbit.commons.SimpleValueFactory;
+import 
org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition;
+import org.apache.sling.feature.cpconverter.accesscontrol.RestrictionProvider;
+import org.apache.sling.jcr.contentloader.ContentCreator;
+
+import javax.jcr.*;
+import javax.json.*;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class  JsonReader extends 
org.apache.sling.jcr.contentloader.internal.readers.JsonReader {
+    
+    private static final String SECURITY_PRINCIPLES = "security:principals";
+    private static final String SECURITY_ACL = "security:acl";
+ 
+    private ValueFactory factory = new SimpleValueFactory();

Review comment:
       what exactly do you need the valuefactory for? i would avoid that and 
just read the string(?) value plus type information from the initial-content.
   repoinit just reads all restrictions as string values and then will convert 
it to the required type as needed (in the jcr-impl where it has a session and a 
value factory at hand)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to