[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / externalsystemapproval / controller / RolesApprovalSystemController.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\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
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \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
19  */\r
20 package org.openecomp.portalapp.externalsystemapproval.controller;\r
21 \r
22 import java.util.ArrayList;\r
23 \r
24 import javax.servlet.http.HttpServletRequest;\r
25 \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
42 \r
43 import io.swagger.annotations.ApiOperation;\r
44 \r
45 @RestController\r
46 @RequestMapping(PortalConstants.REST_AUX_API)\r
47 @Configuration\r
48 @EnableAspectJAutoProxy\r
49 @EPAuditLog\r
50 public class RolesApprovalSystemController implements BasicAuthenticationController {\r
51 \r
52         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RolesApprovalSystemController.class);\r
53 \r
54         @Autowired\r
55         private UserRolesService userRolesService;\r
56 \r
57         /**\r
58          * Creates an application user with the specified roles.\r
59          * \r
60          * @param request\r
61          * @param extSysUser\r
62          * @return PortalRestResponse with appropriate status value and message\r
63          */\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
68                 try {\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
75                 }\r
76         }\r
77 \r
78         /**\r
79          * Updates an application user to have only the specified roles.\r
80          * \r
81          * @param request\r
82          * @param extSysUser\r
83          * @return PortalRestResponse with appropriate status value and message\r
84          */\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
89                 try {\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
96                 }\r
97         }\r
98 \r
99         /**\r
100          * Deletes an application user by removing all assigned roles.\r
101          * \r
102          * @param request\r
103          * @param extSysUser\r
104          *            This object must have zero roles.\r
105          * @return PortalRestResponse with appropriate status value and message\r
106          */\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
111                 try {\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
120                 }\r
121         }\r
122 \r
123         /**\r
124          * Checks for presence of required fields.\r
125          * \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
131          */\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
141         }\r
142 \r
143 }\r