Skip to content

Commit c3a4a01

Browse files
committed
Fix listAccountsCmd logic to be consistent with new interpretation of
listAll, isRecursive and domainId parameters.
1 parent 4a51890 commit c3a4a01

1 file changed

Lines changed: 39 additions & 62 deletions

File tree

‎server/src/com/cloud/api/query/QueryManagerImpl.java‎

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,52 +1834,19 @@ public ListResponse<AccountResponse> searchForAccounts(ListAccountsCmd cmd) {
18341834

18351835
private Pair<List<AccountJoinVO>, Integer> searchForAccountsInternal(ListAccountsCmd cmd) {
18361836
Account caller = CallContext.current().getCallingAccount();
1837-
Long domainId = cmd.getDomainId();
1838-
Long accountId = cmd.getId();
1839-
String accountName = cmd.getSearchName();
1840-
boolean isRecursive = cmd.isRecursive();
1841-
boolean listAll = cmd.listAll();
1842-
Boolean listForDomain = false;
1843-
1844-
if (accountId != null) {
1845-
Account account = _accountDao.findById(accountId);
1846-
if (account == null || account.getId() == Account.ACCOUNT_ID_SYSTEM) {
1847-
throw new InvalidParameterValueException("Unable to find account by id " + accountId);
1848-
}
1849-
1850-
_accountMgr.checkAccess(caller, null, account);
1851-
}
1852-
1853-
if (domainId != null) {
1854-
Domain domain = _domainDao.findById(domainId);
1855-
if (domain == null) {
1856-
throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist");
1857-
}
1858-
1859-
_accountMgr.checkAccess(caller, domain);
1860-
1861-
if (accountName != null) {
1862-
Account account = _accountDao.findActiveAccount(accountName, domainId);
1863-
if (account == null || account.getId() == Account.ACCOUNT_ID_SYSTEM) {
1864-
throw new InvalidParameterValueException("Unable to find account by name " + accountName + " in domain " + domainId);
1865-
}
1866-
_accountMgr.checkAccess(caller, null, account);
1867-
}
1868-
}
1837+
List<Long> permittedDomains = new ArrayList<Long>();
1838+
List<Long> permittedAccounts = new ArrayList<Long>();
1839+
List<Long> permittedResources = new ArrayList<Long>();
18691840

1870-
if (accountId == null) {
1871-
if (_accountMgr.isAdmin(caller.getId()) && listAll && domainId == null) {
1872-
listForDomain = true;
1873-
isRecursive = true;
1874-
if (domainId == null) {
1875-
domainId = caller.getDomainId();
1876-
}
1877-
} else if (_accountMgr.isAdmin(caller.getId()) && domainId != null) {
1878-
listForDomain = true;
1879-
} else {
1880-
accountId = caller.getAccountId();
1881-
}
1882-
}
1841+
boolean listAll = cmd.listAll();
1842+
Long id = cmd.getId();
1843+
String accountName = cmd.getSearchName();
1844+
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(
1845+
cmd.getDomainId(), cmd.isRecursive(), null);
1846+
// ListAccountsCmd is not BaseListAccountResourcesCmd, so no (domainId, accountName) combination
1847+
_accountMgr.buildACLSearchParameters(caller, id, null, null, permittedDomains, permittedAccounts, permittedResources,
1848+
domainIdRecursiveListProject, listAll, false, "listAccounts");
1849+
Boolean isRecursive = domainIdRecursiveListProject.second();
18831850

18841851
Filter searchFilter = new Filter(AccountJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
18851852

@@ -1890,19 +1857,38 @@ private Pair<List<AccountJoinVO>, Integer> searchForAccountsInternal(ListAccount
18901857

18911858
SearchBuilder<AccountJoinVO> sb = _accountJoinDao.createSearchBuilder();
18921859
sb.and("accountName", sb.entity().getAccountName(), SearchCriteria.Op.EQ);
1893-
sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
18941860
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
18951861
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
18961862
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
18971863
sb.and("needsCleanup", sb.entity().isNeedsCleanup(), SearchCriteria.Op.EQ);
18981864
sb.and("typeNEQ", sb.entity().getType(), SearchCriteria.Op.NEQ);
18991865
sb.and("idNEQ", sb.entity().getId(), SearchCriteria.Op.NEQ);
19001866

1901-
if (listForDomain && isRecursive) {
1902-
sb.and("path", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
1903-
}
1904-
19051867
SearchCriteria<AccountJoinVO> sc = sb.create();
1868+
SearchCriteria<AccountJoinVO> aclSc = _accountJoinDao.createSearchCriteria();
1869+
// building ACL search criteria. Here we cannot use the common accountMgr.buildACLViewSearchCriteria because
1870+
// 1) AccountJoinVO does not have accountId field, permittedAccounts correspond to list of resource ids.
1871+
// 2) AccountJoinVO use type not accountType field to indicate its type
1872+
if (!permittedDomains.isEmpty() || !permittedAccounts.isEmpty() || !permittedResources.isEmpty()) {
1873+
if (!permittedDomains.isEmpty()) {
1874+
if (isRecursive) {
1875+
for (int i = 0; i < permittedDomains.size(); i++) {
1876+
Domain domain = _domainDao.findById(permittedDomains.get(i));
1877+
aclSc.addOr("domainPath", SearchCriteria.Op.LIKE, domain.getPath() + "%");
1878+
}
1879+
} else {
1880+
aclSc.addOr("domainId", SearchCriteria.Op.IN, permittedDomains.toArray());
1881+
}
1882+
}
1883+
if (!permittedAccounts.isEmpty()) {
1884+
aclSc.addOr("id", SearchCriteria.Op.IN, permittedAccounts.toArray());
1885+
}
1886+
if (!permittedResources.isEmpty()) {
1887+
aclSc.addOr("id", SearchCriteria.Op.IN, permittedResources.toArray());
1888+
}
1889+
1890+
sc.addAnd("id", SearchCriteria.Op.SC, aclSc);
1891+
}
19061892

19071893
sc.setParameters("idNEQ", Account.ACCOUNT_ID_SYSTEM);
19081894

@@ -1930,19 +1916,10 @@ private Pair<List<AccountJoinVO>, Integer> searchForAccountsInternal(ListAccount
19301916
}
19311917

19321918
// don't return account of type project to the end user
1933-
sc.setParameters("typeNEQ", 5);
1919+
sc.setParameters("typeNEQ", Account.ACCOUNT_TYPE_PROJECT);
19341920

1935-
if (accountId != null) {
1936-
sc.setParameters("id", accountId);
1937-
}
1938-
1939-
if (listForDomain) {
1940-
if (isRecursive) {
1941-
Domain domain = _domainDao.findById(domainId);
1942-
sc.setParameters("path", domain.getPath() + "%");
1943-
} else {
1944-
sc.setParameters("domainId", domainId);
1945-
}
1921+
if (id != null) {
1922+
sc.setParameters("id", id);
19461923
}
19471924

19481925
return _accountJoinDao.searchAndCount(sc, searchFilter);

0 commit comments

Comments
 (0)