Pearl1594 commented on code in PR #11587:
URL: https://github.com/apache/cloudstack/pull/11587#discussion_r2383188745


##########
server/src/main/java/com/cloud/storage/snapshot/SnapshotManagerImpl.java:
##########
@@ -1388,28 +1389,55 @@ protected boolean deletePolicy(Long policyId) {
     }
 
     @Override
-    public Pair<List<? extends SnapshotPolicy>, Integer> 
listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) {
+    public Pair<List<? extends SnapshotPolicy>, Integer> 
listSnapshotPolicies(ListSnapshotPoliciesCmd cmd) {
         Long volumeId = cmd.getVolumeId();
-        boolean display = cmd.isDisplay();
         Long id = cmd.getId();
-        Pair<List<SnapshotPolicyVO>, Integer> result = null;
-        // TODO - Have a better way of doing this.
-        if (id != null) {
-            result = _snapshotPolicyDao.listAndCountById(id, display, null);
-            if (result != null && result.first() != null && 
!result.first().isEmpty()) {
-                SnapshotPolicyVO snapshotPolicy = result.first().get(0);
-                volumeId = snapshotPolicy.getVolumeId();
+        Account caller = CallContext.current().getCallingAccount();
+        boolean isRootAdmin = _accountMgr.isRootAdmin(caller.getId());
+        List<Long> permittedAccounts = new ArrayList<>();
+        Long domainId = null;
+        Boolean isRecursive = null;
+        ListProjectResourcesCriteria listProjectResourcesCriteria = null;
+
+        if (volumeId != null) {
+            VolumeVO volume = _volsDao.findById(volumeId);
+            if (volume != null) {
+                
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, 
volume);
             }
         }
-        VolumeVO volume = _volsDao.findById(volumeId);
-        if (volume == null) {
-            throw new InvalidParameterValueException("Unable to find a volume 
with id " + volumeId);
+
+        if (!isRootAdmin) {
+            Ternary<Long, Boolean, ListProjectResourcesCriteria> 
domainIdRecursiveListProject =
+                    new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
+            _accountMgr.buildACLSearchParameters(caller, id, null, null, 
permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
+            domainId = domainIdRecursiveListProject.first();
+            isRecursive = domainIdRecursiveListProject.second();
+            listProjectResourcesCriteria = 
domainIdRecursiveListProject.third();
         }
-        _accountMgr.checkAccess(CallContext.current().getCallingAccount(), 
null, true, volume);
-        if (result != null)
-            return new Pair<List<? extends SnapshotPolicy>, 
Integer>(result.first(), result.second());
-        result = _snapshotPolicyDao.listAndCountByVolumeId(volumeId, display);
-        return new Pair<List<? extends SnapshotPolicy>, 
Integer>(result.first(), result.second());
+        Filter searchFilter = new Filter(SnapshotPolicyVO.class, "id", false, 
cmd.getStartIndex(), cmd.getPageSizeVal());
+        SearchBuilder<SnapshotPolicyVO> policySearch = 
_snapshotPolicyDao.createSearchBuilder();
+
+        if (!isRootAdmin) {
+            _accountMgr.buildACLSearchBuilder(policySearch, domainId, 
isRecursive, permittedAccounts, listProjectResourcesCriteria);
+        }
+
+        policySearch.and("id", policySearch.entity().getId(), 
SearchCriteria.Op.EQ);
+        policySearch.and("volumeId", policySearch.entity().getVolumeId(), 
SearchCriteria.Op.EQ);
+
+        SearchCriteria<SnapshotPolicyVO> sc = policySearch.create();
+        if (!isRootAdmin) {
+            _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, 
permittedAccounts, listProjectResourcesCriteria);
+        }
+
+        if (volumeId != null) {
+            sc.setParameters("volumeId", volumeId);
+        }
+        if (id != null) {
+            sc.setParameters("id", id);
+        }
+
+        Pair<List<SnapshotPolicyVO>, Integer> result = 
_snapshotPolicyDao.searchAndCount(sc, searchFilter);
+        return new Pair<>(result.first(), result.second());

Review Comment:
   while that would be ideal, because of the dependency on the acl filtering 
logic, historically the approach has been to do this in the service layer. 



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