Skip to content

Commit

Permalink
The clarin user registration is created if it doesn't exist, but it i…
Browse files Browse the repository at this point in the history
…s in the eperson table.
  • Loading branch information
milanmajchrak committed Sep 13, 2023
1 parent a3a9882 commit 11ee9e3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.dspace.content.service.clarin.ClarinLicenseService;
import org.dspace.content.service.clarin.ClarinUserRegistrationService;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -72,6 +73,9 @@ public class ClarinLicenseRestRepository extends DSpaceRestRepository<ClarinLice
@Autowired
ItemService itemService;

@Autowired
ClarinUserRegistrationService clarinUserRegistrationService;

@Override
@PreAuthorize("permitAll()")
public ClarinLicenseRest findOne(Context context, Integer idValue) {
Expand Down Expand Up @@ -155,6 +159,7 @@ protected ClarinLicenseRest createAndReturn(Context context)
"license label cannot be null or empty");
}

// Get user registration record according to current user or create a new record if it doesn't exist yet.
ClarinUserRegistration userRegistration = getUserRegistration(context);

// create
Expand Down Expand Up @@ -265,7 +270,13 @@ private ClarinLicenseLabel getClarinLicenseLabelFromRest(ClarinLicenseLabelRest
return clarinLicenseLabel;
}

private ClarinUserRegistration getUserRegistration(Context context) throws SQLException {
/**
* The user is already authenticated, so he exists in the eperson table.
* @param context
* @return
* @throws SQLException
*/
private ClarinUserRegistration getUserRegistration(Context context) throws SQLException, AuthorizeException {
List<ClarinUserRegistration> userRegistrations = userRegistrationService.findByEPersonUUID(context,
context.getCurrentUser().getID());

Expand All @@ -275,11 +286,23 @@ private ClarinUserRegistration getUserRegistration(Context context) throws SQLEx
}

// In some special case after the migration the user could be inserted into the `eperson` table,
// but it is not in the `user_registration` table. Maybe the user was signed in via Shibboleth.
// but it is not inserted in the `user_registration` table. Maybe the user was signed in via Shibboleth.
// Create a new ClarinUserRegistration record in this case.
throw new UnprocessableEntityException("Clarin License user registration, " +
"cannot be null");
// ClarinUserRegistration userRegistration = userRegistrations.get(0);

// Get current user
EPerson ePerson = context.getCurrentUser();
if (Objects.isNull(ePerson)) {
throw new UnprocessableEntityException("Cannot create a ClarinUserRegistration because the " +
"current user from the context is null.");
}

ClarinUserRegistration clarinUserRegistration = new ClarinUserRegistration();
clarinUserRegistration.setConfirmation(true);
clarinUserRegistration.setEmail(ePerson.getEmail());
clarinUserRegistration.setPersonID(ePerson.getID());
clarinUserRegistration.setOrganization(ePerson.getNetid());
clarinUserRegistrationService.create(context, clarinUserRegistration);
return clarinUserRegistration;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
import org.dspace.content.service.clarin.ClarinLicenseLabelService;
import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService;
import org.dspace.content.service.clarin.ClarinLicenseService;
import org.dspace.content.service.clarin.ClarinUserRegistrationService;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

/**
* Integration tests for the Clarin License Rest Repository
Expand All @@ -73,6 +75,10 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration

@Autowired
ClarinLicenseResourceMappingService clarinLicenseResourceMappingService;

@Autowired
ClarinUserRegistrationService clarinUserRegistrationService;

ClarinLicense firstCLicense;
ClarinLicense secondCLicense;

Expand Down Expand Up @@ -494,6 +500,73 @@ public void findAllBitstreamsAttachedToLicense() throws Exception {
.andExpect(jsonPath("$.bitstreams", is(2)));
}

@Test
public void createUserRegistrationIfItDoesntExist() throws Exception {
context.setCurrentUser(admin);
ClarinLicenseRest clarinLicenseRest = new ClarinLicenseRest();
clarinLicenseRest.setName("name");
clarinLicenseRest.setBitstreams(0);
clarinLicenseRest.setConfirmation(4);
clarinLicenseRest.setRequiredInfo("Not required");
clarinLicenseRest.setDefinition("definition");
clarinLicenseConverter.setExtendedClarinLicenseLabels(clarinLicenseRest, firstCLicense.getLicenseLabels(),
Projection.DEFAULT);
clarinLicenseConverter.setClarinLicenseLabel(clarinLicenseRest, firstCLicense.getLicenseLabels(),
Projection.DEFAULT);
// id of created clarin license
AtomicReference<Integer> idRef = new AtomicReference<>();
String authTokenAdmin = getAuthToken(admin.getEmail(), password);
try {
getClient(authTokenAdmin).perform(post("/api/core/clarinlicenses")
.content(new ObjectMapper().writeValueAsBytes(clarinLicenseRest))
.contentType(org.springframework.http.MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name", is(clarinLicenseRest.getName())))
.andExpect(jsonPath("$.definition",
is(clarinLicenseRest.getDefinition())))
.andExpect(jsonPath("$.confirmation",
is(clarinLicenseRest.getConfirmation())))
.andExpect(jsonPath("$.requiredInfo",
is(clarinLicenseRest.getRequiredInfo())))
.andExpect(jsonPath("$.bitstreams",
is(clarinLicenseRest.getBitstreams())))
.andExpect(jsonPath("$.type",
is(ClarinLicenseRest.NAME)))

.andExpect(jsonPath("$.clarinLicenseLabel.label",
is(clarinLicenseRest.getClarinLicenseLabel().getLabel())))
.andExpect(jsonPath("$.clarinLicenseLabel.title",
is(clarinLicenseRest.getClarinLicenseLabel().getTitle())))
.andExpect(jsonPath("$.clarinLicenseLabel.extended",
is(clarinLicenseRest.getClarinLicenseLabel().isExtended())))
.andExpect(jsonPath("$.clarinLicenseLabel.type",
is(ClarinLicenseLabelRest.NAME)))

.andExpect(jsonPath("$.extendedClarinLicenseLabels[0].label",
is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).getLabel())))
.andExpect(jsonPath("$.extendedClarinLicenseLabels[0].title",
is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).getTitle())))
.andExpect(jsonPath("$.extendedClarinLicenseLabels[0].extended",
is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).isExtended())))
.andExpect(jsonPath("$.extendedClarinLicenseLabels[0].type",
is(ClarinLicenseLabelRest.NAME)))
.andDo(result -> idRef.set(read(result.getResponse().getContentAsString(),
"$.id")));
} finally {
if (Objects.nonNull(idRef.get())) {
// remove created clarin license
ClarinLicenseBuilder.deleteClarinLicense(idRef.get());
}
}

List<ClarinUserRegistration> userRegistrations = clarinUserRegistrationService.findByEPersonUUID(context,
context.getCurrentUser().getID());

Assert.assertFalse(CollectionUtils.isEmpty(userRegistrations));
// Remove the user registration record - it was created in the `getUserRegistration` method.
ClarinUserMetadataBuilder.deleteClarinUserMetadata(userRegistrations.get(0).getID());
}

private ClarinLicenseLabel getNonExtendedLicenseLabel(List<ClarinLicenseLabel> clarinLicenseLabelList) {
for (ClarinLicenseLabel clarinLicenseLabel : clarinLicenseLabelList) {
if (clarinLicenseLabel.isExtended()) {
Expand Down

0 comments on commit 11ee9e3

Please sign in to comment.