@@ -2537,4 +2537,132 @@ public UserAccount getUserByApiKey(String apiKey) {
25372537 return _userAccountDao .getUserByApiKey (apiKey );
25382538 }
25392539
2540+ @ Override
2541+ public void buildACLSearchParameters (Account caller , Long id , String accountName , Long projectId , List <Long > permittedDomains , List <Long > permittedAccounts ,
2542+ List <Long > permittedResources , Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject , boolean listAll , boolean forProjectInvitation ,
2543+ String action ) {
2544+ Long domainId = domainIdRecursiveListProject .first ();
2545+ if (domainId != null ) {
2546+ // look for entity in the given domain
2547+ Domain domain = _domainDao .findById (domainId );
2548+ if (domain == null ) {
2549+ throw new InvalidParameterValueException ("Unable to find domain by id " + domainId );
2550+ }
2551+ // check permissions
2552+ checkAccess (caller , domain );
2553+ }
2554+
2555+ if (id != null ) {
2556+ // look for an individual entity, no other permission criteria are needed
2557+ permittedResources .add (id );
2558+ return ;
2559+ }
2560+
2561+ if (accountName != null ) {
2562+ if (projectId != null ) {
2563+ throw new InvalidParameterValueException ("Account and projectId can't be specified together" );
2564+ }
2565+
2566+ Account userAccount = null ;
2567+ Domain domain = null ;
2568+ if (domainId != null ) {
2569+ userAccount = _accountDao .findActiveAccount (accountName , domainId );
2570+ domain = _domainDao .findById (domainId );
2571+ } else {
2572+ userAccount = _accountDao .findActiveAccount (accountName , caller .getDomainId ());
2573+ domain = _domainDao .findById (caller .getDomainId ());
2574+ }
2575+
2576+ if (userAccount != null ) {
2577+ //check permissions
2578+ checkAccess (caller , null , false , userAccount );
2579+ permittedAccounts .add (userAccount .getId ());
2580+ } else {
2581+ throw new InvalidParameterValueException ("could not find account " + accountName + " in domain " + domain .getUuid ());
2582+ }
2583+ }
2584+
2585+ // set project information
2586+ if (projectId != null ) {
2587+ if (!forProjectInvitation ) {
2588+ if (projectId .longValue () == -1 ) {
2589+ if (isNormalUser (caller .getId ())) {
2590+ permittedAccounts .addAll (_projectMgr .listPermittedProjectAccounts (caller .getId ()));
2591+ } else {
2592+ domainIdRecursiveListProject .third (Project .ListProjectResourcesCriteria .ListProjectResourcesOnly );
2593+ }
2594+ } else {
2595+ Project project = _projectMgr .getProject (projectId );
2596+ if (project == null ) {
2597+ throw new InvalidParameterValueException ("Unable to find project by id " + projectId );
2598+ }
2599+ if (!_projectMgr .canAccessProjectAccount (caller , project .getProjectAccountId ())) {
2600+ throw new PermissionDeniedException ("Account " + caller + " can't access project id=" + projectId );
2601+ }
2602+ permittedAccounts .add (project .getProjectAccountId ());
2603+ }
2604+ }
2605+ } else {
2606+ domainIdRecursiveListProject .third (Project .ListProjectResourcesCriteria .SkipProjectResources );
2607+
2608+ // search for policy permissions associated with caller to get all his authorized domains, accounts, and resources
2609+ // Assumption: if a domain is in grantedDomains, then all the accounts under this domain will not be returned in "grantedAccounts". Similarly, if an account
2610+ // is in grantedAccounts, then all the resources owned by this account will not be returned in "grantedResources".
2611+ List <Long > grantedDomains = _aclService .getGrantedDomains (caller .getId (), AclEntityType .VM , action );
2612+ List <Long > grantedAccounts = _aclService .getGrantedAccounts (caller .getId (), AclEntityType .VM , action );
2613+ List <Long > grantedResources = _aclService .getGrantedResources (caller .getId (), AclEntityType .VM , action );
2614+
2615+ if (domainId != null ) {
2616+ // specific domain is specified
2617+ if (grantedDomains .contains (domainId )) {
2618+ permittedDomains .add (domainId );
2619+ } else {
2620+ for (Long acctId : grantedAccounts ) {
2621+ Account acct = _accountDao .findById (acctId );
2622+ if (acct != null && acct .getDomainId () == domainId ) {
2623+ permittedAccounts .add (acctId );
2624+ }
2625+ }
2626+ permittedResources .addAll (grantedResources );
2627+ }
2628+ } else if (permittedAccounts .isEmpty ()) {
2629+ // neither domain nor account is not specified
2630+ permittedDomains .addAll (grantedDomains );
2631+ permittedAccounts .addAll (grantedAccounts );
2632+ permittedResources .addAll (grantedResources );
2633+ }
2634+ }
2635+
2636+ }
2637+
2638+ @ Override
2639+ public void buildACLViewSearchCriteria (SearchCriteria <? extends ControlledEntity > sc , SearchCriteria <? extends ControlledEntity > aclSc , boolean isRecursive ,
2640+ List <Long > permittedDomains ,
2641+ List <Long > permittedAccounts , List <Long > permittedResources , ListProjectResourcesCriteria listProjectResourcesCriteria ) {
2642+
2643+ if (listProjectResourcesCriteria != null ) {
2644+ sc .addAnd ("accountType" , SearchCriteria .Op .EQ , Account .ACCOUNT_TYPE_PROJECT );
2645+ }
2646+
2647+ // Note that this may have limitations on number of permitted domains, accounts, or resource ids are allowed due to sql package size limitation
2648+ if (!permittedDomains .isEmpty ()) {
2649+ if (isRecursive ) {
2650+ for (int i = 0 ; i < permittedDomains .size (); i ++) {
2651+ Domain domain = _domainDao .findById (permittedDomains .get (i ));
2652+ aclSc .addOr ("domainPath" + i , SearchCriteria .Op .LIKE , domain .getPath () + "%" );
2653+ }
2654+ } else {
2655+ aclSc .addOr ("domainIdIN" , SearchCriteria .Op .IN , permittedDomains .toArray ());
2656+ }
2657+ }
2658+ if (!permittedAccounts .isEmpty ()) {
2659+ aclSc .addOr ("accountIdIN" , SearchCriteria .Op .IN , permittedAccounts .toArray ());
2660+ }
2661+ if (!permittedResources .isEmpty ()) {
2662+ aclSc .addOr ("idIn" , SearchCriteria .Op .IN , permittedResources .toArray ());
2663+ }
2664+
2665+ sc .addAnd ("accountIdIn" , SearchCriteria .Op .SC , aclSc );
2666+ }
2667+
25402668}
0 commit comments