erikbocks commented on code in PR #11587:
URL: https://github.com/apache/cloudstack/pull/11587#discussion_r2370103355
##########
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:
I think that it is better if we do not mix the service and data layer here.
These database related operations should be in its respective DAO class.
##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -638,13 +638,57 @@ protected int
validateAndGetDefaultBackupRetentionIfRequired(Integer maxBackups,
return maxBackups;
}
- @Override
- public List<BackupSchedule> listBackupSchedule(final Long vmId) {
- final VMInstanceVO vm = findVmById(vmId);
- validateBackupForZone(vm.getDataCenterId());
- accountManager.checkAccess(CallContext.current().getCallingAccount(),
null, true, vm);
+ public List<BackupSchedule> listBackupSchedules(ListBackupScheduleCmd cmd)
{
+ Account caller = CallContext.current().getCallingAccount();
+ boolean isRootAdmin = accountManager.isRootAdmin(caller.getId());
+ Long id = cmd.getId();
+ Long vmId = cmd.getVmId();
+ List<Long> permittedAccounts = new ArrayList<>();
+ Long domainId = null;
+ Boolean isRecursive = null;
+ Project.ListProjectResourcesCriteria listProjectResourcesCriteria =
null;
+
+ if (vmId != null) {
+ final VMInstanceVO vm = findVmById(vmId);
+ validateBackupForZone(vm.getDataCenterId());
+
accountManager.checkAccess(CallContext.current().getCallingAccount(), null,
true, vm);
+ }
+
+ if (!isRootAdmin) {
+ Ternary<Long, Boolean, Project.ListProjectResourcesCriteria>
domainIdRecursiveListProject =
+ new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
+ accountManager.buildACLSearchParameters(caller, id, null, null,
permittedAccounts, domainIdRecursiveListProject, true, false);
+ domainId = domainIdRecursiveListProject.first();
+ isRecursive = domainIdRecursiveListProject.second();
+ listProjectResourcesCriteria =
domainIdRecursiveListProject.third();
+ }
+
+ Filter searchFilter = new Filter(BackupScheduleVO.class, "id", false,
null, null);
+ SearchBuilder<BackupScheduleVO> searchBuilder =
backupScheduleDao.createSearchBuilder();
+
+ if (!isRootAdmin) {
+ accountManager.buildACLSearchBuilder(searchBuilder, domainId,
isRecursive, permittedAccounts, listProjectResourcesCriteria);
+ }
+
+ searchBuilder.and("id", searchBuilder.entity().getId(),
SearchCriteria.Op.EQ);
+ if (vmId != null) {
+ searchBuilder.and("vmId", searchBuilder.entity().getVmId(),
SearchCriteria.Op.EQ);
+ }
+
+ SearchCriteria<BackupScheduleVO> sc = searchBuilder.create();
+ if (!isRootAdmin) {
+ accountManager.buildACLSearchCriteria(sc, domainId, isRecursive,
permittedAccounts, listProjectResourcesCriteria);
+ }
+
+ if (id != null) {
+ sc.setParameters("id", id);
+ }
+ if (vmId != null) {
+ sc.setParameters("vmId", vmId);
+ }
- return
backupScheduleDao.listByVM(vmId).stream().map(BackupSchedule.class::cast).collect(Collectors.toList());
+ Pair<List<BackupScheduleVO>, Integer> result =
backupScheduleDao.searchAndCount(sc, searchFilter);
+ return new ArrayList<>(result.first());
Review Comment:
I think that here we can also follow the suggestion that I made for the
`listSnapshotPolicies` method and keep the service and data layers segregated
by moving this logic to the DAO.
--
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]