1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cmd.user;
\r
25 import com.att.cadi.CadiException;
\r
26 import com.att.cadi.LocatorException;
\r
27 import com.att.cadi.client.Future;
\r
28 import com.att.cadi.client.Rcli;
\r
29 import com.att.cadi.client.Retryable;
\r
30 import com.att.cmd.AAFcli;
\r
31 import com.att.cmd.Cmd;
\r
32 import com.att.cmd.Param;
\r
33 import com.att.cssa.rserv.HttpMethods;
\r
34 import com.att.inno.env.APIException;
\r
36 import aaf.v2_0.UserRoleRequest;
\r
43 public class Role extends Cmd {
\r
44 private static final String[] options = {"add", "del", "setTo","extend"};
\r
45 public Role(User parent) {
\r
46 super(parent, "role", new Param(optionsToString(options), true), new Param("user", true), new Param(
\r
47 "role[,role]* (!REQ S)", false));
\r
51 public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
\r
52 return same(new Retryable<Integer>() {
\r
54 public Integer code(Rcli<?> client) throws CadiException, APIException {
\r
56 String key = args[idx++];
\r
57 int option = whichOption(options, key);
\r
58 String user = args[idx++];
\r
59 String realm = getOrgRealm();
\r
61 UserRoleRequest urr = new UserRoleRequest();
\r
62 if (user.indexOf('@') < 0 && realm != null) user += '@' + realm;
\r
64 // Set Start/End commands
\r
67 Future<?> fp = null;
\r
70 if (args.length < 5) {
\r
71 throw new CadiException(build(new StringBuilder("Too few args: "), null).toString());
\r
73 String[] roles = args[idx++].split(",");
\r
74 for (String role : roles) {
\r
75 String verb = null,participle=null;
\r
77 // You can request to be added or removed from role.
\r
78 setQueryParamsOn(client);
\r
81 fp = client.create("/authz/userRole", getDF(UserRoleRequest.class), urr);
\r
83 participle = "] to User [" ;
\r
86 fp = client.delete("/authz/userRole/" + urr.getUser() + '/' + urr.getRole(), Void.class);
\r
88 participle = "] from User [" ;
\r
91 fp = client.update("/authz/userRole/extend/" + urr.getUser() + '/' + urr.getRole());
\r
93 participle = "] to User [" ;
\r
96 throw new CadiException("Invalid action [" + key + ']');
\r
98 if (fp.get(AAFcli.timeout())) {
\r
100 pw().print(" Role [");
\r
101 pw().print(urr.getRole());
\r
102 pw().print(participle);
\r
103 pw().print(urr.getUser());
\r
106 switch(fp.code()) {
\r
108 pw().print("UserRole ");
\r
109 pw().print(option == 0 ? "Creation" : option==1?"Deletion":"Extension");
\r
110 pw().println(" Accepted, but requires Approvals before actualizing");
\r
114 pw().println("Failed with code 404: UserRole is not found, or you do not have permission to view");
\r
123 // option 2 is setTo command (an update call)
\r
124 String allRoles = "";
\r
125 if (idx < args.length)
\r
126 allRoles = args[idx++];
\r
128 urr.setRole(allRoles);
\r
129 fp = client.update("/authz/userRole/user", getDF(UserRoleRequest.class), urr);
\r
130 if (fp.get(AAFcli.timeout())) {
\r
131 pw().println("Set User's Roles to [" + allRoles + "]");
\r
136 return fp == null ? 0 : fp.code();
\r
142 public void detailedHelp(int indent, StringBuilder sb) {
\r
143 detailLine(sb, indent, "Add OR Delete a User to/from a Role OR");
\r
144 detailLine(sb, indent, "Set a User's Roles to the roles supplied");
\r
145 detailLine(sb, indent + 2, "user - ID of User");
\r
146 detailLine(sb, indent + 2, "role(s) - Role or Roles to which to add the User");
\r
148 detailLine(sb, indent + 2, "Note: this is the same as \"role user add...\" except allows");
\r
149 detailLine(sb, indent + 2, "assignment of user to multiple roles");
\r
150 detailLine(sb, indent + 2, "WARNING: Roles supplied with setTo will be the ONLY roles attached to this user");
\r
151 detailLine(sb, indent + 2, "If no roles are supplied, user's roles are reset.");
\r
152 api(sb, indent, HttpMethods.POST, "authz/userRole", UserRoleRequest.class, true);
\r
153 api(sb, indent, HttpMethods.DELETE, "authz/userRole/<user>/<role>", Void.class, false);
\r
154 api(sb, indent, HttpMethods.PUT, "authz/userRole/<user>", UserRoleRequest.class, false);
\r