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 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);
133 if(mayWrite || mayApprove) {
134 Mark js = new Mark();
135 Mark fn = new Mark();
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);",
144 .function(fn,"changedDesc")
146 "dcb=document.getElementById('descCB');",
147 "d=document.getElementById('descText');",
148 "dcb.checked= (d.orig != d.value)"
152 Mark mark = new Mark();
153 hgen.incr(mark,"form","method=post");
154 trans.put(sMark, mark);
158 trans.error().printf("Error calling AAF for Roles in GUI, Role Detail %d: %s",fr.code(),fr.body());
162 if(fur.get(AAF_GUI.TIMEOUT)) {
163 trans.put(sUserRole, fur.value.getUserRole());
165 trans.error().printf("Error calling AAF for UserRoles in GUI, Role Detail %d: %s",fr.code(),fr.body());
175 } catch (Exception e) {
176 trans.error().log(e);
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<>();
187 boolean mayWrite = trans.get(sMayWrite, false);
188 boolean mayApprove = trans.get(sMayApprove, false);
190 String desc = (role.getDescription()!=null?role.getDescription():BLANK);
191 rv.add(new AbsCell[]{
192 new TextCell("Role:","width=45%"),
193 new TextCell(pRole)});
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"),
200 rv.add(AbsCell.HLINE);
201 rv.add(new AbsCell[] {
202 new TextCell("Associated Permissions:","width=25%"),
203 new TextCell("UnGrant","width=10%"),
206 rv.add(new AbsCell[]{
207 new TextCell("Description:","width=45%"),
208 new TextCell(desc)});
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();
216 rv.add(new AbsCell[] {
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%"),
226 rv.add(new AbsCell[] {
227 new TextCell("Associated Permissions:","width=45%"),
232 rv.add(new AbsCell[] {
241 rv.add(AbsCell.HLINE);
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%")
250 List<UserRole> userroles = trans.get(sUserRole,null);
251 if(userroles!=null) {
252 for(UserRole ur : userroles) {
253 String tag = "userrole";
255 rv.add(new AbsCell[] {
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())
267 rv.add(new AbsCell[] {
268 new RefCell("See History",RoleHistory.HREF + "?role=" + pRole,false)
271 rv.add(new AbsCell[]{
272 new TextCell("Role:"),
273 new TextCell(pRole)});
275 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
277 return new Cells(rv, null);
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)
284 public void postfix(AAF_GUI state, AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen) {
285 final Mark mark = trans.get(sMark, 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);