1 /*******************************************************************************
2 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3 *******************************************************************************/
4 package com.att.authz.gui.pages;
6 import static com.att.xgen.html.HTMLGen.TABLE;
8 import java.io.IOException;
9 import java.net.ConnectException;
10 import java.util.ArrayList;
11 import java.util.List;
13 import com.att.authz.env.AuthzTrans;
14 import com.att.authz.gui.AuthGUI;
15 import com.att.authz.gui.BreadCrumbs;
16 import com.att.authz.gui.NamedCode;
17 import com.att.authz.gui.Page;
18 import com.att.cadi.CadiException;
19 import com.att.cadi.client.Future;
20 import com.att.cadi.client.Rcli;
21 import com.att.cadi.client.Retryable;
22 import com.att.inno.env.APIException;
23 import com.att.inno.env.Env;
24 import com.att.inno.env.Slot;
25 import com.att.inno.env.TimeTaken;
26 import com.att.xgen.Cache;
27 import com.att.xgen.DynamicCode;
28 import com.att.xgen.Mark;
29 import com.att.xgen.html.HTMLGen;
32 import aaf.v2_0.Roles;
34 public class PermGrantForm extends Page {
35 static final String HREF = "/gui/permgrant";
36 static final String NAME = "Permission Grant";
37 static final String fields[] = {"type","instance","action","role"};
39 public PermGrantForm(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
40 super(gui.env,NAME,HREF, fields,
41 new BreadCrumbs(breadcrumbs),
42 new NamedCode(true,"content") {
44 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
45 final Slot type = gui.env.slot(NAME+".type");
46 final Slot instance = gui.env.slot(NAME+".instance");
47 final Slot action = gui.env.slot(NAME+".action");
48 final Slot role = gui.env.slot(NAME+".role");
49 // p tags not closing right using .p() - causes issues in IE8 password form - so using leaf for the moment
50 hgen.leaf("p").text("Choose a role to grant to this permission").end()
51 .incr("form","method=post");
52 Mark table = new Mark(TABLE);
54 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
56 public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
58 Mark copyRoleJS = new Mark();
60 hgen.text("function copyRole(role) {");
61 hgen.text("var txtRole = document.querySelector(\"#role\");");
62 // hgen.text("if (role==;");
63 hgen.text("txtRole.value=role;");
67 String typeValue = trans.get(type, "");
68 String instanceValue = trans.get(instance, "");
69 String actionValue = trans.get(action, "");
70 String roleValue = trans.get(role,null);
71 List<String> myRoles = getMyRoles(gui, trans);
73 .input(fields[0],"Perm Type",true,"value="+typeValue,"disabled")
74 .input(fields[1],"Perm Instance",true,"value="+instanceValue,"disabled")
75 .input(fields[2],"Perm Action",true,"value="+actionValue,"disabled");
77 // select & options are not an input type, so we must create table row & cell tags
78 Mark selectRow = new Mark();
80 .incr(selectRow, "tr")
82 .incr("label", "for=myroles", "required").text("My Roles").end()
85 .incr("select", "name=myroles", "id=myroles", "onchange=copyRole(this.value)")
86 .incr("option", "value=").text("Select one of my roles").end();
87 for (String role : myRoles) {
88 hgen.incr("option", "value="+role).text(role).end();
91 .incr("option", "value=").text("Other").end()
94 hgen.input(fields[3],"Role", true, "placeholder=or type a role here");
96 hgen.input(fields[3],"Role",true, "value="+roleValue);
102 hgen.tagOnly("input", "type=submit", "value=Submit")
109 private static List<String> getMyRoles(final AuthGUI gui, final AuthzTrans trans) {
110 List<String> myRoles = new ArrayList<String>();
112 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
114 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
115 TimeTaken tt = trans.start("AAF get my roles",Env.REMOTE);
117 Future<Roles> fr = client.read("/authz/roles/user/"+trans.user(),gui.rolesDF);
120 tt = trans.start("Load Data", Env.SUB);
121 if (fr.value != null) for (Role r : fr.value.getRole()) {
122 myRoles.add(r.getName());
125 gui.writeError(trans, fr, null);
133 } catch (Exception e) {