ed4c19d952016cab914fdc280103e33db2147871
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / UserRoleExtend.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
27 import org.onap.aaf.auth.env.AuthzTrans;
28 import org.onap.aaf.auth.gui.AAF_GUI;
29 import org.onap.aaf.auth.gui.BreadCrumbs;
30 import org.onap.aaf.auth.gui.NamedCode;
31 import org.onap.aaf.auth.gui.Page;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.client.Future;
34 import org.onap.aaf.cadi.client.Rcli;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.misc.env.APIException;
37 import org.onap.aaf.misc.env.Env;
38 import org.onap.aaf.misc.env.Slot;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.xgen.Cache;
41 import org.onap.aaf.misc.xgen.DynamicCode;
42 import org.onap.aaf.misc.xgen.html.HTMLGen;
43
44 public class UserRoleExtend extends Page {
45     public static final String HREF = "/gui/urExtend";
46     static final String NAME = "Extend User Role";
47     static final String fields[] = {"user","role"};
48
49     public UserRoleExtend(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
50         super(gui.env,NAME, HREF, fields,
51                 new BreadCrumbs(breadcrumbs),
52                 new NamedCode(true, "content") {
53             @Override
54             public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
55                 final Slot sUser = gui.env.slot(NAME+".user");
56                 final Slot sRole = gui.env.slot(NAME+".role");
57                 
58                 
59                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
60                     @Override
61                     public void code(final AAF_GUI gui, final AuthzTrans trans,    final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {                        
62                         final String user = trans.get(sUser, "");
63                         final String role = trans.get(sRole, "");
64
65                         TimeTaken tt = trans.start("Request to extend user role",Env.REMOTE);
66                         try {
67                             gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
68                                 @Override
69                                 public Void code(Rcli<?> client)throws CadiException, ConnectException, APIException {
70                                     Future<Void> fv = client.update("/authz/userRole/extend/"+user+"/"+role+"?request=true");
71                                     if (fv.get(5000)) {
72                                         // not sure if we'll ever hit this
73                                         hgen.p("Extended User ["+ user+"] in Role [" +role+"]");
74                                     } else {
75                                         if (fv.code() == 202 ) {
76                                             hgen.p("User ["+ user+"] in Role [" +role+"] Extension sent for Approval");
77                                         } else {
78                                             gui.writeError(trans, fv, hgen,0);
79                                         }
80                                     }
81                                     return null;
82                                 }
83                             });
84                         } catch (Exception e) {
85                             trans.error().log(e);
86                             e.printStackTrace();
87                         } finally {
88                             tt.done();
89                         }
90                         
91                         
92                     }
93                 });
94             }
95             
96         });
97     }
98 }
99