GET /api/eperson/groups/<:uuid>/object

This commit is contained in:
Antoine Snyers
2020-03-16 10:55:27 +01:00
parent de79405b74
commit 65a1a2d15e
4 changed files with 191 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ import static java.util.regex.Pattern.compile;
import static org.apache.http.HttpStatus.SC_NO_CONTENT;
import static org.apache.http.HttpStatus.SC_UNPROCESSABLE_ENTITY;
import static org.dspace.app.rest.utils.ContextUtil.obtainContext;
import static org.dspace.app.rest.utils.RegexUtils.REGEX_REQUESTMAPPING_IDENTIFIER_AS_UUID;
import static org.dspace.app.rest.utils.RegexUtils.REGEX_UUID;
import static org.dspace.app.util.AuthorizeUtil.authorizeManageAdminGroup;
import static org.dspace.app.util.AuthorizeUtil.authorizeManageSubmittersGroup;
@@ -30,7 +31,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dspace.app.rest.exception.UnprocessableEntityException;
import org.dspace.app.rest.model.DSpaceObjectRest;
import org.dspace.app.rest.model.GroupRest;
import org.dspace.app.rest.repository.GroupRestRepository;
import org.dspace.app.rest.utils.GroupUtil;
import org.dspace.app.rest.utils.Utils;
import org.dspace.authorize.AuthorizeException;
@@ -49,6 +52,7 @@ import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
@@ -76,6 +80,9 @@ public class GroupRestController {
@Autowired
GroupUtil groupUtil;
@Autowired
GroupRestRepository repository;
/**
* Method to add one or more subgroups to a group.
* The subgroups to be added should be provided in the request body as a uri-list.
@@ -308,4 +315,9 @@ public class GroupRestController {
throw new AuthorizeException("not authorized to manage this group");
}
@RequestMapping(method = RequestMethod.GET, value = REGEX_REQUESTMAPPING_IDENTIFIER_AS_UUID + "/object")
public DSpaceObjectRest object(@PathVariable UUID uuid) {
return repository.object(uuid);
}
}

View File

@@ -10,12 +10,19 @@ package org.dspace.app.rest.repository;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.Parameter;
import org.dspace.app.rest.SearchRestMethod;
import org.dspace.app.rest.converter.ConverterService;
import org.dspace.app.rest.converter.MetadataConverter;
import org.dspace.app.rest.exception.RepositoryMethodNotImplementedException;
import org.dspace.app.rest.exception.UnprocessableEntityException;
import org.dspace.app.rest.model.DSpaceObjectRest;
import org.dspace.app.rest.model.GroupRest;
import org.dspace.app.rest.model.patch.Patch;
import org.dspace.app.rest.utils.Utils;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.core.Context;
import org.dspace.eperson.Group;
import org.dspace.eperson.service.GroupService;
@@ -45,6 +52,18 @@ public class GroupRestRepository extends DSpaceObjectRestRepository<Group, Group
@Autowired
GroupService gs;
@Autowired
CollectionService collectionService;
@Autowired
CommunityService communityService;
@Autowired
protected Utils utils;
@Autowired
protected ConverterService converter;
@Autowired
GroupRestRepository(GroupService dsoService) {
super(dsoService);
@@ -169,4 +188,37 @@ public class GroupRestRepository extends DSpaceObjectRestRepository<Group, Group
}
}
@PreAuthorize("hasAuthority('ADMIN')")
public DSpaceObjectRest object(UUID uuid) {
Context context = obtainContext();
try {
Group group = gs.find(context, uuid);
if (group == null) {
throw new ResourceNotFoundException(
GroupRest.CATEGORY + "." + GroupRest.NAME
+ " with id: " + uuid + " not found"
);
} else {
final Collection collection = collectionService.findByGroup(context, group);
if (collection != null) {
return converter.toRest(collection, utils.obtainProjection());
} else {
final Community community = communityService.findByAdminGroup(context, group);
if (community != null) {
return converter.toRest(community, utils.obtainProjection());
} else {
throw new ResourceNotFoundException(
GroupRest.CATEGORY + "." + GroupRest.NAME
+ " with id: " + uuid
+ " has no associated collection or community"
);
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}

View File

@@ -26,6 +26,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.builder.CollectionBuilder;
import org.dspace.app.rest.builder.CommunityBuilder;
import org.dspace.app.rest.builder.EPersonBuilder;
import org.dspace.app.rest.builder.GroupBuilder;
@@ -36,8 +37,10 @@ import org.dspace.app.rest.model.MetadataRest;
import org.dspace.app.rest.model.MetadataValueRest;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.app.rest.test.MetadataPatchSuite;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
@@ -1514,7 +1517,6 @@ public class GroupRestRepositoryIT extends AbstractControllerIntegrationTest {
public void deleteGroupTest() throws Exception {
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
Group parentGroup = null;
@@ -1552,7 +1554,6 @@ public class GroupRestRepositoryIT extends AbstractControllerIntegrationTest {
public void deleteGroupUnauthorizedTest() throws Exception {
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
Group parentGroup = null;
@@ -1590,7 +1591,6 @@ public class GroupRestRepositoryIT extends AbstractControllerIntegrationTest {
public void deleteGroupForbiddenTest() throws Exception {
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
Group parentGroup = null;
@@ -1628,24 +1628,116 @@ public class GroupRestRepositoryIT extends AbstractControllerIntegrationTest {
@Test
public void deleteGroupNotFoundTest() throws Exception {
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
context.turnOffAuthorisationSystem();
context.commit();
Group parentGroup = null;
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(
delete("/api/eperson/groups/" + UUID.randomUUID())
).andExpect(status().isNotFound());
}
@Test
public void getGroupObjectCommunityTest() throws Exception {
CommunityService communityService
= ContentServiceFactory.getInstance().getCommunityService();
Community community = null;
Group adminGroup = null;
try {
context.turnOffAuthorisationSystem();
community = communityService.create(null, context);
adminGroup = communityService.createAdministrators(context, community);
context.commit();
adminGroup = context.reloadEntity(adminGroup);
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(
delete("/api/eperson/groups/" + UUID.randomUUID())
).andExpect(status().isNotFound());
get("/api/eperson/groups/" + adminGroup.getID() + "/object")
).andExpect(status().isOk());
} finally {
if (parentGroup != null) {
GroupBuilder.deleteGroup(parentGroup.getID());
if (community != null) {
CommunityBuilder.deleteCommunity(community.getID());
}
if (adminGroup != null) {
GroupBuilder.deleteGroup(adminGroup.getID());
}
}
}
@Test
public void getGroupObjectCollectionTest() throws Exception {
CommunityService communityService
= ContentServiceFactory.getInstance().getCommunityService();
CollectionService collectionService
= ContentServiceFactory.getInstance().getCollectionService();
Community community = null;
Collection collection = null;
Group adminGroup = null;
try {
context.turnOffAuthorisationSystem();
community = communityService.create(null, context);
collection = collectionService.create(context, community);
adminGroup = collectionService.createAdministrators(context, collection);
context.commit();
adminGroup = context.reloadEntity(adminGroup);
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(
get("/api/eperson/groups/" + adminGroup.getID() + "/object")
).andExpect(status().isOk());
} finally {
if (collection != null) {
CollectionBuilder.deleteCollection(collection.getID());
}
if (community != null) {
CommunityBuilder.deleteCommunity(community.getID());
}
if (adminGroup != null) {
GroupBuilder.deleteGroup(adminGroup.getID());
}
}
}
@Test
public void getGroupObjectNotFoundTest() throws Exception {
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
Group adminGroup = null;
try {
context.turnOffAuthorisationSystem();
adminGroup = groupService.create(context);
context.commit();
adminGroup = context.reloadEntity(adminGroup);
String authToken = getAuthToken(admin.getEmail(), password);
getClient(authToken).perform(
get("/api/eperson/groups/" + adminGroup.getID() + "/object")
).andExpect(status().isNotFound());
} finally {
if (adminGroup != null) {
GroupBuilder.deleteGroup(adminGroup.getID());
}
}
}
@Test
public void getGroupObjectUnauthorizedTest() throws Exception {
GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
Group adminGroup = null;
try {
context.turnOffAuthorisationSystem();
adminGroup = groupService.create(context);
context.commit();
adminGroup = context.reloadEntity(adminGroup);
getClient().perform(
get("/api/eperson/groups/" + adminGroup.getID() + "/object")
).andExpect(status().isUnauthorized());
} finally {
if (adminGroup != null) {
GroupBuilder.deleteGroup(adminGroup.getID());
}
}
}

View File

@@ -10,6 +10,7 @@ package org.dspace.app.rest.builder;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharEncoding;
@@ -179,6 +180,28 @@ public class CollectionBuilder extends AbstractDSpaceObjectBuilder<Collection> {
indexingService.commit();
}
/**
* Delete the Test Collection referred to by the given UUID
*
* @param uuid UUID of Test Collection to delete
* @throws SQLException
* @throws IOException
*/
public static void deleteCollection(UUID uuid) throws SQLException, IOException {
try (Context c = new Context()) {
c.turnOffAuthorisationSystem();
Collection collection = collectionService.find(c, uuid);
if (collection != null) {
try {
collectionService.delete(c, collection);
} catch (AuthorizeException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
c.complete();
}
}
@Override
protected DSpaceObjectService<Collection> getService() {
return collectionService;