8b264dfed8d74e068293456566d2d1aa39d607f9
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / RolesShow.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7 import java.net.ConnectException;
8 import java.text.SimpleDateFormat;
9 import java.util.ArrayList;
10
11 import com.att.authz.env.AuthzTrans;
12 import com.att.authz.gui.AuthGUI;
13 import com.att.authz.gui.BreadCrumbs;
14 import com.att.authz.gui.Page;
15 import com.att.authz.gui.Table;
16 import com.att.authz.gui.Table.Cells;
17 import com.att.authz.gui.table.AbsCell;
18 import com.att.authz.gui.table.RefCell;
19 import com.att.authz.gui.table.TextCell;
20 import org.onap.aaf.cadi.CadiException;
21 import org.onap.aaf.cadi.client.Future;
22 import org.onap.aaf.cadi.client.Rcli;
23 import org.onap.aaf.cadi.client.Retryable;
24 import org.onap.aaf.inno.env.APIException;
25 import org.onap.aaf.inno.env.Env;
26 import org.onap.aaf.inno.env.TimeTaken;
27 import org.onap.aaf.inno.env.util.Chrono;
28
29 import aaf.v2_0.UserRole;
30 import aaf.v2_0.UserRoles;
31
32
33 /**
34  * Page content for My Roles
35  * 
36  *
37  */
38 public class RolesShow extends Page {
39         public static final String HREF = "/gui/myroles";
40         private static final String DATE_TIME_FORMAT = "yyyy-MM-dd";
41         private static SimpleDateFormat expiresDF;
42         
43         static {
44                 expiresDF = new SimpleDateFormat(DATE_TIME_FORMAT);
45         }
46         
47         public RolesShow(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
48                 super(gui.env, "MyRoles",HREF, NO_FIELDS,
49                         new BreadCrumbs(breadcrumbs), 
50                         new Table<AuthGUI,AuthzTrans>("Roles",gui.env.newTransNoAvg(),new Model(), "class=std"));
51         }
52
53         /**
54          * Implement the Table Content for Permissions by User
55          * 
56          *
57          */
58         private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
59                 private static final String[] headers = new String[] {"Role","Expires","Remediation","Actions"};
60
61                 @Override
62                 public String[] headers() {
63                         return headers;
64                 }
65                 
66                 @Override
67                 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
68                         Cells rv = Cells.EMPTY;
69
70                         try {
71                                 rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
72                                         @Override
73                                         public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
74                                                 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
75                                                 TimeTaken tt = trans.start("AAF Roles by User",Env.REMOTE);
76                                                 try {
77                                                         Future<UserRoles> fur = client.read("/authz/userRoles/user/"+trans.user(),gui.userrolesDF);
78                                                         if (fur.get(5000)) {
79                                                                 if(fur.value != null) for (UserRole u : fur.value.getUserRole()) {
80                                                                         if(u.getExpires().compare(Chrono.timeStamp()) < 0) {
81                                                                                 AbsCell[] sa = new AbsCell[] {
82                                                                                                 new TextCell(u.getRole() + "*", "class=expired"),
83                                                                                                 new TextCell(expiresDF.format(u.getExpires().toGregorianCalendar().getTime()),"class=expired"),
84                                                                                                 new RefCell("Extend",
85                                                                                                                 UserRoleExtend.HREF + "?user="+trans.user()+"&role="+u.getRole(), 
86                                                                                                                 new String[]{"class=expired"}),
87                                                                                                 new RefCell("Remove",
88                                                                                                         UserRoleRemove.HREF + "?user="+trans.user()+"&role="+u.getRole(), 
89                                                                                                         new String[]{"class=expired"})
90                                                                                                                 
91                                                                                         };
92                                                                                         rv.add(sa);
93                                                                         } else {
94                                                                                 AbsCell[] sa = new AbsCell[] {
95                                                                                                 new RefCell(u.getRole(),
96                                                                                                                 RoleDetail.HREF+"?role="+u.getRole()),
97                                                                                                 new TextCell(expiresDF.format(u.getExpires().toGregorianCalendar().getTime())),
98                                                                                                 AbsCell.Null,
99                                                                                                 new RefCell("Remove",
100                                                                                                                 UserRoleRemove.HREF + "?user="+trans.user()+"&role="+u.getRole())
101                                                                                         };
102                                                                                         rv.add(sa);
103                                                                         }
104                                                                 }
105                                                         }
106                                                         
107                                                 } finally {
108                                                         tt.done();
109                                                 }
110                                                 return new Cells(rv,null);
111                                         }
112                                 });
113                         } catch (Exception e) {
114                                 trans.error().log(e);
115                         }
116                         return rv;
117                 }
118         }
119 }