AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / role / User.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.role;
23
24 import org.onap.aaf.auth.cmd.AAFcli;
25 import org.onap.aaf.auth.cmd.Cmd;
26 import org.onap.aaf.auth.cmd.Param;
27 import org.onap.aaf.auth.rserv.HttpMethods;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.client.Future;
31 import org.onap.aaf.cadi.client.Rcli;
32 import org.onap.aaf.cadi.client.Retryable;
33 import org.onap.aaf.misc.env.APIException;
34
35 import aaf.v2_0.UserRoleRequest;
36
37 /**
38  * p
39  * @author Jonathan
40  *
41  */
42 public class User extends Cmd {
43         private final static String[] options = {"add","del","setTo","extend"};
44         public User(Role parent) {
45                 super(parent,"user", 
46                                 new Param(optionsToString(options),true),
47                                 new Param("role",true),
48                                 new Param("id[,id]* (not required for setTo)",false)); 
49         }
50
51         @Override
52         public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
53                 return same(new Retryable<Integer>() {
54                         @Override
55                         public Integer code(Rcli<?> client) throws CadiException, APIException {
56                                 int idx = index;
57                                 String action = args[idx++];
58                                 int option = whichOption(options, action);
59                                 UserRoleRequest urr = new UserRoleRequest();
60                                 urr.setRole(args[idx++]);
61                                 // Set Start/End commands
62                                 setStartEnd(urr);
63                                 
64                                 Future<?> fp = null;
65                                 
66                                 if (option != 2) {
67                                         String[] ids = args[idx++].split(",");
68                                         String verb=null,participle=null;
69                                         // You can request to be added or removed from role.
70                                         setQueryParamsOn(client);
71
72                                         for(String id: ids) {
73                                                 id=fullID(id);
74                                                 urr.setUser(id);
75                                                 switch(option) {
76                                                         case 0:
77                                                                 fp = client.create(
78                                                                                 "/authz/userRole", 
79                                                                                 getDF(UserRoleRequest.class), 
80                                                                                 urr);
81                                                                 verb = "Added";
82                                                                 participle = "] to Role [" ;
83                                                                 break;
84                                                         case 1:
85                                                                 fp = client.delete(
86                                                                                 "/authz/userRole/"+urr.getUser()+'/'+urr.getRole(), 
87                                                                                 Void.class);
88                                                                 verb = "Removed";
89                                                                 participle = "] from Role [" ;
90                                                                 break;
91                                                     case 3:
92                                                                 fp = client.update("/authz/userRole/extend/" + urr.getUser() + '/' + urr.getRole());
93                                                                 verb = "Extended";
94                                                                 participle = "] in Role [" ;
95                                                                 break;
96
97                                                         default: // actually, should never get here...
98                                                                 throw new CadiException("Invalid action [" + action + ']');
99                                                 }
100                                                 if(fp.get(AAFcli.timeout())) {
101                                                         pw().print(verb);
102                                                         pw().print(" User [");
103                                                         pw().print(urr.getUser());
104                                                         pw().print(participle);
105                                                         pw().print(urr.getRole());
106                                                         pw().println(']');
107                                                 } else {
108                                                         switch(fp.code()) {
109                                                                 case 202:
110                                                                         pw().print("User Role ");
111                                                                         pw().print(action);
112                                                                         pw().println(" is Accepted, but requires Approvals before actualizing");
113                                                                         break;
114                                                                 case 404:
115                                                                         if(option==3) {
116                                                                                 pw().println("Failed with code 404: UserRole is not found, or you do not have permission to view");
117                                                                                 break;
118                                                                         }
119                                                                 default:
120                                                                         error(fp);
121                                                         }
122                                                 }
123                                         }
124                                 } else {
125                                         String allUsers = "";
126                                         if (idx < args.length) 
127                                                 allUsers = args[idx++];
128                                         StringBuilder finalUsers = new StringBuilder(); 
129                                         for (String u : allUsers.split(",")) {
130                                                 if (u != "") {
131                                                         u=fullID(u);
132                                                         if (finalUsers.length() > 0) finalUsers.append(",");
133                                                         finalUsers.append(u);
134                                                 }
135                                         }
136
137                                         urr.setUser(finalUsers.toString());
138                                         fp = client.update(
139                                                         "/authz/userRole/role", 
140                                                         getDF(UserRoleRequest.class), 
141                                                         urr);
142                                         if(fp.get(AAFcli.timeout())) {
143                                                 pw().println("Set the Role to Users [" + allUsers + "]");
144                                         } else {
145                                                 error(fp);
146                                         }               
147                                 }
148                                 return fp==null?0:fp.code();
149                         }
150                 });
151         }
152         
153         @Override
154         public void detailedHelp(int indent, StringBuilder sb) {
155                 detailLine(sb,indent,"Add OR Delete a User to/from a Role OR");
156                 detailLine(sb,indent,"Set a User's Roles to the roles supplied");
157                 detailLine(sb,indent+2,"role  - Name of Role to create");
158                 detailLine(sb,indent+2,"id(s) - ID or IDs to add to the Role");
159                 sb.append('\n');
160                 detailLine(sb,indent+2,"Note: this is the same as \"user role add...\" except allows");
161                 detailLine(sb,indent+2,"assignment of role to multiple userss");
162                 detailLine(sb,indent+2,"WARNING: Users supplied with setTo will be the ONLY users attached to this role");
163                 detailLine(sb,indent+2,"If no users are supplied, the users attached to this role are reset.");
164                 api(sb,indent,HttpMethods.POST,"authz/userRole",UserRoleRequest.class,true);
165                 api(sb,indent,HttpMethods.DELETE,"authz/userRole/<user>/<role>",Void.class,false);
166                 api(sb,indent,HttpMethods.PUT,"authz/userRole/<role>",UserRoleRequest.class,false);
167         }
168
169 }