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