Commit 947c53e1 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Added web service endpoint for retrieving user permissions. Updated client lib

parent f63b310a
......@@ -6,10 +6,12 @@ import it.inaf.ia2.gms.client.call.AddPermissionCall;
import it.inaf.ia2.gms.client.call.CreateGroupCall;
import it.inaf.ia2.gms.client.call.DeleteGroupCall;
import it.inaf.ia2.gms.client.call.GetUserGroupsCall;
import it.inaf.ia2.gms.client.call.GetUserPermissionsCall;
import it.inaf.ia2.gms.client.call.ListGroupsCall;
import it.inaf.ia2.gms.client.call.RemoveMemberCall;
import it.inaf.ia2.gms.client.call.RemovePermissionCall;
import it.inaf.ia2.gms.client.model.Permission;
import it.inaf.ia2.gms.client.model.UserPermission;
import java.util.List;
public class GmsClient {
......@@ -21,7 +23,6 @@ public class GmsClient {
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
baseUrl += "ws/jwt";
httpClientWrapper = new HttpClientWrapper(baseUrl);
}
......@@ -66,4 +67,8 @@ public class GmsClient {
public void removePermission(String completeGroupName, String userId) {
new RemovePermissionCall(httpClientWrapper).removePermission(completeGroupName, userId);
}
public List<UserPermission> getUserPermissions(String userId) {
return new GetUserPermissionsCall(httpClientWrapper).getUserPermissions(userId);
}
}
package it.inaf.ia2.gms.client.call;
import it.inaf.ia2.gms.client.model.Permission;
import it.inaf.ia2.gms.client.model.UserPermission;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GetUserPermissionsCall extends BaseGmsCall {
public GetUserPermissionsCall(HttpClientWrapper clientWrapper) {
super(clientWrapper);
}
public List<UserPermission> getUserPermissions(String userId) {
List<UserPermission> userPermissions = new ArrayList<>();
String endpoint = "permission";
endpoint += "?user_id=" + userId;
HttpRequest groupsRequest = newHttpRequest(endpoint)
.header("Accept", "text/plain")
.GET()
.build();
return getClient().sendAsync(groupsRequest, HttpResponse.BodyHandlers.ofInputStream())
.thenApply(response -> {
if (response.statusCode() == 200) {
return response.body();
}
logServerErrorInputStream(groupsRequest, response);
throw new IllegalStateException("Unable to retrieve groups");
})
.thenApply(inputStream -> {
try (Scanner scan = new Scanner(inputStream)) {
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (!line.isEmpty()) {
String[] split = line.split(" ");
UserPermission userPermission = new UserPermission();
userPermission.setGroup(split[0]);
userPermission.setPermission(Permission.valueOf(split[1]));
userPermissions.add(userPermission);
}
}
}
return userPermissions;
}).join();
}
}
package it.inaf.ia2.gms.client.model;
public class UserPermission {
private String group;
private Permission permission;
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public Permission getPermission() {
return permission;
}
public void setPermission(Permission permission) {
this.permission = permission;
}
}
......@@ -6,6 +6,7 @@ import it.inaf.ia2.gms.manager.GroupsManager;
import it.inaf.ia2.gms.manager.MembershipManager;
import it.inaf.ia2.gms.manager.PermissionsManager;
import it.inaf.ia2.gms.model.Permission;
import it.inaf.ia2.gms.model.response.UserPermission;
import it.inaf.ia2.gms.persistence.GroupsDAO;
import it.inaf.ia2.gms.persistence.PermissionsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
......@@ -13,6 +14,7 @@ import it.inaf.ia2.gms.persistence.model.PermissionEntity;
import it.inaf.ia2.gms.service.GroupsService;
import it.inaf.ia2.gms.service.JoinService;
import it.inaf.ia2.gms.service.PermissionUtils;
import it.inaf.ia2.gms.service.SearchService;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
......@@ -64,6 +66,9 @@ public class JWTWebServiceController {
@Autowired
private PermissionsDAO permissionsDAO;
@Autowired
private SearchService searchService;
/**
* This endpoint is compliant with the IVOA GMS standard.
*/
......@@ -220,6 +225,17 @@ public class JWTWebServiceController {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
@GetMapping(value = "/permission", produces = MediaType.TEXT_PLAIN_VALUE)
public void getUserPermission(@RequestParam("user_id") String userId, HttpServletRequest request, HttpServletResponse response) throws IOException {
try (PrintWriter pw = new PrintWriter(response.getOutputStream())) {
for (UserPermission userPermission : searchService.getUserPermission(userId, permissionsManager.getCurrentUserPermissions(getRoot()))) {
String group = String.join(".", userPermission.getGroupCompleteName());
pw.println(group + " " + userPermission.getPermission());
}
}
}
@PostMapping(value = {"/permission/{group:.+}", "/permission/"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void addPermission(@PathVariable("group") Optional<String> groupNames, HttpServletRequest request, HttpServletResponse response) throws IOException {
......
......@@ -145,7 +145,7 @@ public class SearchService {
.collect(Collectors.toList());
}
private List<UserPermission> getUserPermission(String targetUserId, List<PermissionEntity> actorPermissions) {
public List<UserPermission> getUserPermission(String targetUserId, List<PermissionEntity> actorPermissions) {
List<UserPermission> permissions = new ArrayList<>();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment