2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 package org.openecomp.portalapp.portal.controller;
41 import java.util.ArrayList;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
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;
62 import io.swagger.annotations.ApiOperation;
65 @RequestMapping("/auxapi")
67 @EnableAspectJAutoProxy
69 public class RolesApprovalSystemController implements BasicAuthenticationController {
71 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RolesApprovalSystemController.class);
74 private UserRolesService userRolesService;
77 * Creates an application user with the specified roles.
81 * @return PortalRestResponse with appropriate status value and message
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;
89 logger.info(EELFLoggerDelegate.debugLogger, "postUserProfile: request received for app {}, user {}",
90 extSysUser.getApplicationName(), extSysUser.getLoginId());
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");
112 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
113 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(),
114 "save user profile failed");
117 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, reqResult.getDetailMessage(), "Success");
121 * Updates an application user to have only the specified roles.
125 * @return PortalRestResponse with appropriate status value and message
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;
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");
154 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
155 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
156 e.getMessage(), "save user profile failed");
159 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, reqResult.getDetailMessage() , "Success");
163 * Deletes an application user by removing all assigned roles.
167 * This object must have zero roles.
168 * @return PortalRestResponse with appropriate status value and message
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;
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");
196 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
197 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR,
198 e.getMessage(), "delete user profile failed");
201 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Deleted Successfully", "Success");
205 * Checks for presence of required fields.
208 * @param rolesRequired
209 * If true, checks whether roles are present
211 * If any field is missing.
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");