|
82 | 82 | import org.apache.cloudstack.utils.bytescale.ByteScaleUtils; |
83 | 83 | import org.apache.commons.beanutils.BeanUtils; |
84 | 84 | import org.apache.commons.collections.CollectionUtils; |
| 85 | +import org.apache.commons.lang3.ObjectUtils; |
85 | 86 | import org.apache.commons.lang3.StringUtils; |
86 | 87 | import org.apache.commons.lang3.builder.ReflectionToStringBuilder; |
87 | 88 | import org.apache.commons.lang3.builder.ToStringStyle; |
@@ -245,30 +246,48 @@ public ListResponse<VolumeMetricsStatsResponse> searchForVolumeMetricsStats(List |
245 | 246 | } |
246 | 247 |
|
247 | 248 | /** |
248 | | - * Searches VMs based on {@code ListVMsUsageHistoryCmd} parameters. |
249 | | - * |
250 | | - * @param cmd the {@link ListVMsUsageHistoryCmd} specifying the parameters. |
251 | | - * @return the list of VMs. |
| 249 | + * Outputs the parameters that should be used for access control in the query of a resource to |
| 250 | + * {@code permittedAccounts} and {@code domainIdRecursiveListProject}. |
| 251 | + * @param isIdProvided indicates whether any ID was provided to the command |
252 | 252 | */ |
253 | | - protected Pair<List<UserVmVO>, Integer> searchForUserVmsInternal(ListVMsUsageHistoryCmd cmd) { |
254 | | - final Long id = cmd.getId(); |
| 253 | + private void buildBaseACLSearchParametersForMetrics(boolean isIdProvided, List<Long> permittedAccounts, Ternary<Long, Boolean, |
| 254 | + Project.ListProjectResourcesCriteria> domainIdRecursiveListProject) { |
255 | 255 | Account caller = CallContext.current().getCallingAccount(); |
256 | | - List<Long> permittedAccounts = new ArrayList<>(); |
257 | 256 | Account.Type callerType = caller.getType(); |
| 257 | + |
258 | 258 | boolean recursive = AccountTypesWithRecursiveUsageAccess.contains(callerType); |
259 | | - Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, recursive, null); |
| 259 | + domainIdRecursiveListProject.second(recursive); |
| 260 | + |
| 261 | + // If no ID was provided, then the listing will skip project resources (null); otherwise, project resources should |
| 262 | + // be listed as well (any long allows this) |
| 263 | + Long id = isIdProvided ? 1L : null; |
260 | 264 |
|
261 | 265 | // Allow users to also list metrics of resources owned by projects they belong to (-1L), and admins to list all |
262 | 266 | // metrics belonging to their domains recursively (null) |
263 | | - Long projectId = callerType == Account.Type.NORMAL ? -1L : null; |
| 267 | + Long projectId = isIdProvided && callerType == Account.Type.NORMAL ? -1L : null; |
264 | 268 |
|
265 | 269 | accountMgr.buildACLSearchParameters(caller, id, null, projectId, permittedAccounts, domainIdRecursiveListProject, true, false); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Searches VMs based on {@code ListVMsUsageHistoryCmd} parameters. |
| 274 | + * |
| 275 | + * @param cmd the {@link ListVMsUsageHistoryCmd} specifying the parameters. |
| 276 | + * @return the list of VMs. |
| 277 | + */ |
| 278 | + protected Pair<List<UserVmVO>, Integer> searchForUserVmsInternal(ListVMsUsageHistoryCmd cmd) { |
| 279 | + List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds()); |
| 280 | + |
| 281 | + boolean isIdProvided = CollectionUtils.isNotEmpty(ids); |
| 282 | + List<Long> permittedAccounts = new ArrayList<>(); |
| 283 | + Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, null, null); |
| 284 | + buildBaseACLSearchParametersForMetrics(isIdProvided, permittedAccounts, domainIdRecursiveListProject); |
| 285 | + |
266 | 286 | Long domainId = domainIdRecursiveListProject.first(); |
267 | 287 | Boolean isRecursive = domainIdRecursiveListProject.second(); |
268 | 288 | Project.ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third(); |
269 | 289 |
|
270 | 290 | Filter searchFilter = new Filter(UserVmVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); |
271 | | - List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds()); |
272 | 291 | String name = cmd.getName(); |
273 | 292 | String keyword = cmd.getKeyword(); |
274 | 293 |
|
@@ -363,24 +382,18 @@ protected Map<Long,List<VmStatsVO>> searchForVmMetricsStatsInternal(Date startDa |
363 | 382 | * @return the list of VMs. |
364 | 383 | */ |
365 | 384 | protected Pair<List<VolumeVO>, Integer> searchForVolumesInternal(ListVolumesUsageHistoryCmd cmd) { |
366 | | - final Long id = cmd.getId(); |
367 | | - Account caller = CallContext.current().getCallingAccount(); |
368 | | - List<Long> permittedAccounts = new ArrayList<>(); |
369 | | - Account.Type callerType = caller.getType(); |
370 | | - boolean recursive = AccountTypesWithRecursiveUsageAccess.contains(callerType); |
371 | | - Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, recursive, null); |
| 385 | + List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds()); |
372 | 386 |
|
373 | | - // Allow users to also list metrics of resources owned by projects they belong to (-1L), and admins to list all |
374 | | - // metrics belonging to their domains recursively (null) |
375 | | - Long projectId = callerType == Account.Type.NORMAL ? -1L : null; |
| 387 | + boolean isIdProvided = CollectionUtils.isNotEmpty(ids); |
| 388 | + List<Long> permittedAccounts = new ArrayList<>(); |
| 389 | + Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, null, null); |
| 390 | + buildBaseACLSearchParametersForMetrics(isIdProvided, permittedAccounts, domainIdRecursiveListProject); |
376 | 391 |
|
377 | | - accountMgr.buildACLSearchParameters(caller, id, null, projectId, permittedAccounts, domainIdRecursiveListProject, true, false); |
378 | 392 | Long domainId = domainIdRecursiveListProject.first(); |
379 | 393 | Boolean isRecursive = domainIdRecursiveListProject.second(); |
380 | 394 | Project.ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third(); |
381 | 395 |
|
382 | 396 | Filter searchFilter = new Filter(VolumeVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); |
383 | | - List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds()); |
384 | 397 | String name = cmd.getName(); |
385 | 398 | String keyword = cmd.getKeyword(); |
386 | 399 |
|
|
0 commit comments