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