414c4161c7441329c75482b7bc0fdf4fb3720374
[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     
62     public RolesShow(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
63         super(gui.env, "MyRoles",HREF, NO_FIELDS,
64             new BreadCrumbs(breadcrumbs), 
65             new Table<AAF_GUI,AuthzTrans>("Roles",gui.env.newTransNoAvg(),new Model(), "class=std"));
66     }
67
68     /**
69      * Implement the Table Content for Permissions by User
70      * 
71      * @author Jonathan
72      *
73      */
74     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
75         private static final String[] headers = new String[] {"Role","Expires","Remediation","Actions"};
76         private static final String ROLE = "&role=";
77         private static final String USER = "?user=";
78         private static final String CLASS_EXPIRED = "class=expired";
79
80         @Override
81         public String[] headers() {
82             return headers;
83         }
84         
85         @Override
86         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
87             Cells rv = Cells.EMPTY;
88
89             try {
90                 rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
91                     @Override
92                     public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
93                         ArrayList<AbsCell[]> rv = new ArrayList<>();
94                         TimeTaken tt = trans.start("AAF Roles by User",Env.REMOTE);
95                         try {
96                             Future<UserRoles> fur = client.read("/authz/userRoles/user/"+trans.user(),gui.getDF(UserRoles.class));
97                             if (fur.get(5000) && fur.value != null) for (UserRole u : fur.value.getUserRole()) {
98                                     if (u.getExpires().compare(Chrono.timeStamp()) < 0) {
99                                         AbsCell[] sa = new AbsCell[] {
100                                                 new TextCell(u.getRole() + "*", CLASS_EXPIRED),
101                                                 new TextCell(new SimpleDateFormat(DATE_TIME_FORMAT).format(u.getExpires().toGregorianCalendar().getTime()),CLASS_EXPIRED),
102                                                 new RefCell("Extend",
103                                                         UserRoleExtend.HREF+USER+trans.user()+ROLE+u.getRole(),
104                                                         false,
105                                                         new String[]{CLASS_EXPIRED}),
106                                                 new RefCell("Remove",
107                                                     UserRoleRemove.HREF+USER +trans.user()+ROLE+u.getRole(),
108                                                     false,
109                                                     new String[]{CLASS_EXPIRED})
110                                                         
111                                             };
112                                             rv.add(sa);
113                                     } else {
114                                         AbsCell[] sa = new AbsCell[] {
115                                                 new RefCell(u.getRole(),
116                                                         RoleDetail.HREF+"?role="+u.getRole(),
117                                                         false),
118                                                 new TextCell(new SimpleDateFormat(DATE_TIME_FORMAT).format(u.getExpires().toGregorianCalendar().getTime())),
119                                                 AbsCell.Null,
120                                                 new RefCell("Remove",
121                                                         UserRoleRemove.HREF+USER+trans.user()+ROLE+u.getRole(),
122                                                         false)
123                                             };
124                                             rv.add(sa);
125                                     }
126                             }
127                             
128                         } finally {
129                             tt.done();
130                         }
131                         return new Cells(rv,null);
132                     }
133                 });
134             } catch (Exception e) {
135                 trans.error().log(e);
136             }
137             return rv;
138         }
139     }
140 }