7195806050143c2b37ee29d120c5751a1be3635e
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / PermGrantForm.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.gui.pages;
23
24 import static org.onap.aaf.misc.xgen.html.HTMLGen.TABLE;
25
26 import java.io.IOException;
27 import java.net.ConnectException;
28 import java.util.ArrayList;
29 import java.util.List;
30
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;
48
49 import aaf.v2_0.Role;
50 import aaf.v2_0.Roles;
51
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"};
56     
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") {
61             @Override
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);
71                 hgen.incr(table);
72                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
73                     @Override
74                     public void code(final AAF_GUI gui, final AuthzTrans trans,    final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {
75                         
76                         Mark copyRoleJS = new Mark();
77                         hgen.js(copyRoleJS);
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;");
82                         hgen.text("}");
83                         hgen.end(copyRoleJS);
84                         
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);
90                         hgen
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");
94                         
95                         // select & options are not an input type, so we must create table row & cell tags
96                         Mark selectRow = new Mark();
97                         hgen
98                         .incr(selectRow, "tr")
99                         .incr("td")
100                         .incr("label", "for=myroles", "required").text("My Roles").end()
101                         .end()
102                         .incr("td")
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();
107                         }
108                         hgen
109                         .incr("option", "value=").text("Other").end()                    
110                         .end(selectRow);
111                         if (roleValue==null) {
112                             hgen.input(fields[3],"Role", true, "placeholder=or type a role here");
113                         } else {
114                             hgen.input(fields[3],"Role",true, "value="+roleValue);
115                         }
116                         hgen.end();
117                     }
118                 });
119                 hgen.end();
120                 hgen.tagOnly("input", "type=submit", "value=Submit")
121                 .end();
122
123             }
124         });
125     }
126         
127     private static List<String> getMyRoles(final AAF_GUI gui, final AuthzTrans trans) {
128         final List<String> myRoles = new ArrayList<>();
129         try {
130             gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
131                 @Override
132                 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
133                     TimeTaken tt = trans.start("AAF get my roles",Env.REMOTE);
134                     try {
135                         Future<Roles> fr = client.read("/authz/roles/user/"+trans.user(),gui.getDF(Roles.class));
136                         if (fr.get(5000)) {
137                             tt.done();
138                             tt = trans.start("Load Data", Env.SUB);
139                             if (fr.value != null) for (Role r : fr.value.getRole()) {
140                                 myRoles.add(r.getName());
141                             }
142                         } else {
143                             gui.writeError(trans, fr, null, 0);
144                         }
145                     } finally {
146                         tt.done();
147                     }
148                     return null;
149                 }
150             });
151         } catch (Exception e) {
152             e.printStackTrace();
153         }
154
155         return myRoles;
156     }
157 }