summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/openecomp/portalapp/portal/controller/BasicAuthAccountController.java
blob: c16fa78feb23f765b1e29fd116db928288d78ba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.openecomp.portalapp.portal.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openecomp.portalapp.controller.EPRestrictedBaseController;
import org.openecomp.portalapp.portal.domain.BasicAuthCredentials;
import org.openecomp.portalapp.portal.domain.EPEndpoint;
import org.openecomp.portalapp.portal.domain.EPUser;
import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
import org.openecomp.portalapp.portal.service.AdminRolesService;
import org.openecomp.portalapp.portal.service.BasicAuthAccountService;
import org.openecomp.portalapp.util.EPUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
@EPAuditLog
public class BasicAuthAccountController extends EPRestrictedBaseController {

	@Autowired
	private BasicAuthAccountService basicAuthAccountService;

	@Autowired
	private AdminRolesService adminRolesService;

	/**
	 * Saves Basic Authentication account for external systems
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param newBasicAuthAccount
	 *            BasicAuthCredentials
	 * @return Id of the newly created account
	 * @throws Exception
	 *             on failure
	 */
	@RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.POST)
	public PortalRestResponse<String> createBasicAuthAccount(HttpServletRequest request, HttpServletResponse response,
			@RequestBody BasicAuthCredentials newBasicAuthAccount) throws Exception {

		EPUser user = EPUserUtils.getUserSession(request);
		if (!adminRolesService.isSuperAdmin(user)) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
					"Admin Only Operation! ");
		}

		if (newBasicAuthAccount == null) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
					"newBasicAuthAccount cannot be null or empty");
		}
		long accountId = basicAuthAccountService.saveBasicAuthAccount(newBasicAuthAccount);

		List<Long> endpointIdList = new ArrayList<>();
		try {
			for (EPEndpoint ep : newBasicAuthAccount.getEndpoints()) {
				endpointIdList.add(basicAuthAccountService.saveEndpoints(ep));
			}
			for (Long endpointId : endpointIdList) {
				basicAuthAccountService.saveEndpointAccount(accountId, endpointId);
			}
		} catch (Exception e) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
		}

		return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
	}

	/**
	 * Returns list of all BasicAuthCredentials in the system
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @return List<BasicAuthCredentials>
	 * @throws Exception
	 *             on failure
	 */

	@RequestMapping(value = { "/portalApi/basicAuthAccount" }, method = RequestMethod.GET)
	public PortalRestResponse<List<BasicAuthCredentials>> getBasicAuthAccount(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		EPUser user = EPUserUtils.getUserSession(request);
		if (!adminRolesService.isSuperAdmin(user)) {
			return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.ERROR,
					"UnAuthorized! Admin Only Operation", new ArrayList<>());
		}

		return new PortalRestResponse<List<BasicAuthCredentials>>(PortalRestStatusEnum.OK, "Success",
				basicAuthAccountService.getAccountData());
	}

	/**
	 * Updates an existing BasicAuthCredentials account
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param accountId
	 *            account ID
	 * @param newBasicAuthAccount
	 *            BasicAuthCredentials
	 * @return PortalRestResponse<String>
	 * @throws Exception
	 *             on failure
	 */
	@RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.PUT)
	public PortalRestResponse<String> updateAccount(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("accountId") long accountId, @RequestBody BasicAuthCredentials newBasicAuthAccount)
			throws Exception {

		EPUser user = EPUserUtils.getUserSession(request);
		if (!adminRolesService.isSuperAdmin(user)) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
					"Admin Only Operation! ");
		}

		if (newBasicAuthAccount == null) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
					"BasicAuthCredentials cannot be null or empty");
		}
		try {
			basicAuthAccountService.updateBasicAuthAccount(accountId, newBasicAuthAccount);
		} catch (Exception e) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
		}
		return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
	}

	/**
	 * deletes an existing BasicAuthCredentials account
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param accountId
	 *            account ID
	 * @return PortalRestResponse<String>
	 * @throws Exception
	 *             on failure
	 */
	@RequestMapping(value = { "/portalApi/basicAuthAccount/{accountId}" }, method = RequestMethod.DELETE)
	public PortalRestResponse<String> deleteAccount(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("accountId") long accountId) throws Exception {

		EPUser user = EPUserUtils.getUserSession(request);
		if (!adminRolesService.isSuperAdmin(user)) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Authorization Required",
					"Admin Only Operation! ");
		}

		try {
			basicAuthAccountService.deleteEndpointAccout(accountId);
		} catch (Exception e) {
			return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
		}
		return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
	}

}