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 static org.onap.aaf.misc.xgen.html.HTMLGen.TABLE;
26 import java.io.IOException;
27 import java.net.ConnectException;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.gui.AAF_GUI;
33 import org.onap.aaf.auth.gui.BreadCrumbs;
34 import org.onap.aaf.auth.gui.NamedCode;
35 import org.onap.aaf.auth.gui.Page;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.client.Future;
38 import org.onap.aaf.cadi.client.Rcli;
39 import org.onap.aaf.cadi.client.Retryable;
40 import org.onap.aaf.misc.env.APIException;
41 import org.onap.aaf.misc.env.Env;
42 import org.onap.aaf.misc.env.Slot;
43 import org.onap.aaf.misc.env.TimeTaken;
44 import org.onap.aaf.misc.xgen.Cache;
45 import org.onap.aaf.misc.xgen.DynamicCode;
46 import org.onap.aaf.misc.xgen.Mark;
47 import org.onap.aaf.misc.xgen.html.HTMLGen;
50 import aaf.v2_0.Roles;
52 public class PermGrantForm extends Page {
53 static final String HREF = "/gui/permgrant";
54 static final String NAME = "Permission Grant";
55 static final String fields[] = {"type","instance","action","role"};
57 public PermGrantForm(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
58 super(gui.env,NAME,HREF, fields,
59 new BreadCrumbs(breadcrumbs),
60 new NamedCode(true,"content") {
62 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
63 final Slot type = gui.env.slot(NAME+".type");
64 final Slot instance = gui.env.slot(NAME+".instance");
65 final Slot action = gui.env.slot(NAME+".action");
66 final Slot role = gui.env.slot(NAME+".role");
67 // p tags not closing right using .p() - causes issues in IE8 password form - so using leaf for the moment
68 hgen.leaf("p").text("Choose a role to grant to this permission").end()
69 .incr("form","method=post");
70 Mark table = new Mark(TABLE);
72 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
74 public void code(final AAF_GUI gui, final AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
76 Mark copyRoleJS = new Mark();
78 hgen.text("function copyRole(role) {");
79 hgen.text("var txtRole = document.querySelector(\"#role\");");
80 // hgen.text("if (role==;");
81 hgen.text("txtRole.value=role;");
85 String typeValue = trans.get(type, "");
86 String instanceValue = trans.get(instance, "");
87 String actionValue = trans.get(action, "");
88 String roleValue = trans.get(role,null);
89 List<String> myRoles = getMyRoles(gui, trans);
91 .input(fields[0],"Perm Type",true,"value="+typeValue,"disabled")
92 .input(fields[1],"Perm Instance",true,"value="+instanceValue,"disabled")
93 .input(fields[2],"Perm Action",true,"value="+actionValue,"disabled");
95 // select & options are not an input type, so we must create table row & cell tags
96 Mark selectRow = new Mark();
98 .incr(selectRow, "tr")
100 .incr("label", "for=myroles", "required").text("My Roles").end()
103 .incr("select", "name=myroles", "id=myroles", "onchange=copyRole(this.value)")
104 .incr("option", "value=").text("Select one of my roles").end();
105 for (String role : myRoles) {
106 hgen.incr("option", "value="+role).text(role).end();
109 .incr("option", "value=").text("Other").end()
111 if (roleValue==null) {
112 hgen.input(fields[3],"Role", true, "placeholder=or type a role here");
114 hgen.input(fields[3],"Role",true, "value="+roleValue);
120 hgen.tagOnly("input", "type=submit", "value=Submit")
127 private static List<String> getMyRoles(final AAF_GUI gui, final AuthzTrans trans) {
128 final List<String> myRoles = new ArrayList<>();
130 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
132 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
133 TimeTaken tt = trans.start("AAF get my roles",Env.REMOTE);
135 Future<Roles> fr = client.read("/authz/roles/user/"+trans.user(),gui.getDF(Roles.class));
138 tt = trans.start("Load Data", Env.SUB);
139 if (fr.value != null) for (Role r : fr.value.getRole()) {
140 myRoles.add(r.getName());
143 gui.writeError(trans, fr, null, 0);
151 } catch (Exception e) {