Sonar fix in ecomp-portal-BE-common
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / BasicAuthAccountController.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  *
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.onap.portalapp.controller.EPRestrictedBaseController;
47 import org.onap.portalapp.portal.domain.BasicAuthCredentials;
48 import org.onap.portalapp.portal.domain.EPEndpoint;
49 import org.onap.portalapp.portal.domain.EPUser;
50 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
51 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
52 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
53 import org.onap.portalapp.portal.service.AdminRolesService;
54 import org.onap.portalapp.portal.service.BasicAuthAccountService;
55 import org.onap.portalapp.util.EPUserUtils;
56 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.context.annotation.EnableAspectJAutoProxy;
59 import org.springframework.web.bind.annotation.PathVariable;
60 import org.springframework.web.bind.annotation.RequestBody;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.RequestMethod;
63 import org.springframework.web.bind.annotation.RestController;
64
65 @RestController
66 @org.springframework.context.annotation.Configuration
67 @EnableAspectJAutoProxy
68 @EPAuditLog
69 public class BasicAuthAccountController extends EPRestrictedBaseController {
70
71     private static final String FAILURE = "FAILURE";
72     private static final String SUCCESS = "SUCCESS";
73     private static final String AUTHORIZATION_REQUIRED = "Authorization Required";
74     private static final String ADMIN_ONLY_OPERATIONS = "Admin Only Operation! ";
75
76     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BasicAuthAccountController.class);
77
78         @Autowired
79         private BasicAuthAccountService basicAuthAccountService;
80
81         @Autowired
82         private AdminRolesService adminRolesService;
83
84         /**
85          * Saves Basic Authentication account for external systems
86          *
87          * @param request
88          *            HttpServletRequest
89          * @param response
90          *            HttpServletResponse
91          * @param newBasicAuthAccount
92          *            BasicAuthCredentials
93          * @return Id of the newly created account
94          * @throws Exception
95          *             on failure
96          */
97         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
98         public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
99                         @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
100
101                 EPUser user = EPUserUtils.getUserSession(request);
102                 if (!adminRolesService.isSuperAdmin(user)) {
103             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
104                     ADMIN_ONLY_OPERATIONS);
105                 }
106
107                 if (newBasicAuthAccount == null) {
108             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
109                                         "newBasicAuthAccount cannot be null or empty");
110                 }
111                 long accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
112
113                 List<Long> endpointIdList = new ArrayList<>();
114                 try {
115                         for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
116                                 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
117                         }
118                         for (Long endpointId : endpointIdList) {
119                                 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
120                         }
121                 } catch (Exception e) {
122             logger.error(EELFLoggerDelegate.errorLogger, "createBasicAuthAccount failed", e);
123             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
124                 }
125
126         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
127         }
128
129         /**
130          * Returns list of all BasicAuthCredentials in the system
131          *
132          * @param request
133          *            HttpServletRequest
134          * @param response
135          *            HttpServletResponse
136          * @return List<BasicAuthCredentials>
137          * @throws Exception
138          *             on failure
139          */
140
141         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
142         public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
143                         HttpServletResponse response) throws Exception {
144
145                 EPUser user = EPUserUtils.getUserSession(request);
146                 if (!adminRolesService.isSuperAdmin(user)) {
147             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
148                                         "UnAuthorized! Admin Only Operation", new ArrayList<>());
149                 }
150
151         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "Success",
152                                 basicAuthAccountService.getAccountData());
153         }
154
155         /**
156          * Updates an existing BasicAuthCredentials account
157          *
158          * @param request
159          *            HttpServletRequest
160          * @param response
161          *            HttpServletResponse
162          * @param accountId
163          *            account ID
164          * @param newBasicAuthAccount
165          *            BasicAuthCredentials
166          * @return PortalRestResponse<String>
167          * @throws Exception
168          *             on failure
169          */
170         @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.PUT)
171         public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
172                         @PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount)
173                         throws Exception {
174
175                 EPUser user = EPUserUtils.getUserSession(request);
176                 if (!adminRolesService.isSuperAdmin(user)) {
177             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
178                     ADMIN_ONLY_OPERATIONS);
179                 }
180
181                 if (newBasicAuthAccount == null) {
182             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
183                                         "BasicAuthCredentials cannot be null or empty");
184                 }
185                 try {
186                         basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
187                 } catch (Exception e) {
188             logger.error(EELFLoggerDelegate.errorLogger, "updateAccount failed", e);
189             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
190                 }
191         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
192         }
193
194         /**
195          * deletes an existing BasicAuthCredentials account
196          *
197          * @param request
198          *            HttpServletRequest
199          * @param response
200          *            HttpServletResponse
201          * @param accountId
202          *            account ID
203          * @return PortalRestResponse<String>
204          * @throws Exception
205          *             on failure
206          */
207         @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.DELETE)
208         public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
209                         @PathVariable("accountId") long accountId) throws Exception {
210
211                 EPUser user = EPUserUtils.getUserSession(request);
212                 if (!adminRolesService.isSuperAdmin(user)) {
213             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, AUTHORIZATION_REQUIRED,
214                     ADMIN_ONLY_OPERATIONS);
215                 }
216
217                 try {
218                         basicAuthAccountService.deleteEndpointAccout(accountId);
219                 } catch (Exception e) {
220             logger.error(EELFLoggerDelegate.errorLogger, "deleteAccount failed", e);
221             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
222                 }
223         return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, "");
224         }
225
226 }