diff --git a/gms/pom.xml b/gms/pom.xml index a67189db22dfa75e5041e23d47ceb30de2404e99..71d1429bcd77fe423e4852c9aaf041bf82378afc 100644 --- a/gms/pom.xml +++ b/gms/pom.xml @@ -155,6 +155,9 @@ <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> + <configuration> + <trimStackTrace>false</trimStackTrace> + </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> diff --git a/gms/src/main/java/it/inaf/ia2/gms/controller/PermissionsController.java b/gms/src/main/java/it/inaf/ia2/gms/controller/PermissionsController.java index 18593f140f2c17131bfbf684e4cb9a21c7f5e799..42d6b3208023d7e2eef82870b6987f060d47f54e 100644 --- a/gms/src/main/java/it/inaf/ia2/gms/controller/PermissionsController.java +++ b/gms/src/main/java/it/inaf/ia2/gms/controller/PermissionsController.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; @@ -128,10 +129,24 @@ public class PermissionsController { GroupEntity groupEntity = groupNameService.getGroupFromNames(groupNames); if (userId.isPresent()) { - try ( PrintWriter pw = new PrintWriter(response.getOutputStream())) { - for (UserPermission userPermission : searchService.getUserPermission(groupEntity, userId.get(), permissionsManager.getCurrentUserPermissions(groupEntity))) { - String group = groupNameService.getCompleteName(userPermission.getGroupCompleteName()); - pw.println(group + " " + userPermission.getPermission()); + if (userId.get().equals(request.getUserPrincipal().getName())) { + // asking my permissions + try ( PrintWriter pw = new PrintWriter(response.getOutputStream())) { + List<PermissionEntity> userPermissions = permissionsManager.getCurrentUserPermissions(); + + Map<String, List<String>> namesMap = groupNameService.getNamesFromIds(userPermissions.stream() + .map(pe -> pe.getGroupId()).collect(Collectors.toSet())); + + for (PermissionEntity pe : userPermissions) { + pw.println(groupNameService.getCompleteName(namesMap.get(pe.getGroupId())) + " " + pe.getPermission()); + } + } + } else { + try ( PrintWriter pw = new PrintWriter(response.getOutputStream())) { + for (UserPermission userPermission : searchService.getUserPermission(groupEntity, userId.get(), permissionsManager.getCurrentUserPermissions(groupEntity))) { + String group = groupNameService.getCompleteName(userPermission.getGroupCompleteName()); + pw.println(group + " " + userPermission.getPermission()); + } } } } else { diff --git a/gms/src/test/java/it/inaf/ia2/gms/controller/PermissionsControllerTest.java b/gms/src/test/java/it/inaf/ia2/gms/controller/PermissionsControllerTest.java index 9423ae002a5d8cf4798583cc49479c48d273c396..c56c04b0c099625e08d2ab4178927d6561e07f36 100644 --- a/gms/src/test/java/it/inaf/ia2/gms/controller/PermissionsControllerTest.java +++ b/gms/src/test/java/it/inaf/ia2/gms/controller/PermissionsControllerTest.java @@ -21,7 +21,9 @@ import it.inaf.ia2.gms.service.SearchService; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.Set; import static org.hamcrest.CoreMatchers.is; import org.junit.Before; import org.junit.Test; @@ -148,6 +150,23 @@ public class PermissionsControllerTest { .andExpect(content().string("rap_user ADMIN\n")); } + @Test + public void testGetMyPermissions() throws Exception { + + when(groupsDAO.findGroupById("ROOT")).thenReturn(Optional.of(new GroupEntity())); + when(groupsDAO.getGroupCompleteNamesFromId(Set.of("group_id"))).thenReturn(Map.of("group_id", "TNG")); + + PermissionEntity pe = new PermissionEntity(); + pe.setGroupId("group_id"); + pe.setPermission(Permission.VIEW_MEMBERS); + when(permissionsManager.getCurrentUserPermissions()).thenReturn(List.of(pe)); + + mockMvc.perform(get("/permission?user_id=TEST_PRINCIPAL").principal(getPrincipal()) + .accept(MediaType.TEXT_PLAIN)) + .andExpect(status().isOk()) + .andExpect(content().string("TNG VIEW_MEMBERS\n")); + } + @Test public void testGetUserPermissions() throws Exception {