1818
1919import java .lang .reflect .Field ;
2020import java .util .ArrayList ;
21+ import java .util .Arrays ;
2122import java .util .HashMap ;
2223import java .util .HashSet ;
24+ import java .util .Iterator ;
2325import java .util .LinkedHashSet ;
2426import java .util .List ;
2527import java .util .Map ;
2830import javax .inject .Inject ;
2931
3032import org .apache .cloudstack .acl .APIChecker ;
33+ import org .apache .cloudstack .acl .Role ;
34+ import org .apache .cloudstack .acl .RoleService ;
35+ import org .apache .cloudstack .acl .RoleType ;
3136import org .apache .cloudstack .api .APICommand ;
3237import org .apache .cloudstack .api .BaseAsyncCmd ;
3338import org .apache .cloudstack .api .BaseAsyncCreateCmd ;
3439import org .apache .cloudstack .api .BaseCmd ;
3540import org .apache .cloudstack .api .BaseResponse ;
3641import org .apache .cloudstack .api .Parameter ;
37- import org .apache .cloudstack .acl .Role ;
38- import org .apache .cloudstack .acl .RoleService ;
39- import org .apache .cloudstack .acl .RoleType ;
4042import org .apache .cloudstack .api .command .user .discovery .ListApisCmd ;
4143import org .apache .cloudstack .api .response .ApiDiscoveryResponse ;
4244import org .apache .cloudstack .api .response .ApiParameterResponse ;
4345import org .apache .cloudstack .api .response .ApiResponseResponse ;
4446import org .apache .cloudstack .api .response .ListResponse ;
4547import org .apache .cloudstack .utils .reflectiontostringbuilderutils .ReflectionToStringBuilderUtils ;
48+ import org .apache .commons .collections .CollectionUtils ;
4649import org .apache .commons .lang3 .StringUtils ;
4750import org .apache .log4j .Logger ;
4851import org .reflections .ReflectionUtils ;
@@ -217,6 +220,9 @@ private ApiDiscoveryResponse getCmdRequestMap(Class<?> cmdClass, APICommand apiC
217220 paramResponse .setSince (parameterAnnotation .since ());
218221 }
219222 paramResponse .setRelated (parameterAnnotation .entityType ()[0 ].getName ());
223+ if (parameterAnnotation .authorized () != null ) {
224+ paramResponse .setAuthorizedRoleTypes (Arrays .asList (parameterAnnotation .authorized ()));
225+ }
220226 response .addParam (paramResponse );
221227 }
222228 }
@@ -249,6 +255,7 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
249255
250256 if (user == null )
251257 return null ;
258+ Account account = accountService .getAccount (user .getAccountId ());
252259
253260 if (name != null ) {
254261 if (!s_apiNameDiscoveryResponseMap .containsKey (name ))
@@ -262,10 +269,9 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
262269 return null ;
263270 }
264271 }
265- responseList .add (s_apiNameDiscoveryResponseMap . get (name ));
272+ responseList .add (getApiDiscoveryResponseWithAccessibleParams (name , account ));
266273
267274 } else {
268- Account account = accountService .getAccount (user .getAccountId ());
269275 if (account == null ) {
270276 throw new PermissionDeniedException (String .format ("The account with id [%s] for user [%s] is null." , user .getAccountId (), user ));
271277 }
@@ -286,13 +292,33 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
286292 }
287293
288294 for (String apiName : apisAllowed ) {
289- responseList .add (s_apiNameDiscoveryResponseMap . get (apiName ));
295+ responseList .add (getApiDiscoveryResponseWithAccessibleParams (apiName , account ));
290296 }
291297 }
292298 response .setResponses (responseList );
293299 return response ;
294300 }
295301
302+ private static ApiDiscoveryResponse getApiDiscoveryResponseWithAccessibleParams (String name , Account account ) {
303+ if (Account .Type .ADMIN .equals (account .getType ())) {
304+ return s_apiNameDiscoveryResponseMap .get (name );
305+ }
306+ ApiDiscoveryResponse apiDiscoveryResponse =
307+ new ApiDiscoveryResponse (s_apiNameDiscoveryResponseMap .get (name ));
308+ Iterator <ApiParameterResponse > iterator = apiDiscoveryResponse .getParams ().iterator ();
309+ while (iterator .hasNext ()) {
310+ ApiParameterResponse parameterResponse = iterator .next ();
311+ List <RoleType > authorizedRoleTypes = parameterResponse .getAuthorizedRoleTypes ();
312+ RoleType accountRoleType = RoleType .getByAccountType (account .getType ());
313+ if (CollectionUtils .isNotEmpty (parameterResponse .getAuthorizedRoleTypes ()) &&
314+ accountRoleType != null &&
315+ !authorizedRoleTypes .contains (accountRoleType )) {
316+ iterator .remove ();
317+ }
318+ }
319+ return apiDiscoveryResponse ;
320+ }
321+
296322 @ Override
297323 public List <Class <?>> getCommands () {
298324 List <Class <?>> cmdList = new ArrayList <Class <?>>();
0 commit comments