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.gui.pages;
24 import java.io.IOException;
25 import java.net.ConnectException;
26 import java.util.ArrayList;
27 import java.util.List;
29 import java.util.Map.Entry;
31 import javax.servlet.http.HttpServletRequest;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.gui.AAF_GUI;
35 import org.onap.aaf.auth.gui.BreadCrumbs;
36 import org.onap.aaf.auth.gui.NamedCode;
37 import org.onap.aaf.auth.gui.Page;
38 import org.onap.aaf.auth.gui.table.TableData;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.client.Future;
41 import org.onap.aaf.cadi.client.Rcli;
42 import org.onap.aaf.cadi.client.Retryable;
43 import org.onap.aaf.cadi.util.Split;
44 import org.onap.aaf.misc.env.APIException;
45 import org.onap.aaf.misc.env.Env;
46 import org.onap.aaf.misc.env.Slot;
47 import org.onap.aaf.misc.env.TimeTaken;
48 import org.onap.aaf.misc.xgen.Cache;
49 import org.onap.aaf.misc.xgen.DynamicCode;
50 import org.onap.aaf.misc.xgen.html.HTMLGen;
53 import aaf.v2_0.RolePermRequest;
54 import aaf.v2_0.RoleRequest;
56 public class RoleDetailAction extends Page {
57 public RoleDetailAction(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
58 super(gui.env,RoleDetail.NAME, RoleDetail.HREF, TableData.headers,
59 new BreadCrumbs(breadcrumbs),
60 new NamedCode(true,"content") {
61 final Slot sReq = gui.env.slot(AAF_GUI.HTTP_SERVLET_REQUEST);
64 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
65 cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI, AuthzTrans>() {
67 public void code(final AAF_GUI gui, final AuthzTrans trans,final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
68 final HttpServletRequest req = trans.get(sReq, null);
69 final String role = getSingleParam(req,"role");
71 hgen.text("Parameter 'role' is required").end();
76 /*fail =*/ gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
78 public Boolean code(Rcli<?> client) throws CadiException, ConnectException, APIException {
79 List<TypedFuture> ltf = new ArrayList<>();
81 Map<String, String[]> pm = (Map<String, String[]>)req.getParameterMap();
82 for (final Entry<String, String[]> es : pm.entrySet()) {
83 for (final String v : es.getValue()) {
87 case "desc": // Check box set
88 String desc = getSingleParam(req, "description");
90 text = "Setting Description on " + role + " to " + desc;
91 tt = trans.start(text, Env.REMOTE);
92 RoleRequest rr = new RoleRequest();
94 rr.setDescription(desc);
95 ltf.add(new TypedFuture(ActionType.desc, text,
96 client.update("/authz/role",
97 gui.getDF(RoleRequest.class),rr
102 text = "Ungranting Permission '" + v + "' from '" + role + '\'';
103 tt = trans.start(text, Env.REMOTE);
104 String[] pf = Split.splitTrim('|', v);
106 Pkey perm = new Pkey();
108 perm.setInstance(pf[1]);
109 perm.setAction(pf[2]);
110 RolePermRequest rpr = new RolePermRequest();
113 ltf.add(new TypedFuture(ActionType.ungrant,text,
114 client.delete("/authz/role/" + role + "/perm",
115 gui.getDF(RolePermRequest.class),rpr
118 hgen.p(v + " is not a valid Perm for ungranting");
121 case "userrole.extend":
122 text = "Extending " + v + " in " + role;
123 tt = trans.start(text, Env.REMOTE);
124 ltf.add(new TypedFuture(ActionType.extendUR,text,
125 client.update("/authz/userRole/extend/" + v + '/' + role)));
127 case "userrole.delete":
128 text = "Deleting " + v + " from " + role;
129 tt = trans.start(text, Env.REMOTE);
130 ltf.add(new TypedFuture(ActionType.deleteUR,text,
131 client.delete("/authz/userRole/" + v + '/' + role, Void.class)));
135 // System.out.println(es.getKey() + "=" + v);
147 hgen.p("No Changes");
149 for (TypedFuture tf : ltf) {
150 if (tf.future.get(5000)) {
151 hgen.p("<font color=\"green\"><i>Success</i>:</font> " + tf.text);
153 // Note: if handling of special Error codes is required, use
157 gui.writeError(trans, tf.future, hgen,4);
164 } catch (Exception e) {
165 hgen.p("Unknown Error");
176 enum ActionType {desc, ungrant, deleteUR, extendUR};
177 private static class TypedFuture {
178 // public final ActionType type;
179 public final Future<?> future;
180 public final String text;
182 public TypedFuture(ActionType type, String text, Future<?> future) {
184 this.future = future;