Collection syntax change because of Sonar
[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                                                                 List<Role> roles = fr.value.getRole();
125                                                                 if(!roles.isEmpty()) {
126                                                                         Role role = fr.value.getRole().get(0);
127                                                                         trans.put(sRole, role);
128                                                                         Boolean mayWrite = trans.fish(new AAFPermission(role.getNs()+".access",":role:"+role.getName(),"write"));
129                                                                         trans.put(sMayWrite,mayWrite);
130                                                                         Boolean mayApprove = trans.fish(new AAFPermission(role.getNs()+".access",":role:"+role.getName(),"approve"));
131                                                                         trans.put(sMayApprove, mayApprove);
132                                                                         
133                                                                         if(mayWrite || mayApprove) {
134                                                                                 Mark js = new Mark();
135                                                                                 Mark fn = new Mark();
136                                                                                 hgen.js(js)
137                                                                                         .function(fn,"touchedDesc")
138                                                                                         .li("d=document.getElementById('descText');",
139                                                                                                 "if (d.orig == undefined ) {",
140                                                                                                 "  d.orig = d.value;",
141                                                                                                 "  d.addEventListener('keyup',changedDesc);",
142                                                                                                 "  d.removeEventListener('keypress',touchedDesc);",
143                                                                                                 "}").end(fn)
144                                                                                         .function(fn,"changedDesc")
145                                                                                         .li(
146                                                                                                 "dcb=document.getElementById('descCB');",
147                                                                                                 "d=document.getElementById('descText');",
148                                                                                                 "dcb.checked= (d.orig != d.value)"
149                                                                                         ).end(fn)
150                                                                                         .end(js);
151         
152                                                                                 Mark mark = new Mark();
153                                                                                 hgen.incr(mark,"form","method=post");
154                                                                                 trans.put(sMark, mark);
155                                                                         }
156                                                                 }
157                                                         } else {
158                                                                 trans.error().printf("Error calling AAF for Roles in GUI, Role Detail %d: %s",fr.code(),fr.body());
159                                                                 return false;
160                                                         }
161                                                         
162                                                         if(fur.get(AAF_GUI.TIMEOUT)) {
163                                                                 trans.put(sUserRole, fur.value.getUserRole());
164                                                         } else {
165                                                                 trans.error().printf("Error calling AAF for UserRoles in GUI, Role Detail %d: %s",fr.code(),fr.body());
166                                                                 return false;
167                                                         }
168
169                                                         return true;
170                                                 } finally {
171                                                         tt.done();
172                                                 }
173                                         }
174                                 });
175                         } catch (Exception e) {
176                                 trans.error().log(e);
177                         }
178                 }
179
180                 @Override
181                 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
182                         final String pRole = trans.get(sRoleName, null);
183                         final Role role = trans.get(sRole,null);
184                         ArrayList<AbsCell[]> rv = new ArrayList<>();
185                         
186                         if(role!=null) {
187                                 boolean mayWrite = trans.get(sMayWrite, false);
188                                 boolean mayApprove = trans.get(sMayApprove, false);
189
190                                 String desc = (role.getDescription()!=null?role.getDescription():BLANK);
191                                 rv.add(new AbsCell[]{
192                                                 new TextCell("Role:","width=45%"),
193                                                 new TextCell(pRole)});
194                                 if(mayWrite) {
195                                         rv.add(new AbsCell[]{
196                                                         new TextCell("Description:","width=45%"),
197                                                         new TextInputCell("description","textInput",desc,"id=descText","onkeypress=touchedDesc()"),
198                                                         new CheckBoxCell("desc",ALIGN.left, "changed","id=descCB", "style=visibility: hidden"),
199                                                         });
200                                         rv.add(AbsCell.HLINE);
201                                         rv.add(new AbsCell[] {
202                                                         new TextCell("Associated Permissions:","width=25%"),
203                                                         new TextCell("UnGrant","width=10%"),
204                                                 });
205                                 } else {
206                                         rv.add(new AbsCell[]{
207                                                         new TextCell("Description:","width=45%"),
208                                                         new TextCell(desc)});
209                                 }
210                                 boolean protectedRole = role.getName().endsWith(".owner") ||
211                                                                                 role.getName().endsWith(".admin");
212                                 boolean first = true;
213                                 for(Pkey r : role.getPerms()) {
214                                         String key=r.getType() + '|' + r.getInstance() + '|' + r.getAction();
215                                         if(mayWrite) {
216                                                 rv.add(new AbsCell[] {
217                                                         AbsCell.Null,
218                                                         protectedRole && r.getType().endsWith(".access")
219                                                                 ?new TextCell("protected","class=protected") // Do not allow ungranting of basic NS perms
220                                                                 :new CheckBoxCell("perm.ungrant",key),
221                                                         new TextCell("","width=10%"),
222                                                         new TextCell(key)
223                                                 });
224                                         } else {
225                                                 if(first) {
226                                                         rv.add(new AbsCell[] {
227                                                                         new TextCell("Associated Permissions:","width=45%"),
228                                                                         new TextCell(key)
229                                                                 });
230                                                         first=false;
231                                                 } else {
232                                                         rv.add(new AbsCell[] {
233                                                                         AbsCell.Null,
234                                                                         new TextCell(key)
235                                                         });
236                                                 }
237                                         }
238                                 }
239                                                 
240                                 if(mayApprove) {
241                                         rv.add(AbsCell.HLINE);
242
243                                         // 
244                                         rv.add(new AbsCell[] {
245                                                         new TextCell("Users in Role:","width=25%"),
246                                                         new TextCell("Delete","width=10%"),
247                                                         new TextCell("Extend","width=10%")
248                                                 });
249
250                                         List<UserRole> userroles = trans.get(sUserRole,null);
251                                         if(userroles!=null) {
252                                                 for(UserRole ur : userroles) {
253                                                         String tag = "userrole";
254                                                         
255                                                         rv.add(new AbsCell[] {
256                                                                 AbsCell.Null,
257                                                                 new CheckBoxCell(tag+".delete", ur.getUser()),
258                                                                 new CheckBoxCell(tag+".extend", ur.getUser()),
259                                                                 new TextCell(ur.getUser()),
260                                                                 new TextCell(Chrono.dateOnlyStamp(ur.getExpires())
261                                                         )});
262                                                 }
263                                         }
264                                 }
265                                                 
266                                 // History 
267                                 rv.add(new AbsCell[] {
268                                                 new RefCell("See History",RoleHistory.HREF + "?role=" + pRole,false)
269                                         });
270                         } else {
271                                 rv.add(new AbsCell[]{
272                                                 new TextCell("Role:"),
273                                                 new TextCell(pRole)});
274
275                                 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
276                         }
277                         return new Cells(rv, null);
278                 }
279
280                 /* (non-Javadoc)
281                  * @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)
282                  */
283                 @Override
284                 public void postfix(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) {
285                         final Mark mark = trans.get(sMark, null);
286                         if(mark!=null) {
287                                 hgen.tagOnly("input", "type=submit", "value=Submit");
288                                 final String pNS = trans.get(sNS, null);
289                                 if(pNS!=null && pNS.length()>0) {
290                                         hgen.leaf(mark,HTMLGen.A,"href="+NsDetail.HREF+"?ns="+pNS,"class=greenbutton").text("Back").end(mark);
291                                 }
292                                 hgen.end(mark);
293                         }
294
295                 }
296         }
297 }               
298