Commit 1378daa4 authored by Noel Alonso's avatar Noel Alonso
Browse files

Corrige problemas con conversión de tipos

parent 3a51d851
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ public class SupersetEmbeddedController {
	}

	@RequestMapping(value = "/get-token/{dashboardid}", method = RequestMethod.GET)
	public String getToken(@PathVariable("dashboardid") String dashboardid) {
	public Object getToken(@PathVariable("dashboardid") String dashboardid) {

		return service.getToken(dashboardid);
	}
+38 −37
Original line number Diff line number Diff line
package es.redmic.user.embedded.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
@@ -13,6 +12,10 @@ import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import es.redmic.exception.security.NotAllowedException;
import es.redmic.user.manager.model.User;
import es.redmic.user.manager.service.UserProfileService;

@@ -60,6 +63,8 @@ public class SupersetEmbeddedService {
	@Autowired
	UserProfileService userProfileService;

	private final ObjectMapper objectMapper = new ObjectMapper();

	List<MediaType> acceptableMediaTypes = new ArrayList<>();

	HttpHeaders headers = new HttpHeaders();
@@ -72,7 +77,7 @@ public class SupersetEmbeddedService {
		headers.setAccept(acceptableMediaTypes);
	}

	public String getToken(String dashboardid) {
	public Object getToken(String dashboardid) {

		String username = userProfileService.getUsername();
		User profile = userProfileService.findProfileByUsername(username);
@@ -81,7 +86,7 @@ public class SupersetEmbeddedService {

		//TODO: Cuando se realice la integración Superset + ECOMARCAN + OpenId, comprobar acceso del usuario al dashboard específico,
		// no de forma genérica como está ahora.
		try {

		if (roleId <= 2) {
			// Se trata de un usuario con permisos, por lo que se loguea contra superset con usuario embbeded
			return fetchGuestToken(supersetPrivateDashboardUsername, supersetPrivateDashboardPassword, dashboardid);
@@ -89,57 +94,53 @@ public class SupersetEmbeddedService {
			// Se trata de un usuario sin permisos, por lo que se loguea contra superset con usuario guest
			return fetchGuestToken(supersetPublicDashboardUsername, supersetPublicDashboardPassword, dashboardid);
		}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		throw new NotAllowedException();
	}

		return null;
	}
	private Object fetchGuestToken(String user, String password, String dashboardid) {

	private String fetchGuestToken(String user, String password, String dashboardid) throws JSONException {
		String url = supersetApiUrl + supersetApiBasePath + "guest_token/";
		String accessToken = login(user, password);

		String accessToken;

		try {
			accessToken = login(user, password);
		} catch (IOException e) {
			throw new NotAllowedException();
		}


		RestTemplate restTemplate = new RestTemplate();

		JSONObject body = new JSONObject(
			"{'resources': [{id': " + dashboardid + ", 'type': 'dashboard'}], 'rls': [], 'user': {'username': " + user + "}}");
		String body = "{'resources': [{id': " + dashboardid + ", 'type': 'dashboard'}], 'rls': [], 'user': {'username': " + user + "}}";

		HttpHeaders authHeaders = headers;
		authHeaders.set(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
		HttpEntity<JSONObject> request = new HttpEntity<>(body, authHeaders);

		JSONObject response = restTemplate.postForObject(url, request, JSONObject.class);
		HttpEntity<String> request = new HttpEntity<>(body, authHeaders);

		if (response != null) {
			Object token = response.get("token");
			return (token != null) ? token.toString() : null;
		} else {
			return null;
		}
		return restTemplate.postForObject(url, request, String.class);
	}

	private String login(String user, String password) throws JSONException {
	private String login(String user, String password) throws IOException {

		String url = supersetApiUrl + supersetApiBasePath + "login";

		RestTemplate restTemplate = new RestTemplate();

		JSONObject body = new JSONObject(
			"{'username': " + user
		String body = "{'username': " + user
			+ ", 'password': " + password
			+ ", 'provider': 'db', 'refresh': 'true'}");
			+ ", 'provider': 'db', 'refresh': 'true'}";

		HttpEntity<JSONObject> request = new HttpEntity<>(body, headers);
		HttpEntity<String> request = new HttpEntity<>(body, headers);

		JSONObject response = restTemplate.postForObject(url, request, JSONObject.class);
		String response = restTemplate.postForObject(url, request, String.class);

		if (response != null) {
			Object accessToken = response.get("access_token");
			JsonNode root = objectMapper.readTree(response);
			String accessToken = root.path("access_token").asText();
			return (accessToken != null) ? accessToken.toString() : null;
		} else {
			return null;
			throw new NotAllowedException();
		}
	}
}