2626import javax .ejb .Local ;
2727import javax .inject .Inject ;
2828
29+ import org .apache .log4j .Logger ;
30+ import org .springframework .stereotype .Component ;
31+
2932import org .apache .cloudstack .acl .ControlledEntity .ACLType ;
3033import org .apache .cloudstack .affinity .AffinityGroupDomainMapVO ;
3134import org .apache .cloudstack .affinity .AffinityGroupResponse ;
9598import org .apache .cloudstack .engine .subsystem .api .storage .TemplateState ;
9699import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
97100import org .apache .cloudstack .query .QueryService ;
98- import org .apache .log4j .Logger ;
99- import org .springframework .stereotype .Component ;
100101
101102import com .cloud .api .query .dao .AccountJoinDao ;
102103import com .cloud .api .query .dao .AffinityGroupJoinDao ;
@@ -1834,52 +1835,19 @@ public ListResponse<AccountResponse> searchForAccounts(ListAccountsCmd cmd) {
18341835
18351836 private Pair <List <AccountJoinVO >, Integer > searchForAccountsInternal (ListAccountsCmd cmd ) {
18361837 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- }
1838+ List <Long > permittedDomains = new ArrayList <Long >();
1839+ List <Long > permittedAccounts = new ArrayList <Long >();
1840+ List <Long > permittedResources = new ArrayList <Long >();
18691841
1870- if (accountId == null ) {
1871- if (_accountMgr .isAdmin (caller .getType ()) && listAll && domainId == null ) {
1872- listForDomain = true ;
1873- isRecursive = true ;
1874- if (domainId == null ) {
1875- domainId = caller .getDomainId ();
1876- }
1877- } else if (_accountMgr .isAdmin (caller .getType ()) && domainId != null ) {
1878- listForDomain = true ;
1879- } else {
1880- accountId = caller .getAccountId ();
1881- }
1882- }
1842+ boolean listAll = cmd .listAll ();
1843+ Long id = cmd .getId ();
1844+ String accountName = cmd .getSearchName ();
1845+ Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <Long , Boolean , ListProjectResourcesCriteria >(
1846+ cmd .getDomainId (), cmd .isRecursive (), null );
1847+ // ListAccountsCmd is not BaseListAccountResourcesCmd, so no (domainId, accountName) combination
1848+ _accountMgr .buildACLSearchParameters (caller , id , null , null , permittedDomains , permittedAccounts , permittedResources ,
1849+ domainIdRecursiveListProject , listAll , false , "listAccounts" );
1850+ Boolean isRecursive = domainIdRecursiveListProject .second ();
18831851
18841852 Filter searchFilter = new Filter (AccountJoinVO .class , "id" , true , cmd .getStartIndex (), cmd .getPageSizeVal ());
18851853
@@ -1890,19 +1858,38 @@ private Pair<List<AccountJoinVO>, Integer> searchForAccountsInternal(ListAccount
18901858
18911859 SearchBuilder <AccountJoinVO > sb = _accountJoinDao .createSearchBuilder ();
18921860 sb .and ("accountName" , sb .entity ().getAccountName (), SearchCriteria .Op .EQ );
1893- sb .and ("domainId" , sb .entity ().getDomainId (), SearchCriteria .Op .EQ );
18941861 sb .and ("id" , sb .entity ().getId (), SearchCriteria .Op .EQ );
18951862 sb .and ("type" , sb .entity ().getType (), SearchCriteria .Op .EQ );
18961863 sb .and ("state" , sb .entity ().getState (), SearchCriteria .Op .EQ );
18971864 sb .and ("needsCleanup" , sb .entity ().isNeedsCleanup (), SearchCriteria .Op .EQ );
18981865 sb .and ("typeNEQ" , sb .entity ().getType (), SearchCriteria .Op .NEQ );
18991866 sb .and ("idNEQ" , sb .entity ().getId (), SearchCriteria .Op .NEQ );
19001867
1901- if (listForDomain && isRecursive ) {
1902- sb .and ("path" , sb .entity ().getDomainPath (), SearchCriteria .Op .LIKE );
1903- }
1904-
19051868 SearchCriteria <AccountJoinVO > sc = sb .create ();
1869+ SearchCriteria <AccountJoinVO > aclSc = _accountJoinDao .createSearchCriteria ();
1870+ // building ACL search criteria. Here we cannot use the common accountMgr.buildACLViewSearchCriteria because
1871+ // 1) AccountJoinVO does not have accountId field, permittedAccounts correspond to list of resource ids.
1872+ // 2) AccountJoinVO use type not accountType field to indicate its type
1873+ if (!permittedDomains .isEmpty () || !permittedAccounts .isEmpty () || !permittedResources .isEmpty ()) {
1874+ if (!permittedDomains .isEmpty ()) {
1875+ if (isRecursive ) {
1876+ for (int i = 0 ; i < permittedDomains .size (); i ++) {
1877+ Domain domain = _domainDao .findById (permittedDomains .get (i ));
1878+ aclSc .addOr ("domainPath" , SearchCriteria .Op .LIKE , domain .getPath () + "%" );
1879+ }
1880+ } else {
1881+ aclSc .addOr ("domainId" , SearchCriteria .Op .IN , permittedDomains .toArray ());
1882+ }
1883+ }
1884+ if (!permittedAccounts .isEmpty ()) {
1885+ aclSc .addOr ("id" , SearchCriteria .Op .IN , permittedAccounts .toArray ());
1886+ }
1887+ if (!permittedResources .isEmpty ()) {
1888+ aclSc .addOr ("id" , SearchCriteria .Op .IN , permittedResources .toArray ());
1889+ }
1890+
1891+ sc .addAnd ("id" , SearchCriteria .Op .SC , aclSc );
1892+ }
19061893
19071894 sc .setParameters ("idNEQ" , Account .ACCOUNT_ID_SYSTEM );
19081895
@@ -1930,19 +1917,10 @@ private Pair<List<AccountJoinVO>, Integer> searchForAccountsInternal(ListAccount
19301917 }
19311918
19321919 // don't return account of type project to the end user
1933- sc .setParameters ("typeNEQ" , 5 );
1934-
1935- if (accountId != null ) {
1936- sc .setParameters ("id" , accountId );
1937- }
1920+ sc .setParameters ("typeNEQ" , Account .ACCOUNT_TYPE_PROJECT );
19381921
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- }
1922+ if (id != null ) {
1923+ sc .setParameters ("id" , id );
19461924 }
19471925
19481926 return _accountJoinDao .searchAndCount (sc , searchFilter );
0 commit comments