[PORTAL-16 PORTAL-18] Widget ms; staging
[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 @RestController
28 @org.springframework.context.annotation.Configuration
29 @EnableAspectJAutoProxy
30 @EPAuditLog
31 public class BasicAuthAccountController extends EPRestrictedBaseController {
32
33         @Autowired
34         private BasicAuthAccountService basicAuthAccountService;
35
36         @Autowired
37         private AdminRolesService adminRolesService;
38
39         /**
40          * Saves Basic Authentication account for external systems
41          * 
42          * @param request
43          *            HttpServletRequest
44          * @param response
45          *            HttpServletResponse
46          * @param newBasicAuthAccount
47          *            BasicAuthCredentials
48          * @return Id of the newly created account
49          * @throws Exception
50          *             on failure
51          */
52         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
53         public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
54                         @RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {
55
56                 EPUser user = EPUserUtils.getUserSession(request);
57                 if (!adminRolesService.isSuperAdmin(user)) {
58                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
59                                         "Admin Only Operation! ");
60                 }
61
62                 if (newBasicAuthAccount == null) {
63                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
64                                         "newBasicAuthAccount cannot be null or empty");
65                 }
66                 long accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);
67
68                 List<Long> endpointIdList = new ArrayList<>();
69                 try {
70                         for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
71                                 endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
72                         }
73                         for (Long endpointId : endpointIdList) {
74                                 basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
75                         }
76                 } catch (Exception e) {
77                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
78                 }
79
80                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
81         }
82
83         /**
84          * Returns list of all BasicAuthCredentials in the system
85          * 
86          * @param request
87          *            HttpServletRequest
88          * @param response
89          *            HttpServletResponse
90          * @return List<BasicAuthCredentials>
91          * @throws Exception
92          *             on failure
93          */
94
95         @RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
96         public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
97                         HttpServletResponse response) throws Exception {
98
99                 EPUser user = EPUserUtils.getUserSession(request);
100                 if (!adminRolesService.isSuperAdmin(user)) {
101                         return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.ERROR,
102                                         "UnAuthorized! Admin Only Operation", new ArrayList<>());
103                 }
104
105                 return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.OK, "Success",
106                                 basicAuthAccountService.getAccountData());
107         }
108
109         /**
110          * Updates an existing BasicAuthCredentials account
111          * 
112          * @param request
113          *            HttpServletRequest
114          * @param response
115          *            HttpServletResponse
116          * @param accountId
117          *            account ID
118          * @param newBasicAuthAccount
119          *            BasicAuthCredentials
120          * @return PortalRestResponse<String>
121          * @throws Exception
122          *             on failure
123          */
124         @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.PUT)
125         public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
126                         @PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount)
127                         throws Exception {
128
129                 EPUser user = EPUserUtils.getUserSession(request);
130                 if (!adminRolesService.isSuperAdmin(user)) {
131                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
132                                         "Admin Only Operation! ");
133                 }
134
135                 if (newBasicAuthAccount == null) {
136                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
137                                         "BasicAuthCredentials cannot be null or empty");
138                 }
139                 try {
140                         basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
141                 } catch (Exception e) {
142                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
143                 }
144                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
145         }
146
147         /**
148          * deletes an existing BasicAuthCredentials account
149          * 
150          * @param request
151          *            HttpServletRequest
152          * @param response
153          *            HttpServletResponse
154          * @param accountId
155          *            account ID
156          * @return PortalRestResponse<String>
157          * @throws Exception
158          *             on failure
159          */
160         @RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.DELETE)
161         public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
162                         @PathVariable("accountId") long accountId) throws Exception {
163
164                 EPUser user = EPUserUtils.getUserSession(request);
165                 if (!adminRolesService.isSuperAdmin(user)) {
166                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
167                                         "Admin Only Operation! ");
168                 }
169
170                 try {
171                         basicAuthAccountService.deleteEndpointAccout(accountId);
172                 } catch (Exception e) {
173                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
174                 }
175                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
176         }
177
178 }