7dff0815dca6d15365cbf3bcec1e1c49a3eee624
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / PermsShow.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.util.ArrayList;
27
28 import org.onap.aaf.auth.env.AuthzTrans;
29 import org.onap.aaf.auth.gui.AAF_GUI;
30 import org.onap.aaf.auth.gui.BreadCrumbs;
31 import org.onap.aaf.auth.gui.Page;
32 import org.onap.aaf.auth.gui.Table;
33 import org.onap.aaf.auth.gui.Table.Cells;
34 import org.onap.aaf.auth.gui.table.AbsCell;
35 import org.onap.aaf.auth.gui.table.RefCell;
36 import org.onap.aaf.auth.gui.table.TableData;
37 import org.onap.aaf.auth.gui.table.TextCell;
38 import org.onap.aaf.cadi.CadiException;
39 import org.onap.aaf.cadi.client.Future;
40 import org.onap.aaf.cadi.client.Rcli;
41 import org.onap.aaf.cadi.client.Retryable;
42 import org.onap.aaf.misc.env.APIException;
43 import org.onap.aaf.misc.env.Env;
44 import org.onap.aaf.misc.env.TimeTaken;
45
46 import aaf.v2_0.Perm;
47 import aaf.v2_0.Perms;
48
49 /**
50  * Page content for My Permissions
51  * 
52  * @author Jonathan
53  *
54  */
55 public class PermsShow extends Page {
56     public static final String HREF = "/gui/myperms";
57     
58     public PermsShow(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
59         super(gui.env, "MyPerms",HREF, NO_FIELDS,
60             new BreadCrumbs(breadcrumbs), 
61             new Table<AAF_GUI,AuthzTrans>("Permissions",gui.env.newTransNoAvg(),new Model(), "class=std"));
62     }
63
64     /**
65      * Implement the Table Content for Permissions by User
66      * 
67      * @author Jonathan
68      *
69      */
70     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
71         private static final String[] headers = new String[] {"Type","Instance","Action"};
72
73         @Override
74         public String[] headers() {
75             return headers;
76         }
77         
78         @Override
79         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
80             final ArrayList<AbsCell[]> rv = new ArrayList<>();
81             TimeTaken tt = trans.start("AAF Perms by User",Env.REMOTE);
82             try {
83                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
84                     @Override
85                     public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
86                         Future<Perms> fp = client.read("/authz/perms/user/"+trans.user(), gui.getDF(Perms.class));
87                         if (fp.get(5000)) {
88                             TimeTaken ttld = trans.start("Load Data", Env.SUB);
89                             try {
90                                 if (fp.value!=null) {    
91                                     for (Perm p : fp.value.getPerm()) {
92                                         AbsCell[] sa = new AbsCell[] {
93                                             new RefCell(p.getType(),PermDetail.HREF
94                                                     +"?type="+p.getType()
95                                                     +"&amp;instance="+p.getInstance()
96                                                     +"&amp;action="+p.getAction(),
97                                                     false),
98                                             new TextCell(p.getInstance()),
99                                             new TextCell(p.getAction())
100                                         };
101                                         rv.add(sa);
102                                     }
103                                 } else {
104                                     gui.writeError(trans, fp, null,0);
105                                 }
106                             } finally {
107                                 ttld.done();
108                             }
109                         }
110                         return null;
111                     }
112                 });
113             } catch (Exception e) {
114                 trans.error().log(e);
115             } finally {
116                 tt.done();
117             }
118             return new Cells(rv,null);
119         }
120     }
121 }