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; ...@@ -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.AddPermissionCall;
import it.inaf.ia2.gms.client.call.CreateGroupCall; import it.inaf.ia2.gms.client.call.CreateGroupCall;
import it.inaf.ia2.gms.client.call.DeleteGroupCall; 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.GetUserGroupsCall;
import it.inaf.ia2.gms.client.call.GetUserPermissionsCall; import it.inaf.ia2.gms.client.call.GetUserPermissionsCall;
import it.inaf.ia2.gms.client.call.ListGroupsCall; import it.inaf.ia2.gms.client.call.ListGroupsCall;
import it.inaf.ia2.gms.client.call.RemoveMemberCall; import it.inaf.ia2.gms.client.call.RemoveMemberCall;
import it.inaf.ia2.gms.client.call.RemovePermissionCall; 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.Permission;
import it.inaf.ia2.gms.client.model.UserPermission; import it.inaf.ia2.gms.client.model.UserPermission;
import java.util.List; import java.util.List;
...@@ -74,6 +76,10 @@ public class GmsClient { ...@@ -74,6 +76,10 @@ public class GmsClient {
return new GetUserPermissionsCall(httpClientWrapper).getUserPermissions(userId); 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) { public void addInvitedRegistration(String token, String email, Map<String, Permission> groupsPermissions) {
new AddInvitedRegistrationCall(httpClientWrapper).addInvitedRegistration(token, email, 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 @@ ...@@ -126,6 +126,29 @@
<executable>true</executable> <executable>true</executable>
</configuration> </configuration>
</plugin> </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> </plugins>
</build> </build>
</project> </project>
...@@ -56,7 +56,7 @@ public class JWTWebServiceController { ...@@ -56,7 +56,7 @@ public class JWTWebServiceController {
@Autowired @Autowired
private GroupsService groupsService; private GroupsService groupsService;
@Autowired @Autowired
private GroupNameService groupNameService; private GroupNameService groupNameService;
...@@ -231,13 +231,22 @@ public class JWTWebServiceController { ...@@ -231,13 +231,22 @@ public class JWTWebServiceController {
response.setStatus(HttpServletResponse.SC_NO_CONTENT); response.setStatus(HttpServletResponse.SC_NO_CONTENT);
} }
@GetMapping(value = "/permission", produces = MediaType.TEXT_PLAIN_VALUE) @GetMapping(value = {"/permission/{group:.+}", "/permission/"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void getUserPermission(@RequestParam("user_id") String userId, HttpServletRequest request, HttpServletResponse response) throws IOException { 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())) { if (userId.isPresent()) {
for (UserPermission userPermission : searchService.getUserPermission(userId, permissionsManager.getCurrentUserPermissions(getRoot()))) { try (PrintWriter pw = new PrintWriter(response.getOutputStream())) {
String group = String.join(".", userPermission.getGroupCompleteName()); for (UserPermission userPermission : searchService.getUserPermission(userId.get(), permissionsManager.getCurrentUserPermissions(getRoot()))) {
pw.println(group + " " + userPermission.getPermission()); 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 { ...@@ -319,7 +328,6 @@ public class JWTWebServiceController {
.orElseThrow(() -> new IllegalStateException("Missing root group")); .orElseThrow(() -> new IllegalStateException("Missing root group"));
} }
private List<String> extractGroupNames(Optional<String> group) { private List<String> extractGroupNames(Optional<String> group) {
return extractGroupNames(group.orElse(null)); return extractGroupNames(group.orElse(null));
} }
......
...@@ -4,6 +4,8 @@ import it.inaf.ia2.gms.manager.GroupsManager; ...@@ -4,6 +4,8 @@ import it.inaf.ia2.gms.manager.GroupsManager;
import it.inaf.ia2.gms.manager.MembershipManager; import it.inaf.ia2.gms.manager.MembershipManager;
import it.inaf.ia2.gms.manager.PermissionsManager; import it.inaf.ia2.gms.manager.PermissionsManager;
import it.inaf.ia2.gms.model.Permission; 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.GroupsDAO;
import it.inaf.ia2.gms.persistence.PermissionsDAO; import it.inaf.ia2.gms.persistence.PermissionsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity; import it.inaf.ia2.gms.persistence.model.GroupEntity;
...@@ -12,6 +14,7 @@ import it.inaf.ia2.gms.persistence.model.PermissionEntity; ...@@ -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.GroupsService;
import it.inaf.ia2.gms.service.JoinService; import it.inaf.ia2.gms.service.JoinService;
import java.security.Principal; import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -175,17 +178,33 @@ public class JWTWebServiceControllerTest { ...@@ -175,17 +178,33 @@ public class JWTWebServiceControllerTest {
@Test @Test
public void testRemoveMember() throws Exception { public void testRemoveMember() throws Exception {
List<String> names = Arrays.asList("LBT", "INAF");
when(groupsDAO.findGroupByParentAndName("", "LBT")).thenReturn(Optional.of(lbt)); when(groupsDAO.findGroupByParentAndName("", "LBT")).thenReturn(Optional.of(lbt));
when(groupsDAO.findGroupByParentAndName("lbt_id", "INAF")).thenReturn(Optional.of(inaf)); when(groupsDAO.findGroupByParentAndName("lbt_id", "INAF")).thenReturn(Optional.of(inaf));
mockMvc.perform(delete("/ws/jwt/membership/LBT.INAF?user_id=userId")) mockMvc.perform(delete("/ws/jwt/membership/LBT.INAF?user_id=userId"))
.andExpect(status().isNoContent()); .andExpect(status().isNoContent());
verify(membershipManager, times(1)).removeMember(eq(inaf), eq("userId")); 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 @Test
public void testAddPermission() throws Exception { public void testAddPermission() throws Exception {
...@@ -218,7 +237,7 @@ public class JWTWebServiceControllerTest { ...@@ -218,7 +237,7 @@ public class JWTWebServiceControllerTest {
public void testRemovePermission() throws Exception { public void testRemovePermission() throws Exception {
List<String> names = Arrays.asList("LBT", "INAF"); List<String> names = Arrays.asList("LBT", "INAF");
//when(groupsService.findGroupByNames(names)).thenReturn(Optional.of(inaf)); //when(groupsService.findGroupByNames(names)).thenReturn(Optional.of(inaf));
when(groupsDAO.findGroupByParentAndName("", "LBT")).thenReturn(Optional.of(lbt)); when(groupsDAO.findGroupByParentAndName("", "LBT")).thenReturn(Optional.of(lbt));
when(groupsDAO.findGroupByParentAndName("lbt_id", "INAF")).thenReturn(Optional.of(inaf)); when(groupsDAO.findGroupByParentAndName("lbt_id", "INAF")).thenReturn(Optional.of(inaf));
...@@ -252,4 +271,10 @@ public class JWTWebServiceControllerTest { ...@@ -252,4 +271,10 @@ public class JWTWebServiceControllerTest {
inaf.setPath("lbt_id.inaf_id"); inaf.setPath("lbt_id.inaf_id");
return inaf; 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