Skip to content

Commit

Permalink
avniproject#786 | Added a check for metabaseGroup and permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ombhardwajj committed Sep 14, 2024
1 parent d336318 commit 5c38b12
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public GroupPermissionsGraphResponse getPermissionsGraph() {
return getForObject(url, GroupPermissionsGraphResponse.class);
}

public void updatePermissionsGraph(GroupPermissionsService permissions, int groupId, int databaseId) {
public void updatePermissionsGraph(GroupPermissionsService permissions) {
String url = metabaseApiUrl + "/permissions/graph";
Map<String, Object> requestBody = permissions.getUpdatedPermissionsGraph();
sendPutRequest(url, requestBody);
Expand All @@ -40,4 +40,25 @@ public List<GroupPermissionResponse> getAllGroups() {
GroupPermissionResponse[] response = getForObject(url, GroupPermissionResponse[].class);
return Arrays.asList(response);
}

public void updateGroupPermissions(int groupId, int databaseId) {
GroupPermissionsService groupPermissions = new GroupPermissionsService(getPermissionsGraph());
groupPermissions.updatePermissions(groupId, databaseId);
updatePermissionsGraph(groupPermissions);
}


public Group findOrCreateGroup(String name, int databaseId, int collectionId) {
List<GroupPermissionResponse> existingGroups = getAllGroups();

for (GroupPermissionResponse group : existingGroups) {
if (group.getName().equals(name)) {
return new Group(group.getName(), group.getId());
}
}

Group newGroup = save(new Group(name));
updateGroupPermissions(newGroup.getId(), databaseId);
return newGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import java.util.HashMap;
import java.util.Map;

import org.avni.server.dao.metabase.CollectionPermissionsRepository;
import org.springframework.stereotype.Component;

@Component
public class CollectionPermissionsService {
private final CollectionPermissionsRepository collectionPermissionsRepository;
private final CollectionPermissionsGraphResponse permissionsGraph;

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

public void updatePermissions(int groupId, int collectionId) {
Expand Down Expand Up @@ -55,4 +58,9 @@ public Map<String, Object> getUpdatedPermissionsGraph() {
public CollectionPermissionsGraphResponse getPermissionsGraph() {
return permissionsGraph;
}

public void updateAndSavePermissions(CollectionPermissionsRepository collectionPermissionsRepository, int groupId, int collectionId) {
updatePermissions(groupId, collectionId);
collectionPermissionsRepository.updateCollectionPermissions(this, groupId, collectionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MetabaseService {

Expand Down Expand Up @@ -57,15 +54,13 @@ public void setupMetabase() {
globalCollection = new CollectionInfoResponse(null, metabaseCollection.getId(), false);
}

Group metabaseGroup = findOrCreateGroup(name);
Group metabaseGroup = groupPermissionsRepository.findOrCreateGroup(name, globalDatabase.getId(), globalCollection.getIdAsInt());

GroupPermissionsService groupPermissions = new GroupPermissionsService(groupPermissionsRepository.getPermissionsGraph());
groupPermissions.updatePermissions(metabaseGroup.getId(), globalDatabase.getId());
groupPermissionsRepository.updatePermissionsGraph(groupPermissions, metabaseGroup.getId(), globalDatabase.getId());

CollectionPermissionsService collectionPermissions = new CollectionPermissionsService(collectionPermissionsRepository.getCollectionPermissionsGraph());
collectionPermissions.updatePermissions(metabaseGroup.getId(), globalCollection.getIdAsInt());
collectionPermissionsRepository.updateCollectionPermissions(collectionPermissions, metabaseGroup.getId(), globalCollection.getIdAsInt());
CollectionPermissionsService collectionPermissions = new CollectionPermissionsService(
collectionPermissionsRepository.getCollectionPermissionsGraph(),
collectionPermissionsRepository
);
collectionPermissions.updateAndSavePermissions(collectionPermissionsRepository, metabaseGroup.getId(), globalCollection.getIdAsInt());
}

public Database getGlobalDatabase() {
Expand All @@ -91,14 +86,4 @@ public CollectionInfoResponse getGlobalCollection() {
return globalCollection;
}

private Group findOrCreateGroup(String name) {
List<GroupPermissionResponse> existingGroups = groupPermissionsRepository.getAllGroups();
for (GroupPermissionResponse group : existingGroups) {
if (group.getName().equals(name)) {
return new Group( group.getName(),group.getId());
}
}
return groupPermissionsRepository.save(new Group(name));
}

}

0 comments on commit 5c38b12

Please sign in to comment.