akash-gupta2813 commented on code in PR #1290:
URL: https://github.com/apache/ranger/pull/1290#discussion_r4208081251


##########
security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java:
##########
@@ -1217,55 +1219,28 @@ public RESTResponse 
grantAccess(@PathParam("serviceName") String serviceName, Gr
 
                         ensureAdminAccess(policy, userName);
 
+                        
ServiceRESTUtil.preserveExistingPolicyGuidForGrantUpdate(svcStore, policy);
                         svcStore.updatePolicy(policy);
                     } else {
                         LOG.error("processGrantRequest processing failed");
 
                         throw new Exception("processGrantRequest processing 
failed");
                     }
                 } else {
-                    policy = new RangerPolicy();
-
-                    policy.setService(serviceName);
-                    policy.setName("grant-" + System.currentTimeMillis()); // 
TODO: better policy name
-                    policy.setDescription("created by grant");
-                    policy.setIsAuditEnabled(grantRequest.getEnableAudit());
-                    policy.setCreatedBy(userName);
-
-                    Map<String, RangerPolicyResource> policyResources = new 
HashMap<>();
-                    Set<String>                       resourceNames   = 
resource.getKeys();
-
-                    if (!CollectionUtils.isEmpty(resourceNames)) {
-                        for (String resourceName : resourceNames) {
-                            policyResources.put(resourceName, 
getPolicyResource(resource.getValue(resourceName), grantRequest));
-                        }
-                    }
-
-                    policy.setResources(policyResources);
-
-                    RangerPolicyItem policyItem = new RangerPolicyItem();
-
-                    
policyItem.setDelegateAdmin(grantRequest.getDelegateAdmin());
-                    policyItem.addUsers(grantRequest.getUsers());
-                    policyItem.addGroups(grantRequest.getGroups());
-                    policyItem.addRoles(grantRequest.getRoles());
-
-                    for (String accessType : grantRequest.getAccessTypes()) {
-                        policyItem.addAccess(new 
RangerPolicyItemAccess(accessType, Boolean.TRUE));
-                    }
-
-                    policy.addPolicyItem(policyItem);
-                    policy.setZoneName(zoneName);
-
-                    ensureAdminAccess(policy, userName);
-
-                    svcStore.createPolicy(policy);
+                    createRangerPolicy(serviceName, userName, zoneName, 
resource, grantRequest);
                 }
             } catch (WebApplicationException excp) {
                 throw excp;
             } catch (Throwable excp) {
                 LOG.error("grantAccess({}, {}) failed", serviceName, 
grantRequest, excp);
 
+                if (ServiceRESTUtil.isXPolicyUniqueConstraintViolation(excp)) {

Review Comment:
   made it generic for create/update.



-- 
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