forked from avniproject/avni-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avniproject#733 | Some refactoring changes,and fixed the permissions …
…group error which was caused due to wrong code being pushed in my last commit
- Loading branch information
1 parent
98b4545
commit b48b852
Showing
24 changed files
with
428 additions
and
228 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...erver-api/src/main/java/org/avni/server/dao/metabase/CollectionPermissionsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
avni-server-api/src/main/java/org/avni/server/dao/metabase/GroupPermissionsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
avni-server-api/src/main/java/org/avni/server/dao/metabase/MetabaseRepository.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
avni-server-api/src/main/java/org/avni/server/dao/metabase/PermissionsRepository.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
avni-server-api/src/main/java/org/avni/server/domain/metabase/CollectionPermissions.java
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
...api/src/main/java/org/avni/server/domain/metabase/CollectionPermissionsGraphResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...erver-api/src/main/java/org/avni/server/domain/metabase/CollectionPermissionsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
avni-server-api/src/main/java/org/avni/server/domain/metabase/CollectionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.