AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / RoleDetail.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 java.io.IOException;
25 import java.net.ConnectException;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.onap.aaf.auth.env.AuthzEnv;
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.auth.gui.AAF_GUI;
32 import org.onap.aaf.auth.gui.BreadCrumbs;
33 import org.onap.aaf.auth.gui.Page;
34 import org.onap.aaf.auth.gui.Table;
35 import org.onap.aaf.auth.gui.Table.Cells;
36 import org.onap.aaf.auth.gui.table.AbsCell;
37 import org.onap.aaf.auth.gui.table.CheckBoxCell;
38 import org.onap.aaf.auth.gui.table.CheckBoxCell.ALIGN;
39 import org.onap.aaf.auth.gui.table.RefCell;
40 import org.onap.aaf.auth.gui.table.TableData;
41 import org.onap.aaf.auth.gui.table.TextCell;
42 import org.onap.aaf.auth.gui.table.TextInputCell;
43 import org.onap.aaf.auth.validation.Validator;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.aaf.AAFPermission;
46 import org.onap.aaf.cadi.client.Future;
47 import org.onap.aaf.cadi.client.Rcli;
48 import org.onap.aaf.cadi.client.Retryable;
49 import org.onap.aaf.misc.env.APIException;
50 import org.onap.aaf.misc.env.Env;
51 import org.onap.aaf.misc.env.Slot;
52 import org.onap.aaf.misc.env.TimeTaken;
53 import org.onap.aaf.misc.env.util.Chrono;
54 import org.onap.aaf.misc.xgen.Cache;
55 import org.onap.aaf.misc.xgen.Mark;
56 import org.onap.aaf.misc.xgen.html.HTMLGen;
57
58 import aaf.v2_0.Pkey;
59 import aaf.v2_0.Role;
60 import aaf.v2_0.Roles;
61 import aaf.v2_0.UserRole;
62 import aaf.v2_0.UserRoles;
63
64 /**
65  * Detail Page for Permissions
66  * 
67  * @author Jonathan
68  *
69  */
70 public class RoleDetail extends Page {
71         public static final String HREF = "/gui/roledetail";
72         public static final String NAME = "RoleDetail";
73         private static final String BLANK = "";
74
75         public RoleDetail(final AAF_GUI gui, Page ... breadcrumbs) throws APIException, IOException {
76                 super(gui.env, NAME, HREF, new String[] {"role","ns"},
77                                 new BreadCrumbs(breadcrumbs),
78                                 new Table<AAF_GUI,AuthzTrans>("Role Details",gui.env.newTransNoAvg(),
79                                                 new Model(gui.env),"class=detail")
80                         );
81         }
82
83         /**
84          * Implement the table content for Permissions Detail
85          * 
86          * @author Jonathan
87          *
88          */
89         private static class Model extends TableData<AAF_GUI,AuthzTrans> {
90                 private Slot sRoleName,sRole,sUserRole,sMayWrite,sMayApprove,sMark,sNS;
91                 public Model(AuthzEnv env) {
92                         sRoleName = env.slot(NAME+".role");
93                         sRole = env.slot(NAME+".data.role");
94                         sUserRole = env.slot(NAME+".data.userrole");
95                         sMayWrite = env.slot(NAME+"mayWrite");
96                         sMayApprove = env.slot(NAME+"mayApprove");
97                         sMark = env.slot(NAME+"mark");
98                         sNS = env.slot(NAME+".ns");
99                 }
100
101                 /* (non-Javadoc)
102                  * @see org.onap.aaf.auth.gui.table.TableData#prefix(org.onap.aaf.misc.xgen.html.State, com.att.inno.env.Trans, org.onap.aaf.misc.xgen.Cache, org.onap.aaf.misc.xgen.html.HTMLGen)
103                  */
104                 @Override
105                 public void prefix(final AAF_GUI gui, final AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) {
106                         final String pRole = trans.get(sRoleName, null);
107                         Validator v = new Validator();
108                         v.role(pRole);
109                         if(v.err()) {
110                                 trans.warn().printf("Error in PermDetail Request: %s", v.errs());
111                                 return;
112                         }
113
114                 
115                         try { 
116                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
117                                         @Override
118                                         public Boolean code(Rcli<?> client) throws CadiException, ConnectException, APIException {
119                                                 TimeTaken tt = trans.start("AAF Role Details",Env.REMOTE);
120                                                 try {
121                                                         Future<Roles> fr = client.read("/authz/roles/"+pRole+"?ns",gui.getDF(Roles.class));
122                                                         Future<UserRoles> fur = client.read("/authz/userRoles/role/"+pRole,gui.getDF(UserRoles.class));
123                                                         if(fr.get(AAF_GUI.TIMEOUT)) {
124                                                                 Role role = fr.value.getRole().get(0);
125                                                                 trans.put(sRole, role);
126                                                                 Boolean mayWrite = trans.fish(new AAFPermission(role.getNs()+".access",":role:"+role.getName(),"write"));
127                                                                 trans.put(sMayWrite,mayWrite);
128                                                                 Boolean mayApprove = trans.fish(new AAFPermission(role.getNs()+".access",":role:"+role.getName(),"approve"));
129                                                                 trans.put(sMayApprove, mayApprove);
130                                                                 
131                                                                 if(mayWrite || mayApprove) {
132                                                                         Mark js = new Mark();
133                                                                         Mark fn = new Mark();
134                                                                         hgen.js(js)
135                                                                                 .function(fn,"touchedDesc")
136                                                                                 .li("d=document.getElementById('descText');",
137                                                                                         "if (d.orig == undefined ) {",
138                                                                                         "  d.orig = d.value;",
139                                                                                         "  d.addEventListener('keyup',changedDesc);",
140                                                                                         "  d.removeEventListener('keypress',touchedDesc);",
141                                                                                         "}").end(fn)
142                                                                                 .function(fn,"changedDesc")
143                                                                                 .li(
144                                                                                         "dcb=document.getElementById('descCB');",
145                                                                                         "d=document.getElementById('descText');",
146                                                                                         "dcb.checked= (d.orig != d.value)"
147                                                                                 ).end(fn)
148                                                                                 .end(js);
149
150                                                                         Mark mark = new Mark();
151                                                                         hgen.incr(mark,"form","method=post");
152                                                                         trans.put(sMark, mark);
153                                                                 }
154                                                         } else {
155                                                                 trans.error().printf("Error calling AAF for Roles in GUI, Role Detail %d: %s",fr.code(),fr.body());
156                                                                 return false;
157                                                         }
158                                                         
159                                                         if(fur.get(AAF_GUI.TIMEOUT)) {
160                                                                 trans.put(sUserRole, fur.value.getUserRole());
161                                                         } else {
162                                                                 trans.error().printf("Error calling AAF for UserRoles in GUI, Role Detail %d: %s",fr.code(),fr.body());
163                                                                 return false;
164                                                         }
165
166                                                         return true;
167                                                 } finally {
168                                                         tt.done();
169                                                 }
170                                         }
171                                 });
172                         } catch (Exception e) {
173                                 trans.error().log(e);
174                         }
175                 }
176
177                 @Override
178                 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
179                         final String pRole = trans.get(sRoleName, null);
180                         final Role role = trans.get(sRole,null);
181                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
182                         
183                         if(role!=null) {
184                                 boolean mayWrite = trans.get(sMayWrite, false);
185                                 boolean mayApprove = trans.get(sMayApprove, false);
186
187                                 String desc = (role.getDescription()!=null?role.getDescription():BLANK);
188                                 rv.add(new AbsCell[]{
189                                                 new TextCell("Role:","width=45%"),
190                                                 new TextCell(pRole)});
191                                 if(mayWrite) {
192                                         rv.add(new AbsCell[]{
193                                                         new TextCell("Description:","width=45%"),
194                                                         new TextInputCell("description","textInput",desc,"id=descText","onkeypress=touchedDesc()"),
195                                                         new CheckBoxCell("desc",ALIGN.left, "changed","id=descCB", "style=visibility: hidden"),
196                                                         });
197                                         rv.add(AbsCell.HLINE);
198                                         rv.add(new AbsCell[] {
199                                                         new TextCell("Associated Permissions:","width=25%"),
200                                                         new TextCell("UnGrant","width=10%"),
201                                                 });
202                                 } else {
203                                         rv.add(new AbsCell[]{
204                                                         new TextCell("Description:","width=45%"),
205                                                         new TextCell(desc)});
206                                 }
207                                 boolean protectedRole = role.getName().endsWith(".owner") ||
208                                                                                 role.getName().endsWith(".admin");
209                                 boolean first = true;
210                                 for(Pkey r : role.getPerms()) {
211                                         String key=r.getType() + '|' + r.getInstance() + '|' + r.getAction();
212                                         if(mayWrite) {
213                                                 rv.add(new AbsCell[] {
214                                                         AbsCell.Null,
215                                                         protectedRole && r.getType().endsWith(".access")
216                                                                 ?new TextCell("protected","class=protected") // Do not allow ungranting of basic NS perms
217                                                                 :new CheckBoxCell("perm.ungrant",key),
218                                                         new TextCell("","width=10%"),
219                                                         new TextCell(key)
220                                                 });
221                                         } else {
222                                                 if(first) {
223                                                         rv.add(new AbsCell[] {
224                                                                         new TextCell("Associated Permissions:","width=45%"),
225                                                                         new TextCell(key)
226                                                                 });
227                                                         first=false;
228                                                 } else {
229                                                         rv.add(new AbsCell[] {
230                                                                         AbsCell.Null,
231                                                                         new TextCell(key)
232                                                         });
233                                                 }
234                                         }
235                                 }
236                                                 
237                                 if(mayApprove) {
238                                         rv.add(AbsCell.HLINE);
239
240                                         // 
241                                         rv.add(new AbsCell[] {
242                                                         new TextCell("Users in Role:","width=25%"),
243                                                         new TextCell("Delete","width=10%"),
244                                                         new TextCell("Extend","width=10%")
245                                                 });
246
247                                         List<UserRole> userroles = trans.get(sUserRole,null);
248                                         if(userroles!=null) {
249                                                 for(UserRole ur : userroles) {
250                                                         String tag = "userrole";
251                                                         
252                                                         rv.add(new AbsCell[] {
253                                                                 AbsCell.Null,
254                                                                 new CheckBoxCell(tag+".delete", ur.getUser()),
255                                                                 new CheckBoxCell(tag+".extend", ur.getUser()),
256                                                                 new TextCell(ur.getUser()),
257                                                                 new TextCell(Chrono.dateOnlyStamp(ur.getExpires())
258                                                         )});
259                                                 }
260                                         }
261                                 }
262                                                 
263                                 // History 
264                                 rv.add(new AbsCell[] {
265                                                 new RefCell("See History",RoleHistory.HREF + "?role=" + pRole,false)
266                                         });
267                         } else {
268                                 rv.add(new AbsCell[]{
269                                                 new TextCell("Role:"),
270                                                 new TextCell(pRole)});
271
272                                 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
273                         }
274                         return new Cells(rv, null);
275                 }
276
277                 /* (non-Javadoc)
278                  * @see org.onap.aaf.auth.gui.table.TableData#postfix(org.onap.aaf.misc.xgen.html.State, com.att.inno.env.Trans, org.onap.aaf.misc.xgen.Cache, org.onap.aaf.misc.xgen.html.HTMLGen)
279                  */
280                 @Override
281                 public void postfix(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) {
282                         final Mark mark = trans.get(sMark, null);
283                         if(mark!=null) {
284                                 hgen.tagOnly("input", "type=submit", "value=Submit");
285                                 final String pNS = trans.get(sNS, null);
286                                 if(pNS!=null && pNS.length()>0) {
287                                         hgen.leaf(mark,HTMLGen.A,"href="+NsDetail.HREF+"?ns="+pNS,"class=greenbutton").text("Back").end(mark);
288                                 }
289                                 hgen.end(mark);
290                         }
291
292                 }
293         }
294 }               
295