2525
2626import org .apache .cloudstack .acl .PermissionScope ;
2727import org .apache .cloudstack .acl .QuerySelector ;
28+ import org .apache .cloudstack .acl .SecurityChecker .AccessType ;
2829import org .apache .cloudstack .iam .api .IAMGroup ;
2930import org .apache .cloudstack .iam .api .IAMPolicy ;
3031import org .apache .cloudstack .iam .api .IAMPolicyPermission ;
@@ -41,14 +42,22 @@ public class RoleBasedEntityQuerySelector extends AdapterBase implements QuerySe
4142 IAMService _iamService ;
4243
4344 @ Override
44- public List <Long > getAuthorizedDomains (Account caller , String action ) {
45+ public List <Long > getAuthorizedDomains (Account caller , String action , AccessType accessType ) {
4546 long accountId = caller .getAccountId ();
47+ if (accessType == null ) {
48+ accessType = AccessType .UseEntry ; // default always show resources authorized to use
49+ }
4650 // Get the static Policies of the Caller
4751 List <IAMPolicy > policies = _iamService .listIAMPolicies (accountId );
4852 // for each policy, find granted permission with Domain scope
4953 List <Long > domainIds = new ArrayList <Long >();
5054 for (IAMPolicy policy : policies ) {
51- List <IAMPolicyPermission > pp = _iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .DOMAIN .toString ());
55+ List <IAMPolicyPermission > pp = new ArrayList <IAMPolicyPermission >();
56+ for (AccessType type : AccessType .values ()) {
57+ if (type .ordinal () >= accessType .ordinal ()) {
58+ pp .addAll (_iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .DOMAIN .toString (), type ));
59+ }
60+ }
5261 if (pp != null ) {
5362 for (IAMPolicyPermission p : pp ) {
5463 if (p .getScopeId () != null ) {
@@ -65,14 +74,22 @@ public List<Long> getAuthorizedDomains(Account caller, String action) {
6574 }
6675
6776 @ Override
68- public List <Long > getAuthorizedAccounts (Account caller , String action ) {
77+ public List <Long > getAuthorizedAccounts (Account caller , String action , AccessType accessType ) {
6978 long accountId = caller .getAccountId ();
79+ if (accessType == null ) {
80+ accessType = AccessType .UseEntry ; // default always show resources authorized to use
81+ }
7082 // Get the static Policies of the Caller
7183 List <IAMPolicy > policies = _iamService .listIAMPolicies (accountId );
7284 // for each policy, find granted permission with Account scope
7385 List <Long > accountIds = new ArrayList <Long >();
7486 for (IAMPolicy policy : policies ) {
75- List <IAMPolicyPermission > pp = _iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .ACCOUNT .toString ());
87+ List <IAMPolicyPermission > pp = new ArrayList <IAMPolicyPermission >();
88+ for (AccessType type : AccessType .values ()) {
89+ if (type .ordinal () >= accessType .ordinal ()) {
90+ pp .addAll (_iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .ACCOUNT .toString (), type ));
91+ }
92+ }
7693 if (pp != null ) {
7794 for (IAMPolicyPermission p : pp ) {
7895 if (p .getScopeId () != null ) {
@@ -89,8 +106,11 @@ public List<Long> getAuthorizedAccounts(Account caller, String action) {
89106 }
90107
91108 @ Override
92- public List <Long > getAuthorizedResources (Account caller , String action ) {
109+ public List <Long > getAuthorizedResources (Account caller , String action , AccessType accessType ) {
93110 long accountId = caller .getAccountId ();
111+ if (accessType == null ) {
112+ accessType = AccessType .UseEntry ; // default always show resources authorized to use
113+ }
94114 // Get the static Policies of the Caller
95115 List <IAMPolicy > policies = _iamService .listIAMPolicies (accountId );
96116
@@ -107,7 +127,12 @@ public List<Long> getAuthorizedResources(Account caller, String action) {
107127 // for each policy, find granted permission with Resource scope
108128 List <Long > entityIds = new ArrayList <Long >();
109129 for (IAMPolicy policy : policies ) {
110- List <IAMPolicyPermission > pp = _iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .RESOURCE .toString ());
130+ List <IAMPolicyPermission > pp = new ArrayList <IAMPolicyPermission >();
131+ for (AccessType type : AccessType .values ()) {
132+ if (type .ordinal () >= accessType .ordinal ()) {
133+ pp .addAll (_iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .RESOURCE .toString (), type ));
134+ }
135+ }
111136 if (pp != null ) {
112137 for (IAMPolicyPermission p : pp ) {
113138 if (p .getScopeId () != null ) {
@@ -120,15 +145,23 @@ public List<Long> getAuthorizedResources(Account caller, String action) {
120145 }
121146
122147 @ Override
123- public boolean isGrantedAll (Account caller , String action ) {
148+ public boolean isGrantedAll (Account caller , String action , AccessType accessType ) {
124149 long accountId = caller .getAccountId ();
150+ if (accessType == null ) {
151+ accessType = AccessType .UseEntry ; // default always show resources authorized to use
152+ }
125153 // Get the static Policies of the Caller
126154 List <IAMPolicy > policies = _iamService .listIAMPolicies (accountId );
127155 // for each policy, find granted permission with ALL scope
128156 for (IAMPolicy policy : policies ) {
129- List <IAMPolicyPermission > pp = _iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .ALL .toString ());
130- if (pp != null && pp .size () > 0 ) {
131- return true ;
157+ List <IAMPolicyPermission > pp = new ArrayList <IAMPolicyPermission >();
158+ for (AccessType type : AccessType .values ()) {
159+ if (type .ordinal () >= accessType .ordinal ()) {
160+ pp .addAll (_iamService .listPolicyPermissionsByScope (policy .getId (), action , PermissionScope .ALL .toString (), type ));
161+ }
162+ if (pp != null && pp .size () > 0 ) {
163+ return true ;
164+ }
132165 }
133166 }
134167 return false ;
0 commit comments