[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / BasicAuthAccountController.java
1 package org.openecomp.portalapp.portal.controller;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
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;
26
27 @SuppressWarnings("unchecked")
28 @RestController
29 @org.springframework.context.annotation.Configuration
30 @EnableAspectJAutoProxy
31 @EPAuditLog
32 public class BasicAuthAccountController extends EPRestrictedBaseController{
33         
34         @Autowired
35         private BasicAuthAccountService basicAuthAccountService;
36
37         @Autowired
38         private AdminRolesService adminRolesService;
39
40         /**
41          * Saves Basic Authentication account for external systems 
42          * @param BasicAuthCredentials
43          * @return Id of the newly created account
44         */
45         
46         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
47         public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
48                         @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
49                 
50                 EPUser user = EPUserUtils.getUserSession(request);
51                 if (!adminRolesService.isSuperAdmin(user)){
52                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required", "Admin Only Operation! ");
53                 }
54                 
55                 if(newBasicAuthAccount == null){
56                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
57                                         "newBasicAuthAccount cannot be null or empty");
58                 }
59                 long accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
60                 
61                 List<Long> endpointIdList = new ArrayList<>();
62                 try {
63                         for(EPEndpoint ep: newBasicAuthAccount.getEndpoints()){
64                                 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
65                         }
66                         for(Long endpointId: endpointIdList){
67                                 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
68                         }
69                 } catch (Exception e) {
70                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
71                 }
72                 
73                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
74         }
75         
76         /**
77          * Returns list of all  BasicAuthCredentials in the system
78          * @return List<BasicAuthCredentials>
79          */
80         
81         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
82         public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request, HttpServletResponse response)
83                         throws Exception {
84                 
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<>());
88                 }
89
90                 return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.OK, "Success", basicAuthAccountService.getAccountData());
91         }
92         
93         /**
94          * Updates an existing BasicAuthCredentials account
95          */
96         
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 {
100                 
101                 EPUser user = EPUserUtils.getUserSession(request);
102                 if (!adminRolesService.isSuperAdmin(user)){
103                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required", "Admin Only Operation! ");
104                 }
105
106                 if (newBasicAuthAccount == null) {
107                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
108                                         "BasicAuthCredentials cannot be null or empty");
109                 }
110                 try {
111                         basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
112                 } catch (Exception e) {
113                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
114                 }
115                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
116         }
117         
118         /**
119          * deletes an existing BasicAuthCredentials account
120          */
121
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 {
125                 
126                 EPUser user = EPUserUtils.getUserSession(request);
127                 if (!adminRolesService.isSuperAdmin(user)){
128                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required", "Admin Only Operation! ");
129                 }
130
131                 
132                 try {
133                         basicAuthAccountService.deleteEndpointAccout(accountId);
134                 } catch (Exception e) {
135                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
136                 }
137                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
138         }
139         
140         
141 }