From 90521a3e15ab3e1848d039b279da1847c0b10c52 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Mon, 21 Oct 2013 14:30:17 +0200 Subject: [PATCH] allow Domain Admin to list usage records in the domain --- client/tomcatconf/commands.properties.in | 2 +- .../src/com/cloud/usage/UsageServiceImpl.java | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in index 96e841a4c7d..0296de0a3cc 100644 --- a/client/tomcatconf/commands.properties.in +++ b/client/tomcatconf/commands.properties.in @@ -493,7 +493,7 @@ listVirtualRouterElements=7 #### usage commands generateUsageRecords=1 -listUsageRecords=1 +listUsageRecords=7 listUsageTypes=1 #### traffic monitor commands diff --git a/server/src/com/cloud/usage/UsageServiceImpl.java b/server/src/com/cloud/usage/UsageServiceImpl.java index 2ffb01d3bce..0fed51ca524 100755 --- a/server/src/com/cloud/usage/UsageServiceImpl.java +++ b/server/src/com/cloud/usage/UsageServiceImpl.java @@ -39,6 +39,7 @@ import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import com.cloud.configuration.Config; +import com.cloud.domain.DomainVO; import com.cloud.domain.dao.DomainDao; import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.PermissionDeniedException; @@ -150,7 +151,8 @@ public class UsageServiceImpl extends ManagerBase implements UsageService, Manag } boolean isAdmin = false; - + boolean isDomainAdmin = false; + //If accountId couldn't be found using accountName and domainId, get it from userContext if(accountId == null){ accountId = caller.getId(); @@ -158,6 +160,8 @@ public class UsageServiceImpl extends ManagerBase implements UsageService, Manag //If account_id or account_name is explicitly mentioned, list records for the specified account only even if the caller is of type admin if(caller.getType() == Account.ACCOUNT_TYPE_ADMIN){ isAdmin = true; + } else if(caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN){ + isDomainAdmin = true; } s_logger.debug("Account details not available. Using userContext accountId: " + accountId); } @@ -179,10 +183,20 @@ public class UsageServiceImpl extends ManagerBase implements UsageService, Manag SearchCriteria sc = _usageDao.createSearchCriteria(); - if (accountId != -1 && accountId != Account.ACCOUNT_ID_SYSTEM && !isAdmin) { + if (accountId != -1 && accountId != Account.ACCOUNT_ID_SYSTEM && !isAdmin && !isDomainAdmin) { sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); } + if (isDomainAdmin) { + SearchCriteria sdc = _domainDao.createSearchCriteria(); + sdc.addOr("path", SearchCriteria.Op.LIKE, _domainDao.findById(caller.getDomainId()).getPath() + "%"); + List domains = _domainDao.search(sdc, null); + List domainIds = new ArrayList(); + for(DomainVO domain:domains) + domainIds.add(domain.getId()); + sc.addAnd("domainId", SearchCriteria.Op.IN, domainIds.toArray()); + } + if (domainId != null) { sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); }