c9abc3b0b4755d287e23a0415092d3dfba5bbfbd
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / RolesApprovalSystemController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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.openecomp.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.openecomp.portalapp.externalsystemapproval.model.ExternalSystemRoleApproval;
47 import org.openecomp.portalapp.externalsystemapproval.model.ExternalSystemUser;
48 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
49 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
50 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
51 import org.openecomp.portalapp.portal.service.UserRolesService;
52 import org.openecomp.portalapp.portal.transport.ExternalRequestFieldsValidator;
53 import org.openecomp.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("405")) {
108                                 response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
109                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(),
110                                                 "save user profile failed");
111                         } else {
112                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
113                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(),
114                                                 "save user profile failed");
115                         }
116                 }
117                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, reqResult.getDetailMessage(), "Success");
118         }
119
120         /**
121          * Updates an application user to have only the specified roles.
122          * 
123          * @param request
124          * @param extSysUser
125          * @return PortalRestResponse with appropriate status value and message
126          */
127         @ApiOperation(value = "Updates an application user to have only the specified roles.", response = PortalRestResponse.class)
128         @RequestMapping(value = { "/userProfile" }, method = RequestMethod.PUT, produces = "application/json")
129         public PortalRestResponse<String> putUserProfile(HttpServletRequest request,
130                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
131                 ExternalRequestFieldsValidator reqResult = null;
132                 try {
133                         logger.info(EELFLoggerDelegate.debugLogger, "putUserProfile: request received for app {}, user {}", 
134                                         extSysUser.getApplicationName(), extSysUser.getLoginId());
135                         validateExtSystemUser(extSysUser, true);
136                         reqResult = userRolesService.setExternalRequestUserAppRole(extSysUser, "PUT");
137                          if (!reqResult.isResult())
138                                         throw new Exception(reqResult.getDetailMessage());
139                 } catch (Exception e) {
140                         logger.error(EELFLoggerDelegate.errorLogger, "putUserProfile: failed for app {}, user {}",
141                                         extSysUser.getApplicationName(), extSysUser.getLoginId(), e);
142                         if(reqResult == null || (!reqResult.isResult()  && !e.getMessage().contains("404") && !e.getMessage().contains("405"))){
143                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
144                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
145                                                 e.getMessage(), "save user profile failed"); 
146                     } else if(e.getMessage().contains("404")){
147                                 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
148                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
149                                                 e.getMessage(), "save user profile failed");
150                         } else if (e.getMessage().contains("405")) {
151                                 response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
152                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "save user profile failed");
153                         } else{
154                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
155                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
156                                                 e.getMessage(), "save user profile failed");
157                         }
158                 }
159                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, reqResult.getDetailMessage() , "Success");
160         }
161
162         /**
163          * Deletes an application user by removing all assigned roles.
164          * 
165          * @param request
166          * @param extSysUser
167          *            This object must have zero roles.
168          * @return PortalRestResponse with appropriate status value and message
169          */
170         @ApiOperation(value = "Processes a request to delete one or more application roles for one      specified user who has roles.", response = PortalRestResponse.class)
171         @RequestMapping(value = { "/userProfile" }, method = RequestMethod.DELETE, produces = "application/json")
172         public PortalRestResponse<String> deleteUserProfile(HttpServletRequest request,
173                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
174                 ExternalRequestFieldsValidator reqResult  = null;
175                 try {
176                         logger.info(EELFLoggerDelegate.debugLogger, "deleteUserProfile: request received for app {}, user {}", 
177                                         extSysUser.getApplicationName(), extSysUser.getLoginId());
178                         validateExtSystemUser(extSysUser, false);
179                         // Ignore any roles that might be mistakenly present in the request
180                         extSysUser.setRoles(new ArrayList<ExternalSystemRoleApproval>());
181                         reqResult = userRolesService.setExternalRequestUserAppRole(extSysUser, "DELETE");
182                          if (!reqResult.isResult())
183                                         throw new Exception(reqResult.getDetailMessage());
184                 } catch (Exception e) {
185                         logger.error(EELFLoggerDelegate.errorLogger, "deleteUserProfile: failed for app {}, user {}",
186                                         extSysUser.getApplicationName(), extSysUser.getLoginId(), e);
187                         if(reqResult == null || (!reqResult.isResult()  && !e.getMessage().contains("404"))){
188                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
189                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
190                                                 e.getMessage(), "delete user profile failed"); 
191                     }else if(e.getMessage().contains("404")){
192                                 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
193                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
194                                                 e.getMessage(), "delete user profile failed");
195                         } else{
196                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
197                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
198                                                 e.getMessage(), "delete user profile failed");
199                         }
200                 }
201                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Deleted Successfully", "Success");
202         }
203
204         /**
205          * Checks for presence of required fields.
206          * 
207          * @param extSysUser
208          * @param rolesRequired
209          *            If true, checks whether roles are present
210          * @throws Exception
211          *             If any field is missing.
212          */
213         private void validateExtSystemUser(ExternalSystemUser extSysUser, boolean rolesRequired) throws Exception {
214                 if (extSysUser.getLoginId() == null || extSysUser.getLoginId() == "")
215                         throw new Exception("Request has no login ID");
216                 if (extSysUser.getApplicationName() == null || extSysUser.getApplicationName() == "")
217                         throw new Exception("Request has no application name");
218                 if (extSysUser.getMyloginrequestId() == null)
219                         throw new Exception("Request has no request ID");
220                 if (rolesRequired && (extSysUser.getRoles() == null || extSysUser.getRoles().size() == 0))
221                         throw new Exception("Request has no roles");
222         }
223
224 }