b3b51f63d209eece461b1a3d087630756d1be512
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / PermGrantForm.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import static com.att.xgen.html.HTMLGen.TABLE;
7
8 import java.io.IOException;
9 import java.net.ConnectException;
10 import java.util.ArrayList;
11 import java.util.List;
12
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 org.onap.aaf.cadi.CadiException;
19 import org.onap.aaf.cadi.client.Future;
20 import org.onap.aaf.cadi.client.Rcli;
21 import org.onap.aaf.cadi.client.Retryable;
22 import org.onap.aaf.inno.env.APIException;
23 import org.onap.aaf.inno.env.Env;
24 import org.onap.aaf.inno.env.Slot;
25 import org.onap.aaf.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;
30
31 import aaf.v2_0.Role;
32 import aaf.v2_0.Roles;
33
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"};
38         
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") {
43                         @Override
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);
53                                 hgen.incr(table);
54                                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AuthGUI, AuthzTrans>() {
55                                         @Override
56                                         public void code(AuthGUI gui, AuthzTrans trans, Cache<HTMLGen> cache, HTMLGen hgen)     throws APIException, IOException {
57                                                 
58                                                 Mark copyRoleJS = new Mark();
59                                                 hgen.js(copyRoleJS);
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;");
64                                                 hgen.text("}");
65                                                 hgen.end(copyRoleJS);
66                                                 
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);
72                                                 hgen
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");
76                                                 
77                                                 // select & options are not an input type, so we must create table row & cell tags
78                                                 Mark selectRow = new Mark();
79                                                 hgen
80                                                 .incr(selectRow, "tr")
81                                                 .incr("td")
82                                                 .incr("label", "for=myroles", "required").text("My Roles").end()
83                                                 .end()
84                                                 .incr("td")
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();
89                                                 }
90                                                 hgen
91                                                 .incr("option", "value=").text("Other").end()                                   
92                                                 .end(selectRow);
93                                                 if(roleValue==null) {
94                                                         hgen.input(fields[3],"Role", true, "placeholder=or type a role here");
95                                                 } else {
96                                                         hgen.input(fields[3],"Role",true, "value="+roleValue);
97                                                 }
98                                                 hgen.end();
99                                         }
100                                 });
101                                 hgen.end();
102                                 hgen.tagOnly("input", "type=submit", "value=Submit")
103                                 .end();
104
105                         }
106                 });
107         }
108                 
109         private static List<String> getMyRoles(final AuthGUI gui, final AuthzTrans trans) {
110                 List<String> myRoles = new ArrayList<String>();
111                 try {
112                         gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
113                                 @Override
114                                 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
115                                         TimeTaken tt = trans.start("AAF get my roles",Env.REMOTE);
116                                         try {
117                                                 Future<Roles> fr = client.read("/authz/roles/user/"+trans.user(),gui.rolesDF);
118                                                 if(fr.get(5000)) {
119                                                         tt.done();
120                                                         tt = trans.start("Load Data", Env.SUB);
121                                                         if (fr.value != null) for (Role r : fr.value.getRole()) {
122                                                                 myRoles.add(r.getName());
123                                                         }
124                                                 } else {
125                                                         gui.writeError(trans, fr, null);
126                                                 }
127                                         } finally {
128                                                 tt.done();
129                                         }
130                                         return null;
131                                 }
132                         });
133                 } catch (Exception e) {
134                         e.printStackTrace();
135                 }
136
137                 return myRoles;
138         }
139 }