Skip to content

Commit 6730fa2

Browse files
committed
Fill in implementation of AclService.getGrantedDomains,
getGrantedAccounts and getGrantedResources.
1 parent 288a811 commit 6730fa2

5 files changed

Lines changed: 102 additions & 19 deletions

File tree

‎api/src/org/apache/cloudstack/acl/AclService.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ public interface AclService {
5757
List<AclPolicy> getEffectivePolicies(Account caller, ControlledEntity entity);
5858

5959
/* Visibility related interfaces */
60-
List<Long> getGrantedDomains(long accountId, AclEntityType entityType, String action);
60+
List<Long> getGrantedDomains(long accountId, String action);
6161

62-
List<Long> getGrantedAccounts(long accountId, AclEntityType entityType, String action);
62+
List<Long> getGrantedAccounts(long accountId, String action);
6363

64-
List<Long> getGrantedResources(long accountId, AclEntityType entityType, String action);
64+
List<Long> getGrantedResources(long accountId, String action);
6565

6666
}

‎engine/schema/src/org/apache/cloudstack/acl/dao/AclPolicyPermissionDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ public interface AclPolicyPermissionDao extends GenericDao<AclPolicyPermissionVO
3131

3232
AclPolicyPermissionVO findByPolicyAndEntity(long policyId, String entityType, PermissionScope scope, Long scopeId, String action, Permission perm);
3333

34+
List<AclPolicyPermissionVO> listGrantedByActionAndScope(long policyId, String action, PermissionScope scope);
35+
3436

3537
}

‎engine/schema/src/org/apache/cloudstack/acl/dao/AclPolicyPermissionDaoImpl.java‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,70 @@
2626
import org.apache.cloudstack.acl.PermissionScope;
2727

2828
import com.cloud.utils.db.GenericDaoBase;
29+
import com.cloud.utils.db.SearchBuilder;
30+
import com.cloud.utils.db.SearchCriteria;
2931

3032
public class AclPolicyPermissionDaoImpl extends GenericDaoBase<AclPolicyPermissionVO, Long> implements
3133
AclPolicyPermissionDao {
3234

35+
private SearchBuilder<AclPolicyPermissionVO> policyIdSearch;
36+
private SearchBuilder<AclPolicyPermissionVO> fullSearch;
37+
private SearchBuilder<AclPolicyPermissionVO> actionScopeSearch;
3338

3439
@Override
3540
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
3641
super.configure(name, params);
3742

43+
policyIdSearch = createSearchBuilder();
44+
policyIdSearch.and("policyId", policyIdSearch.entity().getAclPolicyId(), SearchCriteria.Op.EQ);
45+
policyIdSearch.done();
46+
47+
fullSearch = createSearchBuilder();
48+
fullSearch.and("policyId", fullSearch.entity().getAclPolicyId(), SearchCriteria.Op.EQ);
49+
fullSearch.and("entityType", fullSearch.entity().getEntityType(), SearchCriteria.Op.EQ);
50+
fullSearch.and("scope", fullSearch.entity().getScope(), SearchCriteria.Op.EQ);
51+
fullSearch.and("scopeId", fullSearch.entity().getScopeId(), SearchCriteria.Op.EQ);
52+
fullSearch.and("action", fullSearch.entity().getAction(), SearchCriteria.Op.EQ);
53+
fullSearch.and("permission", fullSearch.entity().getPermission(), SearchCriteria.Op.EQ);
54+
fullSearch.done();
55+
56+
actionScopeSearch = createSearchBuilder();
57+
actionScopeSearch.and("policyId", actionScopeSearch.entity().getAclPolicyId(), SearchCriteria.Op.EQ);
58+
actionScopeSearch.and("scope", actionScopeSearch.entity().getScope(), SearchCriteria.Op.EQ);
59+
actionScopeSearch.and("action", actionScopeSearch.entity().getAction(), SearchCriteria.Op.EQ);
60+
actionScopeSearch.and("permission", actionScopeSearch.entity().getPermission(), SearchCriteria.Op.EQ);
61+
actionScopeSearch.done();
62+
3863
return true;
3964
}
4065

4166
@Override
4267
public List<AclPolicyPermissionVO> listByPolicy(long policyId) {
43-
// TODO Auto-generated method stub
44-
return null;
68+
SearchCriteria<AclPolicyPermissionVO> sc = policyIdSearch.create();
69+
sc.setParameters("policyId", policyId);
70+
return listBy(sc);
4571
}
4672

4773
@Override
4874
public AclPolicyPermissionVO findByPolicyAndEntity(long policyId, String entityType, PermissionScope scope, Long scopeId, String action, Permission perm) {
49-
// TODO Auto-generated method stub
50-
return null;
75+
SearchCriteria<AclPolicyPermissionVO> sc = fullSearch.create();
76+
sc.setParameters("policyId", policyId);
77+
sc.setParameters("entityType", entityType);
78+
sc.setParameters("scope", scope);
79+
sc.setParameters("scopeId", scopeId);
80+
sc.setParameters("action", action);
81+
sc.setParameters("permission", perm);
82+
return findOneBy(sc);
83+
}
84+
85+
@Override
86+
public List<AclPolicyPermissionVO> listGrantedByActionAndScope(long policyId, String action, PermissionScope scope) {
87+
SearchCriteria<AclPolicyPermissionVO> sc = actionScopeSearch.create();
88+
sc.setParameters("policyId", policyId);
89+
sc.setParameters("action", action);
90+
sc.setParameters("scope", scope);
91+
sc.setParameters("permission", Permission.Allow);
92+
return listBy(sc);
5193
}
5294

5395
}

‎server/src/com/cloud/user/AccountManagerImpl.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,9 +2610,9 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
26102610
// search for policy permissions associated with caller to get all his authorized domains, accounts, and resources
26112611
// Assumption: if a domain is in grantedDomains, then all the accounts under this domain will not be returned in "grantedAccounts". Similarly, if an account
26122612
// is in grantedAccounts, then all the resources owned by this account will not be returned in "grantedResources".
2613-
List<Long> grantedDomains = _aclService.getGrantedDomains(caller.getId(), AclEntityType.VM, action);
2614-
List<Long> grantedAccounts = _aclService.getGrantedAccounts(caller.getId(), AclEntityType.VM, action);
2615-
List<Long> grantedResources = _aclService.getGrantedResources(caller.getId(), AclEntityType.VM, action);
2613+
List<Long> grantedDomains = _aclService.getGrantedDomains(caller.getId(), action);
2614+
List<Long> grantedAccounts = _aclService.getGrantedAccounts(caller.getId(), action);
2615+
List<Long> grantedResources = _aclService.getGrantedResources(caller.getId(), action);
26162616

26172617
if (domainId != null) {
26182618
// specific domain is specified

‎server/src/org/apache/cloudstack/acl/AclServiceImpl.java‎

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -678,21 +678,60 @@ public List<AclPolicy> getEffectivePolicies(Account caller, ControlledEntity ent
678678
}
679679

680680
@Override
681-
public List<Long> getGrantedDomains(long accountId, AclEntityType entityType, String action) {
682-
// TODO Auto-generated method stub
683-
return null;
681+
public List<Long> getGrantedDomains(long accountId, String action) {
682+
// Get the static Policies of the Caller
683+
List<AclPolicy> policies = listAclPolicies(accountId);
684+
// for each policy, find granted permission with Domain scope
685+
List<Long> domainIds = new ArrayList<Long>();
686+
for (AclPolicy policy : policies) {
687+
List<AclPolicyPermissionVO> pp = _policyPermissionDao.listGrantedByActionAndScope(policy.getId(), action, PermissionScope.DOMAIN);
688+
if (pp != null) {
689+
for (AclPolicyPermissionVO p : pp) {
690+
if (p.getScopeId() != null) {
691+
domainIds.add(p.getScopeId());
692+
}
693+
}
694+
}
695+
}
696+
return domainIds;
684697
}
685698

686699
@Override
687-
public List<Long> getGrantedAccounts(long accountId, AclEntityType entityType, String action) {
688-
// TODO Auto-generated method stub
689-
return null;
700+
public List<Long> getGrantedAccounts(long accountId, String action) {
701+
// Get the static Policies of the Caller
702+
List<AclPolicy> policies = listAclPolicies(accountId);
703+
// for each policy, find granted permission with Account scope
704+
List<Long> accountIds = new ArrayList<Long>();
705+
for (AclPolicy policy : policies) {
706+
List<AclPolicyPermissionVO> pp = _policyPermissionDao.listGrantedByActionAndScope(policy.getId(), action, PermissionScope.ACCOUNT);
707+
if (pp != null) {
708+
for (AclPolicyPermissionVO p : pp) {
709+
if (p.getScopeId() != null) {
710+
accountIds.add(p.getScopeId());
711+
}
712+
}
713+
}
714+
}
715+
return accountIds;
690716
}
691717

692718
@Override
693-
public List<Long> getGrantedResources(long accountId, AclEntityType entityType, String action) {
694-
// TODO Auto-generated method stub
695-
return null;
719+
public List<Long> getGrantedResources(long accountId, String action) {
720+
// Get the static Policies of the Caller
721+
List<AclPolicy> policies = listAclPolicies(accountId);
722+
// for each policy, find granted permission with Resource scope
723+
List<Long> entityIds = new ArrayList<Long>();
724+
for (AclPolicy policy : policies) {
725+
List<AclPolicyPermissionVO> pp = _policyPermissionDao.listGrantedByActionAndScope(policy.getId(), action, PermissionScope.RESOURCE);
726+
if (pp != null) {
727+
for (AclPolicyPermissionVO p : pp) {
728+
if (p.getScopeId() != null) {
729+
entityIds.add(p.getScopeId());
730+
}
731+
}
732+
}
733+
}
734+
return entityIds;
696735
}
697736

698737
}

0 commit comments

Comments
 (0)