2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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============================================
38 package org.onap.portalapp.portal.controller;
40 import java.util.ArrayList;
41 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
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.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.context.annotation.EnableAspectJAutoProxy;
58 import org.springframework.web.bind.annotation.PathVariable;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RequestMethod;
62 import org.springframework.web.bind.annotation.RestController;
65 @org.springframework.context.annotation.Configuration
66 @EnableAspectJAutoProxy
68 public class BasicAuthAccountController extends EPRestrictedBaseController {
71 private BasicAuthAccountService basicAuthAccountService;
74 private AdminRolesService adminRolesService;
77 * Saves Basic Authentication account for external systems
83 * @param newBasicAuthAccount
84 * BasicAuthCredentials
85 * @return Id of the newly created account
89 @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
90 public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
91 @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
93 EPUser user = EPUserUtils.getUserSession(request);
94 if (!adminRolesService.isSuperAdmin(user)) {
95 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
96 "Admin Only Operation! ");
99 if (newBasicAuthAccount == null) {
100 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
101 "newBasicAuthAccount cannot be null or empty");
103 long accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
105 List<Long> endpointIdList = new ArrayList<>();
107 for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
108 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
110 for (Long endpointId : endpointIdList) {
111 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
113 } catch (Exception e) {
114 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
117 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
121 * Returns list of all BasicAuthCredentials in the system
126 * HttpServletResponse
127 * @return List<BasicAuthCredentials>
132 @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
133 public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
134 HttpServletResponse response) throws Exception {
136 EPUser user = EPUserUtils.getUserSession(request);
137 if (!adminRolesService.isSuperAdmin(user)) {
138 return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.ERROR,
139 "UnAuthorized! Admin Only Operation", new ArrayList<>());
142 return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.OK, "Success",
143 basicAuthAccountService.getAccountData());
147 * Updates an existing BasicAuthCredentials account
152 * HttpServletResponse
155 * @param newBasicAuthAccount
156 * BasicAuthCredentials
157 * @return PortalRestResponse<String>
161 @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.PUT)
162 public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
163 @PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount)
166 EPUser user = EPUserUtils.getUserSession(request);
167 if (!adminRolesService.isSuperAdmin(user)) {
168 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
169 "Admin Only Operation! ");
172 if (newBasicAuthAccount == null) {
173 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
174 "BasicAuthCredentials cannot be null or empty");
177 basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
178 } catch (Exception e) {
179 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
181 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
185 * deletes an existing BasicAuthCredentials account
190 * HttpServletResponse
193 * @return PortalRestResponse<String>
197 @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.DELETE)
198 public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
199 @PathVariable("accountId") long accountId) throws Exception {
201 EPUser user = EPUserUtils.getUserSession(request);
202 if (!adminRolesService.isSuperAdmin(user)) {
203 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
204 "Admin Only Operation! ");
208 basicAuthAccountService.deleteEndpointAccout(accountId);
209 } catch (Exception e) {
210 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
212 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");