2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.cmd.user;
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;
35 import aaf.v2_0.UserRoleRequest;
43 public class Role extends Cmd {
44 private static final String[] options = {"add", "del", "extend"};
45 public Role(User parent) {
46 super(parent, "role", new Param(optionsToString(options), true), new Param("user", true),
47 new Param("role[,role]*", false));
51 public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
52 return same(new Retryable<Integer>() {
54 public Integer code(Rcli<?> client) throws CadiException, APIException {
56 String key = args[idx++];
57 int option = whichOption(options, key);
58 final String user = fullID(args[idx++]);
60 UserRoleRequest urr = new UserRoleRequest();
62 // Set Start/End commands
67 if (args.length < 5) {
68 throw new CadiException(build(new StringBuilder("Too few args: "), null).toString());
70 String[] roles = args[idx++].split(",");
71 for (String role : roles) {
72 String verb = null,participle=null;
74 // You can request to be added or removed from role.
75 setQueryParamsOn(client);
78 fp = client.create("/authz/userRole", getDF(UserRoleRequest.class), urr);
80 participle = "] to User [" ;
83 fp = client.delete("/authz/userRole/" + urr.getUser() + '/' + urr.getRole(), Void.class);
85 participle = "] from User [" ;
88 fp = client.update("/authz/userRole/extend/" + urr.getUser() + '/' + urr.getRole());
90 participle = "] to User [" ;
93 throw new CadiException("Invalid action [" + key + ']');
95 if (fp.get(AAFcli.timeout())) {
97 pw().print(" Role [");
98 pw().print(urr.getRole());
99 pw().print(participle);
100 pw().print(urr.getUser());
105 pw().print("UserRole ");
106 pw().print(option == 0 ? "Creation" : option==1?"Deletion":"Extension");
107 pw().println(" Accepted, but requires Approvals before actualizing");
111 pw().println("Failed with code 404: UserRole is not found, or you do not have permission to view");
119 return fp == null ? 0 : fp.code();
125 public void detailedHelp(int indent, StringBuilder sb) {
126 detailLine(sb, indent, "Add or Delete a User to/from a Role OR extend Expiration");
127 detailLine(sb, indent + 2, "user - ID of User");
128 detailLine(sb, indent + 2, "role(s) - Role or Roles to which to add the User");
130 api(sb, indent, HttpMethods.POST, "authz/userRole", UserRoleRequest.class, true);
131 api(sb, indent, HttpMethods.DELETE, "authz/userRole/<user>/<role>", Void.class, false);
132 api(sb,indent,HttpMethods.PUT,"authz/userRole/extend/<user>/<role>",Void.class,false);