2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.gui.pages;
24 import java.io.IOException;
25 import java.net.ConnectException;
26 import java.util.ArrayList;
27 import java.util.List;
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;
60 import aaf.v2_0.Roles;
61 import aaf.v2_0.UserRole;
62 import aaf.v2_0.UserRoles;
65 * Detail Page for Permissions
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 = "";
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")
84 * Implement the table content for Permissions Detail
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");
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)
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();
110 trans.warn().printf("Error in PermDetail Request: %s", v.errs());
116 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
118 public Boolean code(Rcli<?> client) throws CadiException, ConnectException, APIException {
119 TimeTaken tt = trans.start("AAF Role Details",Env.REMOTE);
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);
131 if(mayWrite || mayApprove) {
132 Mark js = new Mark();
133 Mark fn = new Mark();
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);",
142 .function(fn,"changedDesc")
144 "dcb=document.getElementById('descCB');",
145 "d=document.getElementById('descText');",
146 "dcb.checked= (d.orig != d.value)"
150 Mark mark = new Mark();
151 hgen.incr(mark,"form","method=post");
152 trans.put(sMark, mark);
155 trans.error().printf("Error calling AAF for Roles in GUI, Role Detail %d: %s",fr.code(),fr.body());
159 if(fur.get(AAF_GUI.TIMEOUT)) {
160 trans.put(sUserRole, fur.value.getUserRole());
162 trans.error().printf("Error calling AAF for UserRoles in GUI, Role Detail %d: %s",fr.code(),fr.body());
172 } catch (Exception e) {
173 trans.error().log(e);
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[]>();
184 boolean mayWrite = trans.get(sMayWrite, false);
185 boolean mayApprove = trans.get(sMayApprove, false);
187 String desc = (role.getDescription()!=null?role.getDescription():BLANK);
188 rv.add(new AbsCell[]{
189 new TextCell("Role:","width=45%"),
190 new TextCell(pRole)});
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"),
197 rv.add(AbsCell.HLINE);
198 rv.add(new AbsCell[] {
199 new TextCell("Associated Permissions:","width=25%"),
200 new TextCell("UnGrant","width=10%"),
203 rv.add(new AbsCell[]{
204 new TextCell("Description:","width=45%"),
205 new TextCell(desc)});
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();
213 rv.add(new AbsCell[] {
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%"),
223 rv.add(new AbsCell[] {
224 new TextCell("Associated Permissions:","width=45%"),
229 rv.add(new AbsCell[] {
238 rv.add(AbsCell.HLINE);
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%")
247 List<UserRole> userroles = trans.get(sUserRole,null);
248 if(userroles!=null) {
249 for(UserRole ur : userroles) {
250 String tag = "userrole";
252 rv.add(new AbsCell[] {
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())
264 rv.add(new AbsCell[] {
265 new RefCell("See History",RoleHistory.HREF + "?role=" + pRole,false)
268 rv.add(new AbsCell[]{
269 new TextCell("Role:"),
270 new TextCell(pRole)});
272 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
274 return new Cells(rv, null);
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)
281 public void postfix(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) {
282 final Mark mark = trans.get(sMark, 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);