ListForCreds,Roles,Permissions-removing useless assignments
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / ListForPermission.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cmd.user;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.util.Collections;
27 import java.util.Comparator;
28
29 import org.onap.aaf.auth.cmd.AAFcli;
30 import org.onap.aaf.auth.cmd.Cmd;
31 import org.onap.aaf.auth.cmd.Param;
32 import org.onap.aaf.auth.rserv.HttpMethods;
33 import org.onap.aaf.cadi.CadiException;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Retryable;
38 import org.onap.aaf.cadi.config.Config;
39 import org.onap.aaf.misc.env.APIException;
40
41 import aaf.v2_0.Users;
42 import aaf.v2_0.Users.User;
43
44 /**
45  * p
46  * @author Jonathan
47  *
48  */
49 public class ListForPermission extends Cmd {
50     private static final String HEADER = "List Users for Permission";
51     public ListForPermission(List parent) {
52         super(parent,"perm",
53                 new Param("type",true),
54                 new Param("instance",true),
55                 new Param("action",true));
56     }
57
58     @Override
59     public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
60         return same(new Retryable<Integer>() {
61             @Override
62             public Integer code(Rcli<?> client) throws CadiException, APIException {
63                 int idx = index;
64                 String type = args[idx++];
65                 String instance = args[idx++];
66                 if ("\\*".equals(instance))instance="*";
67                 String action = args[idx];
68                 if ("\\*".equals(action))action="*";
69                 try {
70                     Future<Users> fp = client.read(
71                             "/authz/users/perm/" +
72                                 type + '/' +
73                                 URLEncoder.encode(instance,Config.UTF_8) + '/' +
74                                 action,
75                             getDF(Users.class)
76                             );
77                     if (fp.get(AAFcli.timeout())) {
78                         if (aafcli.isTest())
79                             Collections.sort(fp.value.getUser(), (Comparator<User>) (u1, u2) -> u1.getId().compareTo(u2.getId()));
80                         ((org.onap.aaf.auth.cmd.user.List)parent).report(fp.value,false,HEADER,type+"|"+instance+"|"+action);
81                         if (fp.code()==404)return 200;
82                     } else {
83                         error(fp);
84                     }
85                     return fp.code();
86                 } catch (UnsupportedEncodingException e) {
87                     throw new CadiException(e);
88                 }
89             }
90         });
91     }
92
93     @Override
94     public void detailedHelp(int _indent, StringBuilder sb) {
95             int indent = _indent;
96         detailLine(sb,indent,HEADER);
97         indent+=2;
98         detailLine(sb,indent,"This report lists the users associated to Permissions.  Since Users");
99         detailLine(sb,indent,"are associated to Roles, and Roles have Permissions, this report");
100         detailLine(sb,indent,"accomodates all these linkages.");
101         sb.append('\n');
102         detailLine(sb,indent,"The URL must contain the Permission's type,instance and action, and ");
103         detailLine(sb,indent,"may include \"*\"s (type in as \\\\*).");
104         detailLine(sb,indent,"See Perm Create Documentation for definitions.");
105         indent-=2;
106         api(sb,indent,HttpMethods.GET,"authz/users/perm/<type>/<instance>/<action>",Users.class,true);
107     }
108 }