Update Fixes from testing
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / api / API_User.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.service.api;
23
24 import static org.onap.aaf.auth.layer.Result.OK;
25 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
26
27 import java.net.URLDecoder;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.eclipse.jetty.http.HttpStatus;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.auth.service.AAF_Service;
36 import org.onap.aaf.auth.service.Code;
37 import org.onap.aaf.auth.service.facade.AuthzFacade;
38 import org.onap.aaf.auth.service.mapper.Mapper.API;
39 import org.onap.aaf.cadi.config.Config;
40
41 /**
42  * User Role APIs
43  * @author Jonathan
44  *
45  */
46 public class API_User {
47     /**
48      * Normal Init level APIs
49      * 
50      * @param authzAPI
51      * @param facade
52      * @throws Exception
53      */
54     public static void init(final AAF_Service authzAPI, AuthzFacade facade) throws Exception {
55         /**
56          * get all Users who have Permission X
57          */
58         authzAPI.route(GET,"/authz/users/perm/:type/:instance/:action",API.USERS,new Code(facade,"Get Users By Permission", true) {
59             @Override
60             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
61 //                trans.checkpoint(pathParam(req,"type") + " " 
62 //                        + pathParam(req,"instance") + " " 
63 //                        + pathParam(req,"action"));
64 //
65                 Result<Void> r = context.getUsersByPermission(trans, resp,
66                         pathParam(req, ":type"),
67                         URLDecoder.decode(pathParam(req, ":instance"),Config.UTF_8),
68                         pathParam(req, ":action"));
69                 switch(r.status) {
70                     case OK:
71                         resp.setStatus(HttpStatus.OK_200); 
72                         break;
73                     default:
74                         context.error(trans,resp,r);
75                 }
76             }
77         });
78
79
80         /**
81          * get all Users who have Role X
82          */
83         authzAPI.route(GET,"/authz/users/role/:role",API.USERS,new Code(facade,"Get Users By Role", true) {
84             @Override
85             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
86                 Result<Void> r = context.getUsersByRole(trans, resp, pathParam(req, ":role"));
87                 switch(r.status) {
88                     case OK:
89                         resp.setStatus(HttpStatus.OK_200); 
90                         break;
91                     default:
92                         context.error(trans,resp,r);
93                 }
94             }
95         });
96         
97         /**
98          * Get User Role if exists
99          * @deprecated
100          */
101         authzAPI.route(GET,"/authz/userRole/:user/:role",API.USERS,new Code(facade,"Get if User is In Role", true) {
102             @Override
103             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
104                 Result<Void> r = context.getUserInRole(trans, resp, pathParam(req,":user"),pathParam(req,":role"));
105                 switch(r.status) {
106                     case OK:
107                         resp.setStatus(HttpStatus.OK_200); 
108                         break;
109                     default:
110                         context.error(trans,resp,r);
111                 }
112             }
113         });
114
115         /**
116          * Get User Role if exists
117          */
118         authzAPI.route(GET,"/authz/users/:user/:role",API.USERS,new Code(facade,"Get if User is In Role", true) {
119             @Override
120             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
121                 Result<Void> r = context.getUserInRole(trans, resp, pathParam(req,":user"),pathParam(req,":role"));
122                 switch(r.status) {
123                     case OK:
124                         resp.setStatus(HttpStatus.OK_200); 
125                         break;
126                     default:
127                         context.error(trans,resp,r);
128                 }
129             }
130         });
131         
132
133
134     }
135         
136 }