1 package org.openecomp.portalapp.portal.controller;
3 import java.util.ArrayList;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
9 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
10 import org.openecomp.portalapp.portal.domain.BasicAuthCredentials;
11 import org.openecomp.portalapp.portal.domain.EPEndpoint;
12 import org.openecomp.portalapp.portal.domain.EPUser;
13 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
14 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
15 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
16 import org.openecomp.portalapp.portal.service.AdminRolesService;
17 import org.openecomp.portalapp.portal.service.BasicAuthAccountService;
18 import org.openecomp.portalapp.util.EPUserUtils;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.context.annotation.EnableAspectJAutoProxy;
21 import org.springframework.web.bind.annotation.PathVariable;
22 import org.springframework.web.bind.annotation.RequestBody;
23 import org.springframework.web.bind.annotation.RequestMapping;
24 import org.springframework.web.bind.annotation.RequestMethod;
25 import org.springframework.web.bind.annotation.RestController;
27 @SuppressWarnings("unchecked")
29 @org.springframework.context.annotation.Configuration
30 @EnableAspectJAutoProxy
32 public class BasicAuthAccountController extends EPRestrictedBaseController{
35 private BasicAuthAccountService basicAuthAccountService;
38 private AdminRolesService adminRolesService;
41 * Saves Basic Authentication account for external systems
42 * @param BasicAuthCredentials
43 * @return Id of the newly created account
46 @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
47 public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
48 @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
50 EPUser user = EPUserUtils.getUserSession(request);
51 if (!adminRolesService.isSuperAdmin(user)){
52 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required", "Admin Only Operation! ");
55 if(newBasicAuthAccount == null){
56 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
57 "newBasicAuthAccount cannot be null or empty");
59 long accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
61 List<Long> endpointIdList = new ArrayList<>();
63 for(EPEndpoint ep: newBasicAuthAccount.getEndpoints()){
64 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
66 for(Long endpointId: endpointIdList){
67 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
69 } catch (Exception e) {
70 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
73 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
77 * Returns list of all BasicAuthCredentials in the system
78 * @return List<BasicAuthCredentials>
81 @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
82 public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request, HttpServletResponse response)
85 EPUser user = EPUserUtils.getUserSession(request);
86 if (!adminRolesService.isSuperAdmin(user)){
87 return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.ERROR, "UnAuthorized! Admin Only Operation", new ArrayList<>());
90 return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.OK, "Success", basicAuthAccountService.getAccountData());
94 * Updates an existing BasicAuthCredentials account
97 @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.PUT)
98 public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
99 @PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
101 EPUser user = EPUserUtils.getUserSession(request);
102 if (!adminRolesService.isSuperAdmin(user)){
103 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required", "Admin Only Operation! ");
106 if (newBasicAuthAccount == null) {
107 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
108 "BasicAuthCredentials cannot be null or empty");
111 basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
112 } catch (Exception e) {
113 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
115 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
119 * deletes an existing BasicAuthCredentials account
122 @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.DELETE)
123 public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
124 @PathVariable("accountId") long accountId) throws Exception {
126 EPUser user = EPUserUtils.getUserSession(request);
127 if (!adminRolesService.isSuperAdmin(user)){
128 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required", "Admin Only Operation! ");
133 basicAuthAccountService.deleteEndpointAccout(accountId);
134 } catch (Exception e) {
135 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
137 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");