Skip to content

Commit

Permalink
avniproject#733 | Some refactoring changes,and fixed the permissions …
Browse files Browse the repository at this point in the history
…group error which was caused due to wrong code being pushed in my last commit
  • Loading branch information
ombhardwajj committed Jun 24, 2024
1 parent 98b4545 commit fd0215c
Show file tree
Hide file tree
Showing 22 changed files with 493 additions and 209 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.CollectionPermissions;
import org.avni.server.domain.metabase.CollectionPermissionsGraphResponse;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class CollectionPermissionsRepository extends MetabaseConnector {

public CollectionPermissionsRepository(RestTemplateBuilder restTemplateBuilder) {
super(restTemplateBuilder);
}

public CollectionPermissionsGraphResponse getCollectionPermissionsGraph() {
String url = metabaseApiUrl + "/collection/graph";
return getForObject(url, CollectionPermissionsGraphResponse.class);
}

public void updateCollectionPermissions(CollectionPermissions collectionPermissions, int groupId, int collectionId) {
collectionPermissions.updatePermissionsGraph(groupId, collectionId);
String url = metabaseApiUrl + "/collection/graph";
sendPutRequest(url, convertToMap(collectionPermissions.getPermissionsGraph()));
}

private Map<String, Object> convertToMap(CollectionPermissionsGraphResponse collectionPermissionsGraphResponse) {
Map<String, Object> map = new HashMap<>();
map.put("revision", collectionPermissionsGraphResponse.getRevision());
map.put("groups", collectionPermissionsGraphResponse.getGroups());
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
import org.avni.server.domain.metabase.Collection;
import org.avni.server.domain.metabase.CollectionResponse;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Repository;

public class CollectionRepository extends MetabaseRepository{
@Repository
public class CollectionRepository extends MetabaseConnector {
public CollectionRepository(RestTemplateBuilder restTemplateBuilder) {
super(restTemplateBuilder);
}

public CollectionResponse save(Collection collection) {
String url = metabaseApiUrl + "/collection";
HttpEntity<Collection> entity = createHttpEntity(collection);
CollectionResponse response = restTemplate.postForObject(url, entity, CollectionResponse.class);

return response;
return postForObject(url, collection, CollectionResponse.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import org.avni.server.domain.metabase.Database;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Repository;

@Repository
public class DatabaseRepository extends MetabaseRepository{
public class DatabaseRepository extends MetabaseConnector {
public DatabaseRepository(RestTemplateBuilder restTemplateBuilder) {
super(restTemplateBuilder);
}

public Database save(Database database) {
String url = metabaseApiUrl + "/database";
HttpEntity<Database> entity = createHttpEntity(database);
Database response = restTemplate.postForObject(url, entity, Database.class);

return new Database(response.getId(), database.getName(), database.getEngine(), database.getDetails());
Database response = postForObject(url, database, Database.class);
database.setId(response.getId());
return database;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.PermissionsGroup;
import org.avni.server.domain.metabase.PermissionsGroupBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.Map;

@Repository
public class MetabaseConnector {
protected final RestTemplate restTemplate;

@Value("${metabase.api.url}")
protected String metabaseApiUrl;

@Value("${metabase.api.key}")
private String apiKey;

public MetabaseConnector(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}

protected HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);
return headers;
}

protected <T> HttpEntity<T> createHttpEntity(T body) {
HttpHeaders headers = getHeaders();
return new HttpEntity<>(body, headers);
}

protected void sendPutRequest(String url, Map<String, ?> requestBody) {
HttpEntity<Map<String, ?>> entity = createHttpEntity(requestBody);
restTemplate.exchange(url, HttpMethod.PUT, entity, Map.class);
}

protected <T> T postForObject(String url, Object request, Class<T> responseType) {
HttpEntity<Object> entity = createHttpEntity(request);
return restTemplate.postForObject(url, entity, responseType);
}

protected <T> T getForObject(String url, Class<T> responseType) {
HttpHeaders headers = getHeaders();
HttpEntity<Void> entity = new HttpEntity<>(headers);
return restTemplate.exchange(url, HttpMethod.GET, entity, responseType).getBody();
}

protected HttpEntity<Map<String, Object>> createJsonEntity(PermissionsGroupBody body) {
HttpHeaders headers = getHeaders();
return new HttpEntity<>(body.getBody(), headers);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.PermissionsGroupService;
import org.avni.server.domain.metabase.PermissionsGraphResponse;
import org.avni.server.domain.metabase.PermissionsGroup;
import org.avni.server.domain.metabase.PermissionsGroupBody;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class PermissionsGroupRepository extends MetabaseConnector {

public PermissionsGroupRepository(RestTemplateBuilder restTemplateBuilder) {
super(restTemplateBuilder);
}

public PermissionsGroup save(PermissionsGroup permissionsGroup) {
String url = metabaseApiUrl + "/permissions/group";
PermissionsGroupBody body = new PermissionsGroupBody(permissionsGroup.getName());
HttpEntity<Map<String, Object>> entity = createJsonEntity(body);

PermissionsGroup response = restTemplate.postForObject(url, entity, PermissionsGroup.class);
return response;
}

public PermissionsGraphResponse getPermissionsGraph() {
String url = metabaseApiUrl + "/permissions/graph";
return getForObject(url, PermissionsGraphResponse.class);
}

public void updatePermissionsGraph(PermissionsGroupService permissions, int groupId, int databaseId) {
String url = metabaseApiUrl + "/permissions/graph";
Map<String, Object> requestBody = permissions.getUpdatedPermissionsGraph();
sendPutRequest(url, requestBody);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,85 @@
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CollectionPermissions {
private Map<String, Object> permissionsGraph;
private final CollectionPermissionsGraphResponse permissionsGraphResponse;

public CollectionPermissions(Map<String, Object> permissionsGraph) {
this.permissionsGraph = permissionsGraph;
@Autowired
public CollectionPermissions(CollectionPermissionsGraphResponse permissionsGraphResponse) {
this.permissionsGraphResponse = permissionsGraphResponse;
}

public Map<String, Object> getPermissionsGraph() {
return permissionsGraph;
/**
* Update the permissions graph to add write access for a specific group and collection.
*
* @param groupId the ID of the group
* @param collectionId the ID of the collection
*/
public void updatePermissionsGraph(int groupId, int collectionId) {
Map<String, Map<String, String>> groupsMap = permissionsGraphResponse.getGroups();
ensureGroupsMapExists(groupsMap);

Map<String, String> groupPermissions = getOrCreateGroupPermissions(groupsMap, groupId);
updateGroupPermissions(groupPermissions, collectionId);

handleSpecialGroupPermissions(groupsMap, collectionId);
}

public void updatePermissionsGraph(int groupId, int collectionId) {
Map<String, Map<String, String>> groups = (Map<String, Map<String, String>>) permissionsGraph.get("groups");
/**
* Ensure the groups map exists, throw an exception if it does not.
*
* @param groupsMap the map of groups
*/
private void ensureGroupsMapExists(Map<String, Map<String, String>> groupsMap) {
if (groupsMap == null) {
throw new RuntimeException("Groups not found in the collection permissions graph.");
}
}

/**
* Get the permissions map for a specific group, create it if it does not exist.
*
* @param groupsMap the map of all groups
* @param groupId the ID of the group
* @return the permissions map for the specified group
*/
private Map<String, String> getOrCreateGroupPermissions(Map<String, Map<String, String>> groupsMap, int groupId) {
return groupsMap.computeIfAbsent(String.valueOf(groupId), k -> new HashMap<>());
}

groups.computeIfAbsent(String.valueOf(groupId), k -> new HashMap<>());
Map<String, String> groupPermissions = groups.get(String.valueOf(groupId));
/**
* Update the permissions for a specific collection in a group.
*
* @param groupPermissions the map of permissions for a group
* @param collectionId the ID of the collection
*/
private void updateGroupPermissions(Map<String, String> groupPermissions, int collectionId) {
groupPermissions.put(String.valueOf(collectionId), "write");
}

if (groups.containsKey("1")) {
Map<String, String> allGroupPermissions = groups.get("1");
allGroupPermissions.put(String.valueOf(collectionId), "none");
/**
* Handle the special case for group with ID "1", setting its permission for the collection to "none".
*
* @param groupsMap the map of all groups
* @param collectionId the ID of the collection
*/
private void handleSpecialGroupPermissions(Map<String, Map<String, String>> groupsMap, int collectionId) {
if (groupsMap.containsKey("1")) {
Map<String, String> group1Permissions = groupsMap.get("1");
group1Permissions.put(String.valueOf(collectionId), "none");
}
}

/**
* Get the permissions graph response.
*
* @return the permissions graph response
*/
public CollectionPermissionsGraphResponse getPermissionsGraph() {
return permissionsGraphResponse;
}
}
Loading

0 comments on commit fd0215c

Please sign in to comment.