2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
20 package org.openecomp.portalapp.externalsystemapproval.controller;
\r
22 import java.util.ArrayList;
\r
24 import javax.servlet.http.HttpServletRequest;
\r
26 import org.openecomp.portalapp.externalsystemapproval.model.ExternalSystemRoleApproval;
\r
27 import org.openecomp.portalapp.externalsystemapproval.model.ExternalSystemUser;
\r
28 import org.openecomp.portalapp.portal.controller.BasicAuthenticationController;
\r
29 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
\r
30 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
\r
31 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
\r
32 import org.openecomp.portalapp.portal.service.UserRolesService;
\r
33 import org.openecomp.portalapp.portal.utils.PortalConstants;
\r
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
35 import org.springframework.beans.factory.annotation.Autowired;
\r
36 import org.springframework.context.annotation.Configuration;
\r
37 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r
38 import org.springframework.web.bind.annotation.RequestBody;
\r
39 import org.springframework.web.bind.annotation.RequestMapping;
\r
40 import org.springframework.web.bind.annotation.RequestMethod;
\r
41 import org.springframework.web.bind.annotation.RestController;
\r
43 import io.swagger.annotations.ApiOperation;
\r
46 @RequestMapping(PortalConstants.REST_AUX_API)
\r
48 @EnableAspectJAutoProxy
\r
50 public class RolesApprovalSystemController implements BasicAuthenticationController {
\r
52 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RolesApprovalSystemController.class);
\r
55 private UserRolesService userRolesService;
\r
58 * Creates an application user with the specified roles.
\r
62 * @return PortalRestResponse with appropriate status value and message
\r
64 @ApiOperation(value = "Creates an application user with the specified roles.", response = PortalRestResponse.class)
\r
65 @RequestMapping(value = { "/userProfile" }, method = RequestMethod.POST, produces = "application/json")
\r
66 public PortalRestResponse<String> postUserProfile(HttpServletRequest request,
\r
67 @RequestBody ExternalSystemUser extSysUser) {
\r
69 validateExtSystemUser(extSysUser, true);
\r
70 String response = userRolesService.setAppWithUserRoleStateForUser(extSysUser);
\r
71 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Saved Successfully", response);
\r
72 } catch (Exception e) {
\r
73 logger.error(EELFLoggerDelegate.errorLogger, "postUserProfile failed", e);
\r
74 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.toString(), null);
\r
79 * Updates an application user to have only the specified roles.
\r
83 * @return PortalRestResponse with appropriate status value and message
\r
85 @ApiOperation(value = "Updates an application user to have only the specified roles.", response = PortalRestResponse.class)
\r
86 @RequestMapping(value = { "/userProfile" }, method = RequestMethod.PUT, produces = "application/json")
\r
87 public PortalRestResponse<String> putUserProfile(HttpServletRequest request,
\r
88 @RequestBody ExternalSystemUser extSysUser) {
\r
90 validateExtSystemUser(extSysUser, true);
\r
91 String response = userRolesService.setAppWithUserRoleStateForUser(extSysUser);
\r
92 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Updated Successfully", response);
\r
93 } catch (Exception e) {
\r
94 logger.error(EELFLoggerDelegate.errorLogger, "putUserProfile failed", e);
\r
95 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.toString(), null);
\r
100 * Deletes an application user by removing all assigned roles.
\r
103 * @param extSysUser
\r
104 * This object must have zero roles.
\r
105 * @return PortalRestResponse with appropriate status value and message
\r
107 @ApiOperation(value = "Processes a request to delete one or more application roles for one specified user who has roles.", response = PortalRestResponse.class)
\r
108 @RequestMapping(value = { "/userProfile" }, method = RequestMethod.DELETE, produces = "application/json")
\r
109 public PortalRestResponse<String> deleteUserProfile(HttpServletRequest request,
\r
110 @RequestBody ExternalSystemUser extSysUser) {
\r
112 validateExtSystemUser(extSysUser, false);
\r
113 // Ignore any roles that might be mistakenly present in the request
\r
114 extSysUser.setRoles(new ArrayList<ExternalSystemRoleApproval>());
\r
115 String result = userRolesService.setAppWithUserRoleStateForUser(extSysUser);
\r
116 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Deleted Successfully", result);
\r
117 } catch (Exception e) {
\r
118 logger.error(EELFLoggerDelegate.errorLogger, "deleteUserProfile failed", e);
\r
119 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.toString(), null);
\r
124 * Checks for presence of required fields.
\r
126 * @param extSysUser
\r
127 * @param rolesRequired
\r
128 * If true, checks whether roles are present
\r
129 * @throws Exception
\r
130 * If any field is missing.
\r
132 private void validateExtSystemUser(ExternalSystemUser extSysUser, boolean rolesRequired) throws Exception {
\r
133 if (extSysUser.getLoginId() == null)
\r
134 throw new Exception("Request has no login ID");
\r
135 if (extSysUser.getApplicationName() == null)
\r
136 throw new Exception("Request has no application name");
\r
137 if (extSysUser.getMyloginrequestId() == null)
\r
138 throw new Exception("Request has no request ID");
\r
139 if (rolesRequired && (extSysUser.getRoles() == null || extSysUser.getRoles().size() == 0))
\r
140 throw new Exception("Request has no roles");
\r