Commit f3024b5b authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Improved getUserPermission endpoint

parent 9fb9c38f
......@@ -6,11 +6,13 @@ import it.inaf.ia2.gms.client.call.AddMemberCall;
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.GetGroupPermissionsCall;
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.GroupPermission;
import it.inaf.ia2.gms.client.model.Permission;
import it.inaf.ia2.gms.client.model.UserPermission;
import java.util.List;
......@@ -74,6 +76,10 @@ public class GmsClient {
return new GetUserPermissionsCall(httpClientWrapper).getUserPermissions(userId);
}
public List<GroupPermission> getGroupPermissions(String groupId) {
return new GetGroupPermissionsCall(httpClientWrapper).getGroupPermissions(groupId);
}
public void addInvitedRegistration(String token, String email, Map<String, Permission> groupsPermissions) {
new AddInvitedRegistrationCall(httpClientWrapper).addInvitedRegistration(token, email, groupsPermissions);
}
......
package it.inaf.ia2.gms.client.call;
import it.inaf.ia2.gms.client.model.GroupPermission;
import it.inaf.ia2.gms.client.model.Permission;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GetGroupPermissionsCall extends BaseGmsCall {
public GetGroupPermissionsCall(HttpClientWrapper clientWrapper) {
super(clientWrapper);
}
public List<GroupPermission> getGroupPermissions(String groupId) {
List<GroupPermission> groupPermissions = new ArrayList<>();
String endpoint = "permission";
endpoint += "/" + groupId;
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(" ");
GroupPermission groupPermission = new GroupPermission();
groupPermission.setUserId(split[0]);
groupPermission.setPermission(Permission.valueOf(split[1]));
groupPermissions.add(groupPermission);
}
}
}
return groupPermissions;
}).join();
}
}
package it.inaf.ia2.gms.client.model;
public class GroupPermission {
private String userId;
private Permission permission;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Permission getPermission() {
return permission;
}
public void setPermission(Permission permission) {
this.permission = permission;
}
}
......@@ -126,6 +126,29 @@
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
......@@ -56,7 +56,7 @@ public class JWTWebServiceController {
@Autowired
private GroupsService groupsService;
@Autowired
private GroupNameService groupNameService;
......@@ -231,13 +231,22 @@ 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 {
@GetMapping(value = {"/permission/{group:.+}", "/permission/"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void getUserPermission(@PathVariable("group") Optional<String> groupNames, @RequestParam("user_id") Optional<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());
if (userId.isPresent()) {
try (PrintWriter pw = new PrintWriter(response.getOutputStream())) {
for (UserPermission userPermission : searchService.getUserPermission(userId.get(), permissionsManager.getCurrentUserPermissions(getRoot()))) {
String group = String.join(".", userPermission.getGroupCompleteName());
pw.println(group + " " + userPermission.getPermission());
}
}
} else {
GroupEntity groupEntity = getGroupFromNames(extractGroupNames(groupNames));
try (PrintWriter pw = new PrintWriter(response.getOutputStream())) {
for (it.inaf.ia2.gms.model.UserPermission up : permissionsManager.getAllPermissions(groupEntity)) {
pw.println(up.getUser().getId() + " " + up.getPermission());
}
}
}
}
......@@ -319,7 +328,6 @@ public class JWTWebServiceController {
.orElseThrow(() -> new IllegalStateException("Missing root group"));
}
private List<String> extractGroupNames(Optional<String> group) {
return extractGroupNames(group.orElse(null));
}
......
......@@ -4,6 +4,8 @@ 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.RapUser;
import it.inaf.ia2.gms.model.UserPermission;
import it.inaf.ia2.gms.persistence.GroupsDAO;
import it.inaf.ia2.gms.persistence.PermissionsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
......@@ -12,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 java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
......@@ -175,17 +178,33 @@ public class JWTWebServiceControllerTest {
@Test
public void testRemoveMember() throws Exception {
List<String> names = Arrays.asList("LBT", "INAF");
when(groupsDAO.findGroupByParentAndName("", "LBT")).thenReturn(Optional.of(lbt));
when(groupsDAO.findGroupByParentAndName("lbt_id", "INAF")).thenReturn(Optional.of(inaf));
mockMvc.perform(delete("/ws/jwt/membership/LBT.INAF?user_id=userId"))
.andExpect(status().isNoContent());
verify(membershipManager, times(1)).removeMember(eq(inaf), eq("userId"));
}
@Test
public void testGetGroupPermissions() throws Exception {
when(groupsDAO.findGroupByParentAndName("", "LBT")).thenReturn(Optional.of(lbt));
when(groupsDAO.findGroupByParentAndName("lbt_id", "INAF")).thenReturn(Optional.of(inaf));
List<UserPermission> permissions = new ArrayList<>();
UserPermission up = new UserPermission();
up.setUser(getRapUser());
up.setPermission(Permission.ADMIN);
permissions.add(up);
when(permissionsManager.getAllPermissions(any())).thenReturn(permissions);
mockMvc.perform(get("/ws/jwt/permission/LBT.INAF").principal(principal))
.andExpect(status().isOk())
.andExpect(content().string("rap_user ADMIN\n"));
}
@Test
public void testAddPermission() throws Exception {
......@@ -218,7 +237,7 @@ public class JWTWebServiceControllerTest {
public void testRemovePermission() throws Exception {
List<String> names = Arrays.asList("LBT", "INAF");
//when(groupsService.findGroupByNames(names)).thenReturn(Optional.of(inaf));
when(groupsDAO.findGroupByParentAndName("", "LBT")).thenReturn(Optional.of(lbt));
when(groupsDAO.findGroupByParentAndName("lbt_id", "INAF")).thenReturn(Optional.of(inaf));
......@@ -252,4 +271,10 @@ public class JWTWebServiceControllerTest {
inaf.setPath("lbt_id.inaf_id");
return inaf;
}
private RapUser getRapUser() {
RapUser user = new RapUser();
user.setId("rap_user");
return user;
}
}
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