b4babfdfdeccd505ca03becae3fc1b8b857a8200
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / RolesApprovalSystemController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38
39 package org.onap.portalapp.portal.controller;
40
41 import java.util.ArrayList;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.onap.portalapp.externalsystemapproval.model.ExternalSystemRoleApproval;
47 import org.onap.portalapp.externalsystemapproval.model.ExternalSystemUser;
48 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
49 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
50 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
51 import org.onap.portalapp.portal.service.UserRolesService;
52 import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.context.annotation.Configuration;
56 import org.springframework.context.annotation.EnableAspectJAutoProxy;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestMethod;
60 import org.springframework.web.bind.annotation.RestController;
61
62 import io.swagger.annotations.ApiOperation;
63
64 @RestController
65 @RequestMapping("/auxapi")
66 @Configuration
67 @EnableAspectJAutoProxy
68 @EPAuditLog
69 public class RolesApprovalSystemController implements BasicAuthenticationController {
70         
71         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RolesApprovalSystemController.class);
72
73         @Autowired
74         private UserRolesService userRolesService;
75
76         /**
77          * Creates an application user with the specified roles.
78          * 
79          * @param request
80          * @param extSysUser
81          * @return PortalRestResponse with appropriate status value and message
82          */
83         @ApiOperation(value = "Creates an application user with the specified roles.", response = PortalRestResponse.class)
84         @RequestMapping(value = { "/userProfile" }, method = RequestMethod.POST, produces = "application/json")
85         public PortalRestResponse<String> postUserProfile(HttpServletRequest request,
86                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
87                 ExternalRequestFieldsValidator reqResult = null;
88                 try {
89                         logger.info(EELFLoggerDelegate.debugLogger, "postUserProfile: request received for app {}, user {}",
90                                         extSysUser.getApplicationName(), extSysUser.getLoginId());
91                         
92                         validateExtSystemUser(extSysUser, true);
93                  reqResult = userRolesService.setExternalRequestUserAppRole(extSysUser, "POST");
94                  if (!reqResult.isResult())
95                                 throw new Exception(reqResult.getDetailMessage());
96                 } catch (Exception e) {
97                         logger.error(EELFLoggerDelegate.errorLogger, "postUserProfile: failed for app {}, user {}",
98                                         extSysUser.getApplicationName(), extSysUser.getLoginId(), e);
99                         if(reqResult == null || (!reqResult.isResult()  && !e.getMessage().contains("404") && !e.getMessage().contains("405"))){
100                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
101                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
102                                                 e.getMessage(), "save user profile failed"); 
103                     } else if(e.getMessage().contains("404")){
104                                 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
105                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
106                                                 e.getMessage(), "save user profile failed");
107                     } else if(e.getMessage().contains("500")){
108                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
109                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
110                                                 e.getMessage(), "save user profile failed");
111                         } else if (e.getMessage().contains("405")) {
112                                 response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
113                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(),
114                                                 "save user profile failed");
115                         } else {
116                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
117                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(),
118                                                 "save user profile failed");
119                         }
120                 }
121                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, reqResult.getDetailMessage(), "Success");
122         }
123
124         /**
125          * Updates an application user to have only the specified roles.
126          * 
127          * @param request
128          * @param extSysUser
129          * @return PortalRestResponse with appropriate status value and message
130          */
131         @ApiOperation(value = "Updates an application user to have only the specified roles.", response = PortalRestResponse.class)
132         @RequestMapping(value = { "/userProfile" }, method = RequestMethod.PUT, produces = "application/json")
133         public PortalRestResponse<String> putUserProfile(HttpServletRequest request,
134                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
135                 ExternalRequestFieldsValidator reqResult = null;
136                 try {
137                         logger.info(EELFLoggerDelegate.debugLogger, "putUserProfile: request received for app {}, user {}", 
138                                         extSysUser.getApplicationName(), extSysUser.getLoginId());
139                         validateExtSystemUser(extSysUser, true);
140                         reqResult = userRolesService.setExternalRequestUserAppRole(extSysUser, "PUT");
141                          if (!reqResult.isResult())
142                                         throw new Exception(reqResult.getDetailMessage());
143                 } catch (Exception e) {
144                         logger.error(EELFLoggerDelegate.errorLogger, "putUserProfile: failed for app {}, user {}",
145                                         extSysUser.getApplicationName(), extSysUser.getLoginId(), e);
146                         if(reqResult == null || (!reqResult.isResult()  && !e.getMessage().contains("404") && !e.getMessage().contains("405"))){
147                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
148                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
149                                                 e.getMessage(), "save user profile failed"); 
150                     } else if(e.getMessage().contains("404")){
151                                 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
152                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
153                                                 e.getMessage(), "save user profile failed");
154                         } else if(e.getMessage().contains("500")){
155                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
156                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
157                                                 e.getMessage(), "save user profile failed");
158                         } else if (e.getMessage().contains("405")) {
159                                 response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
160                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "save user profile failed");
161                         } else{
162                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
163                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
164                                                 e.getMessage(), "save user profile failed");
165                         }
166                 }
167                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, reqResult.getDetailMessage() , "Success");
168         }
169
170         /**
171          * Deletes an application user by removing all assigned roles.
172          * 
173          * @param request
174          * @param extSysUser
175          *            This object must have zero roles.
176          * @return PortalRestResponse with appropriate status value and message
177          */
178         @ApiOperation(value = "Processes a request to delete one or more application roles for one      specified user who has roles.", response = PortalRestResponse.class)
179         @RequestMapping(value = { "/userProfile" }, method = RequestMethod.DELETE, produces = "application/json")
180         public PortalRestResponse<String> deleteUserProfile(HttpServletRequest request,
181                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
182                 ExternalRequestFieldsValidator reqResult  = null;
183                 try {
184                         logger.info(EELFLoggerDelegate.debugLogger, "deleteUserProfile: request received for app {}, user {}", 
185                                         extSysUser.getApplicationName(), extSysUser.getLoginId());
186                         validateExtSystemUser(extSysUser, false);
187                         // Ignore any roles that might be mistakenly present in the request
188                         extSysUser.setRoles(new ArrayList<ExternalSystemRoleApproval>());
189                         reqResult = userRolesService.setExternalRequestUserAppRole(extSysUser, "DELETE");
190                          if (!reqResult.isResult())
191                                         throw new Exception(reqResult.getDetailMessage());
192                 } catch (Exception e) {
193                         logger.error(EELFLoggerDelegate.errorLogger, "deleteUserProfile: failed for app {}, user {}",
194                                         extSysUser.getApplicationName(), extSysUser.getLoginId(), e);
195                         if(reqResult == null || (!reqResult.isResult()  && !e.getMessage().contains("404"))){
196                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
197                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
198                                                 e.getMessage(), "delete user profile failed"); 
199                     }else if(e.getMessage().contains("404")){
200                                 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
201                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
202                                                 e.getMessage(), "delete user profile failed");
203                         } else{
204                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
205                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
206                                                 e.getMessage(), "delete user profile failed");
207                         }
208                 }
209                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Deleted Successfully", "Success");
210         }
211
212         /**
213          * Checks for presence of required fields.
214          * 
215          * @param extSysUser
216          * @param rolesRequired
217          *            If true, checks whether roles are present
218          * @throws Exception
219          *             If any field is missing.
220          */
221         private void validateExtSystemUser(ExternalSystemUser extSysUser, boolean rolesRequired) throws Exception {
222                 if (extSysUser.getLoginId() == null || extSysUser.getLoginId() == "")
223                         throw new Exception("Request has no login ID");
224                 if (extSysUser.getApplicationName() == null || extSysUser.getApplicationName() == "")
225                         throw new Exception("Request has no application name");
226                 if (extSysUser.getMyloginrequestId() == null)
227                         throw new Exception("Request has no request ID");
228                 if (rolesRequired && (extSysUser.getRoles() == null || extSysUser.getRoles().size() == 0))
229                         throw new Exception("Request has no roles");
230         }
231
232 }