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 b48b852
Show file tree
Hide file tree
Showing 24 changed files with 428 additions and 228 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.CollectionPermissionsService;
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(CollectionPermissionsService collectionPermissionsService, int groupId, int collectionId) {
collectionPermissionsService.updatePermissions(groupId, collectionId);
String url = metabaseApiUrl + "/collection/graph";
sendPutRequest(url, collectionPermissionsService.getUpdatedPermissionsGraph());
}
}
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,39 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.GroupPermissionsService;
import org.avni.server.domain.metabase.GroupPermissionsGraphResponse;
import org.avni.server.domain.metabase.Group;
import org.avni.server.domain.metabase.GroupPermissionsBody;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class GroupPermissionsRepository extends MetabaseConnector {

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

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

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

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

public void updatePermissionsGraph(GroupPermissionsService permissions, int groupId, int databaseId) {
String url = metabaseApiUrl + "/permissions/graph";
Map<String, Object> requestBody = permissions.getUpdatedPermissionsGraph();
sendPutRequest(url, requestBody);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.GroupPermissionsBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
Expand All @@ -8,11 +9,10 @@
import org.springframework.http.MediaType;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Repository
public class BaseMetabaseRepository {
public class MetabaseConnector {
protected final RestTemplate restTemplate;

@Value("${metabase.api.url}")
Expand All @@ -21,11 +21,11 @@ public class BaseMetabaseRepository {
@Value("${metabase.api.key}")
private String apiKey;

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

private HttpHeaders getHeaders() {
protected HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);
Expand All @@ -37,8 +37,24 @@ protected <T> HttpEntity<T> createHttpEntity(T body) {
return new HttpEntity<>(body, headers);
}

protected void sendPutRequest(String url, Map<String, Object> requestBody) {
HttpEntity<Map<String, Object>> entity = createHttpEntity(requestBody);
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(GroupPermissionsBody body) {
HttpHeaders headers = getHeaders();
return new HttpEntity<>(body.getBody(), headers);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

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

import java.util.Map;

import org.springframework.stereotype.Component;

@Component
public class CollectionPermissionsGraphResponse {
private int revision;
private Map<String, Map<String, String>> groups;

public int getRevision() {
return revision;
}

public void setRevision(int revision) {
this.revision = revision;
}

public Map<String, Map<String, String>> getGroups() {
return groups;
}

public void setGroups(Map<String, Map<String, String>> groups) {
this.groups = groups;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.avni.server.domain.metabase;

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

import org.springframework.stereotype.Component;

@Component
public class CollectionPermissionsService {
private final CollectionPermissionsGraphResponse permissionsGraph;

public CollectionPermissionsService(CollectionPermissionsGraphResponse permissionsGraph) {
this.permissionsGraph = permissionsGraph;
}

public void updatePermissions(int groupId, int collectionId) {
Map<String, Map<String, String>> groups = permissionsGraph.getGroups();

ensureGroupsMapExists(groups);

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

handleSpecialGroupPermissions(groups, collectionId);
}

private void ensureGroupsMapExists(Map<String, Map<String, String>> groupsMap) {
if (groupsMap == null) {
throw new RuntimeException("Groups not found in the collection permissions graph.");
}
}

private Map<String, String> getOrCreateGroupPermissions(Map<String, Map<String, String>> groupsMap, int groupId) {
return groupsMap.computeIfAbsent(String.valueOf(groupId), k -> new HashMap<>());
}

private void updateGroupPermissions(Map<String, String> groupPermissions, int collectionId) {
groupPermissions.put(String.valueOf(collectionId), "write");
}

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");
}
}

public Map<String, Object> getUpdatedPermissionsGraph() {
Map<String, Object> updatedPermissionsGraph = new HashMap<>();
updatedPermissionsGraph.put("groups", permissionsGraph.getGroups());
updatedPermissionsGraph.put("revision", permissionsGraph.getRevision());
return updatedPermissionsGraph;
}

public CollectionPermissionsGraphResponse getPermissionsGraph() {
return permissionsGraph;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.avni.server.domain.metabase;

import org.springframework.stereotype.Component;

@Component
public class CollectionResponse {
private int id;

Expand Down
Loading

0 comments on commit b48b852

Please sign in to comment.