Merge changes I8f2a44e8,I1e483f2a,Ie2473ea0,I1abb5025,I796bc731, ...
[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.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;
63
64 @RestController
65 @org.springframework.context.annotation.Configuration
66 @EnableAspectJAutoProxy
67 @EPAuditLog
68 public class BasicAuthAccountController extends EPRestrictedBaseController {
69
70         @Autowired
71         private BasicAuthAccountService basicAuthAccountService;
72
73         @Autowired
74         private AdminRolesService adminRolesService;
75
76         /**
77          * Saves Basic Authentication account for external systems
78          * 
79          * @param request
80          *            HttpServletRequest
81          * @param response
82          *            HttpServletResponse
83          * @param newBasicAuthAccount
84          *            BasicAuthCredentials
85          * @return Id of the newly created account
86          * @throws Exception
87          *             on failure
88          */
89         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
90         public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
91                         @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
92
93                 EPUser user = EPUserUtils.getUserSession(request);
94                 if (!adminRolesService.isSuperAdmin(user)) {
95                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
96                                         "Admin Only Operation! ");
97                 }
98
99                 if (newBasicAuthAccount == null) {
100                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
101                                         "newBasicAuthAccount cannot be null or empty");
102                 }
103                 long accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
104
105                 List<Long> endpointIdList = new ArrayList<>();
106                 try {
107                         for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
108                                 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
109                         }
110                         for (Long endpointId : endpointIdList) {
111                                 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
112                         }
113                 } catch (Exception e) {
114                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
115                 }
116
117                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
118         }
119
120         /**
121          * Returns list of all BasicAuthCredentials in the system
122          * 
123          * @param request
124          *            HttpServletRequest
125          * @param response
126          *            HttpServletResponse
127          * @return List<BasicAuthCredentials>
128          * @throws Exception
129          *             on failure
130          */
131
132         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
133         public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
134                         HttpServletResponse response) throws Exception {
135
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<>());
140                 }
141
142                 return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.OK, "Success",
143                                 basicAuthAccountService.getAccountData());
144         }
145
146         /**
147          * Updates an existing BasicAuthCredentials account
148          * 
149          * @param request
150          *            HttpServletRequest
151          * @param response
152          *            HttpServletResponse
153          * @param accountId
154          *            account ID
155          * @param newBasicAuthAccount
156          *            BasicAuthCredentials
157          * @return PortalRestResponse<String>
158          * @throws Exception
159          *             on failure
160          */
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)
164                         throws Exception {
165
166                 EPUser user = EPUserUtils.getUserSession(request);
167                 if (!adminRolesService.isSuperAdmin(user)) {
168                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
169                                         "Admin Only Operation! ");
170                 }
171
172                 if (newBasicAuthAccount == null) {
173                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
174                                         "BasicAuthCredentials cannot be null or empty");
175                 }
176                 try {
177                         basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
178                 } catch (Exception e) {
179                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
180                 }
181                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
182         }
183
184         /**
185          * deletes an existing BasicAuthCredentials account
186          * 
187          * @param request
188          *            HttpServletRequest
189          * @param response
190          *            HttpServletResponse
191          * @param accountId
192          *            account ID
193          * @return PortalRestResponse<String>
194          * @throws Exception
195          *             on failure
196          */
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 {
200
201                 EPUser user = EPUserUtils.getUserSession(request);
202                 if (!adminRolesService.isSuperAdmin(user)) {
203                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
204                                         "Admin Only Operation! ");
205                 }
206
207                 try {
208                         basicAuthAccountService.deleteEndpointAccout(accountId);
209                 } catch (Exception e) {
210                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
211                 }
212                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
213         }
214
215 }