AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / RolesShow.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.text.SimpleDateFormat;
27 import java.util.ArrayList;
28
29 import org.onap.aaf.auth.env.AuthzTrans;
30 import org.onap.aaf.auth.gui.AAF_GUI;
31 import org.onap.aaf.auth.gui.BreadCrumbs;
32 import org.onap.aaf.auth.gui.Page;
33 import org.onap.aaf.auth.gui.Table;
34 import org.onap.aaf.auth.gui.Table.Cells;
35 import org.onap.aaf.auth.gui.table.AbsCell;
36 import org.onap.aaf.auth.gui.table.RefCell;
37 import org.onap.aaf.auth.gui.table.TableData;
38 import org.onap.aaf.auth.gui.table.TextCell;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.client.Future;
41 import org.onap.aaf.cadi.client.Rcli;
42 import org.onap.aaf.cadi.client.Retryable;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.Env;
45 import org.onap.aaf.misc.env.TimeTaken;
46 import org.onap.aaf.misc.env.util.Chrono;
47
48 import aaf.v2_0.UserRole;
49 import aaf.v2_0.UserRoles;
50
51
52 /**
53  * Page content for My Roles
54  * 
55  * @author Jonathan
56  *
57  */
58 public class RolesShow extends Page {
59         public static final String HREF = "/gui/myroles";
60         private static final String DATE_TIME_FORMAT = "yyyy-MM-dd";
61         private static SimpleDateFormat expiresDF;
62         
63         static {
64                 expiresDF = new SimpleDateFormat(DATE_TIME_FORMAT);
65         }
66         
67         public RolesShow(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
68                 super(gui.env, "MyRoles",HREF, NO_FIELDS,
69                         new BreadCrumbs(breadcrumbs), 
70                         new Table<AAF_GUI,AuthzTrans>("Roles",gui.env.newTransNoAvg(),new Model(), "class=std"));
71         }
72
73         /**
74          * Implement the Table Content for Permissions by User
75          * 
76          * @author Jonathan
77          *
78          */
79         private static class Model extends TableData<AAF_GUI,AuthzTrans> {
80                 private static final String[] headers = new String[] {"Role","Expires","Remediation","Actions"};
81
82                 @Override
83                 public String[] headers() {
84                         return headers;
85                 }
86                 
87                 @Override
88                 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
89                         Cells rv = Cells.EMPTY;
90
91                         try {
92                                 rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
93                                         @Override
94                                         public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
95                                                 ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
96                                                 TimeTaken tt = trans.start("AAF Roles by User",Env.REMOTE);
97                                                 try {
98                                                         Future<UserRoles> fur = client.read("/authz/userRoles/user/"+trans.user(),gui.getDF(UserRoles.class));
99                                                         if (fur.get(5000)) {
100                                                                 if(fur.value != null) for (UserRole u : fur.value.getUserRole()) {
101                                                                         if(u.getExpires().compare(Chrono.timeStamp()) < 0) {
102                                                                                 AbsCell[] sa = new AbsCell[] {
103                                                                                                 new TextCell(u.getRole() + "*", "class=expired"),
104                                                                                                 new TextCell(expiresDF.format(u.getExpires().toGregorianCalendar().getTime()),"class=expired"),
105                                                                                                 new RefCell("Extend",
106                                                                                                                 UserRoleExtend.HREF + "?user="+trans.user()+"&role="+u.getRole(),
107                                                                                                                 false,
108                                                                                                                 new String[]{"class=expired"}),
109                                                                                                 new RefCell("Remove",
110                                                                                                         UserRoleRemove.HREF + "?user="+trans.user()+"&role="+u.getRole(),
111                                                                                                         false,
112                                                                                                         new String[]{"class=expired"})
113                                                                                                                 
114                                                                                         };
115                                                                                         rv.add(sa);
116                                                                         } else {
117                                                                                 AbsCell[] sa = new AbsCell[] {
118                                                                                                 new RefCell(u.getRole(),
119                                                                                                                 RoleDetail.HREF+"?role="+u.getRole(),
120                                                                                                                 false),
121                                                                                                 new TextCell(expiresDF.format(u.getExpires().toGregorianCalendar().getTime())),
122                                                                                                 AbsCell.Null,
123                                                                                                 new RefCell("Remove",
124                                                                                                                 UserRoleRemove.HREF + "?user="+trans.user()+"&role="+u.getRole(),
125                                                                                                                 false)
126                                                                                         };
127                                                                                         rv.add(sa);
128                                                                         }
129                                                                 }
130                                                         }
131                                                         
132                                                 } finally {
133                                                         tt.done();
134                                                 }
135                                                 return new Cells(rv,null);
136                                         }
137                                 });
138                         } catch (Exception e) {
139                                 trans.error().log(e);
140                         }
141                         return rv;
142                 }
143         }
144 }