Skip to content

Commit

Permalink
Merge pull request #59 from DiSSCo/feature/use-agent-token
Browse files Browse the repository at this point in the history
use agent instead of user id
  • Loading branch information
southeo authored Oct 8, 2024
2 parents f8a1803 + ca32667 commit e26ed7d
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public ResponseEntity<JsonApiWrapper> createDataMapping(Authentication authentic
@RequestBody JsonApiRequestWrapper requestBody, HttpServletRequest servletRequest)
throws JsonProcessingException, ProcessingFailedException, ForbiddenException {
var dataMapping = getDataMappingRequestFromRequest(requestBody);
var userId = getAgent(authentication).getId();
log.info("Received create request for data mapping: {} from user: {}", dataMapping, userId);
var user = getAgent(authentication);
log.info("Received create request for data mapping: {} from user: {}", dataMapping, user.getId());
String path = appProperties.getBaseUrl() + servletRequest.getRequestURI();
var result = service.createDataMapping(dataMapping, userId, path);
var result = service.createDataMapping(dataMapping, user, path);
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}

Expand All @@ -61,10 +61,10 @@ public ResponseEntity<JsonApiWrapper> updateDataMapping(Authentication authentic
throws JsonProcessingException, NotFoundException, ProcessingFailedException, ForbiddenException {
var dataMapping = getDataMappingRequestFromRequest(requestBody);
var id = prefix + '/' + suffix;
var userId = getAgent(authentication).getId();
log.info("Received update request for data mapping: {} from user: {}", dataMapping, userId);
var user = getAgent(authentication);
log.info("Received update request for data mapping: {} from user: {}", dataMapping, user.getId());
String path = appProperties.getBaseUrl() + servletRequest.getRequestURI();
var result = service.updateDataMapping(id, dataMapping, userId, path);
var result = service.updateDataMapping(id, dataMapping, user, path);
if (result == null) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public ResponseEntity<JsonApiWrapper> createMachineAnnotationService(
@RequestBody JsonApiRequestWrapper requestBody, HttpServletRequest servletRequest)
throws JsonProcessingException, ProcessingFailedException, ForbiddenException {
var machineAnnotationService = getMachineAnnotation(requestBody);
var userId = getAgent(authentication).getId();
var user = getAgent(authentication);
log.info("Received create request for machine annotation service: {} from user: {}",
machineAnnotationService, userId);
machineAnnotationService, user.getId());
String path = appProperties.getBaseUrl() + servletRequest.getRequestURI();
var result = service.createMachineAnnotationService(machineAnnotationService, userId, path);
var result = service.createMachineAnnotationService(machineAnnotationService, user, path);
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}

Expand All @@ -63,12 +63,12 @@ public ResponseEntity<JsonApiWrapper> updateMachineAnnotationService(
@RequestBody JsonApiRequestWrapper requestBody, HttpServletRequest servletRequest)
throws JsonProcessingException, NotFoundException, ProcessingFailedException, ForbiddenException {
var machineAnnotationService = getMachineAnnotation(requestBody);
var userId = getAgent(authentication).getId();
var user = getAgent(authentication);
var id = prefix + '/' + suffix;
log.info("Received update request for machine annotation service: {} from user: {}", id,
userId);
user.getId());
String path = appProperties.getBaseUrl() + servletRequest.getRequestURI();
var result = service.updateMachineAnnotationService(id, machineAnnotationService, userId, path);
var result = service.updateMachineAnnotationService(id, machineAnnotationService, user, path);
if (result == null) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public ResponseEntity<JsonApiWrapper> createSourceSystem(Authentication authenti
throws IOException, NotFoundException, ProcessingFailedException, ForbiddenException {
var sourceSystemRequest = getSourceSystemFromRequest(requestBody);
String path = appProperties.getBaseUrl() + servletRequest.getRequestURI();
var userId = getAgent(authentication).getId();
var user = getAgent(authentication);
log.info("Received create request for source system: {} from user: {}", sourceSystemRequest,
userId);
var result = service.createSourceSystem(sourceSystemRequest, userId, path);
user.getId());
var result = service.createSourceSystem(sourceSystemRequest, user, path);
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}

Expand All @@ -64,10 +64,10 @@ public ResponseEntity<JsonApiWrapper> updateSourceSystem(Authentication authenti
throws IOException, NotFoundException, ProcessingFailedException, ForbiddenException {
var sourceSystemRequest = getSourceSystemFromRequest(requestBody);
var id = prefix + '/' + suffix;
var userId = getAgent(authentication).getId();
log.info("Received update request for source system: {} from user: {}", id, userId);
var user = getAgent(authentication);
log.info("Received update request for source system: {} from user: {}", id, user.getId());
String path = appProperties.getBaseUrl() + servletRequest.getRequestURI();
var result = service.updateSourceSystem(id, sourceSystemRequest, userId, path, trigger);
var result = service.updateSourceSystem(id, sourceSystemRequest, user, path, trigger);
if (result == null) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import eu.dissco.orchestration.backend.properties.FdoProperties;
import eu.dissco.orchestration.backend.repository.DataMappingRepository;
import eu.dissco.orchestration.backend.schema.Agent;
import eu.dissco.orchestration.backend.schema.Agent.Type;
import eu.dissco.orchestration.backend.schema.DataMapping;
import eu.dissco.orchestration.backend.schema.DataMapping.OdsMappingDataStandard;
import eu.dissco.orchestration.backend.schema.DataMapping.OdsStatus;
Expand Down Expand Up @@ -87,7 +86,7 @@ private static List<FieldMapping> buildFieldMapping(
return mappedList;
}

public JsonApiWrapper createDataMapping(DataMappingRequest mappingRequest, String userId,
public JsonApiWrapper createDataMapping(DataMappingRequest mappingRequest, Agent user,
String path)
throws ProcessingFailedException {
var requestBody = fdoRecordService.buildCreateRequest(mappingRequest, ObjectType.DATA_MAPPING);
Expand All @@ -97,15 +96,15 @@ public JsonApiWrapper createDataMapping(DataMappingRequest mappingRequest, Strin
} catch (PidException e) {
throw new ProcessingFailedException(e.getMessage(), e);
}
var dataMapping = buildDataMapping(mappingRequest, 1, userId, handle,
var dataMapping = buildDataMapping(mappingRequest, 1, user, handle,
Date.from(Instant.now()));
repository.createDataMapping(dataMapping);
publishCreateEvent(dataMapping);
return wrapSingleResponse(dataMapping, path);
}

private DataMapping buildDataMapping(DataMappingRequest dataMappingRequest, int version,
String userId, String handle, Date created) {
Agent user, String handle, Date created) {
var id = HANDLE_PROXY + handle;
return new DataMapping()
.withId(id)
Expand All @@ -118,7 +117,7 @@ private DataMapping buildDataMapping(DataMappingRequest dataMappingRequest, int
.withSchemaDescription(dataMappingRequest.getSchemaDescription())
.withSchemaDateCreated(created)
.withSchemaDateModified(Date.from(Instant.now()))
.withSchemaCreator(new Agent().withId(userId).withType(Type.SCHEMA_PERSON))
.withSchemaCreator(user)
.withOdsDefaultMapping(buildDefaultMapping(dataMappingRequest))
.withOdsFieldMapping(buildFieldMapping(dataMappingRequest))
.withOdsMappingDataStandard(OdsMappingDataStandard.fromValue(
Expand Down Expand Up @@ -149,15 +148,15 @@ private void rollbackMappingCreation(DataMapping dataMapping) {
}

public JsonApiWrapper updateDataMapping(String id, DataMappingRequest dataMappingRequest,
String userId, String path) throws NotFoundException, ProcessingFailedException {
Agent user, String path) throws NotFoundException, ProcessingFailedException {
var currentDataMappingOptional = repository.getActiveDataMapping(id);
if (currentDataMappingOptional.isEmpty()) {
throw new NotFoundException("Requested data mapping does not exist");
}
var currentDataMapping = currentDataMappingOptional.get();
var dataMapping = buildDataMapping(dataMappingRequest,
currentDataMapping.getSchemaVersion() + 1,
userId, id, currentDataMapping.getSchemaDateCreated());
user, id, currentDataMapping.getSchemaDateCreated());
if (isEqual(dataMapping, currentDataMapping)) {
return null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import eu.dissco.orchestration.backend.properties.MachineAnnotationServiceProperties;
import eu.dissco.orchestration.backend.repository.MachineAnnotationServiceRepository;
import eu.dissco.orchestration.backend.schema.Agent;
import eu.dissco.orchestration.backend.schema.Agent.Type;
import eu.dissco.orchestration.backend.schema.MachineAnnotationService;
import eu.dissco.orchestration.backend.schema.MachineAnnotationService.OdsStatus;
import eu.dissco.orchestration.backend.schema.MachineAnnotationServiceRequest;
Expand Down Expand Up @@ -95,13 +94,12 @@ private static SchemaContactPoint__1 buildContactPoint(SchemaContactPoint schema

public JsonApiWrapper createMachineAnnotationService(
MachineAnnotationServiceRequest masRequest,
String userId,
String path) throws ProcessingFailedException {
Agent user, String path) throws ProcessingFailedException {
var requestBody = fdoRecordService.buildCreateRequest(masRequest, ObjectType.MAS);
try {
var handle = handleComponent.postHandle(requestBody);
setDefaultMas(masRequest, handle);
var mas = buildMachineAnnotationService(masRequest, 1, userId, handle,
var mas = buildMachineAnnotationService(masRequest, 1, user, handle,
Instant.now());
repository.createMachineAnnotationService(mas);
createDeployment(mas);
Expand All @@ -113,7 +111,7 @@ public JsonApiWrapper createMachineAnnotationService(
}

private MachineAnnotationService buildMachineAnnotationService(
MachineAnnotationServiceRequest mas, int version, String userId, String handle,
MachineAnnotationServiceRequest mas, int version, Agent user, String handle,
Instant created) {
var id = HANDLE_PROXY + handle;
return new MachineAnnotationService()
Expand All @@ -127,7 +125,7 @@ private MachineAnnotationService buildMachineAnnotationService(
.withSchemaDescription(mas.getSchemaDescription())
.withSchemaDateCreated(Date.from(created))
.withSchemaDateModified(Date.from(Instant.now()))
.withSchemaCreator(new Agent().withId(userId).withType(Type.SCHEMA_PERSON))
.withSchemaCreator(user)
.withOdsContainerTag(mas.getOdsContainerTag())
.withOdsContainerImage(mas.getOdsContainerImage())
.withOdsTargetDigitalObjectFilter(buildTargetFilters(mas.getOdsTargetDigitalObjectFilter()))
Expand Down Expand Up @@ -356,14 +354,14 @@ private void rollbackMasCreation(MachineAnnotationService mas,
}

public JsonApiWrapper updateMachineAnnotationService(String id,
MachineAnnotationServiceRequest masRequest, String userId, String path)
MachineAnnotationServiceRequest masRequest, Agent user, String path)
throws NotFoundException, ProcessingFailedException {
var currentMasOptional = repository.getActiveMachineAnnotationService(id);
if (currentMasOptional.isPresent()) {
var currentMas = currentMasOptional.get();
setDefaultMas(masRequest, id);
var machineAnnotationService = buildMachineAnnotationService(masRequest,
currentMas.getSchemaVersion() + 1, userId, id, Instant.now());
currentMas.getSchemaVersion() + 1, user, id, Instant.now());
if (isEqual(machineAnnotationService, currentMas)) {
log.debug("No changes found for MAS");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import eu.dissco.orchestration.backend.properties.TranslatorJobProperties;
import eu.dissco.orchestration.backend.repository.SourceSystemRepository;
import eu.dissco.orchestration.backend.schema.Agent;
import eu.dissco.orchestration.backend.schema.Agent.Type;
import eu.dissco.orchestration.backend.schema.SourceSystem;
import eu.dissco.orchestration.backend.schema.SourceSystem.OdsStatus;
import eu.dissco.orchestration.backend.schema.SourceSystem.OdsTranslatorType;
Expand Down Expand Up @@ -137,7 +136,7 @@ private void updateCronsToImageTag() throws ApiException {
}

private SourceSystem buildSourceSystem(
SourceSystemRequest sourceSystemRequest, int version, String userId, String handle,
SourceSystemRequest sourceSystemRequest, int version, Agent user, String handle,
Date created) {
var id = HANDLE_PROXY + handle;
return new SourceSystem()
Expand All @@ -151,9 +150,7 @@ private SourceSystem buildSourceSystem(
.withSchemaDescription(sourceSystemRequest.getSchemaDescription())
.withSchemaDateCreated(created)
.withSchemaDateModified(Date.from(Instant.now()))
.withSchemaCreator(new Agent()
.withType(Type.SCHEMA_PERSON)
.withId(userId))
.withSchemaCreator(user)
.withSchemaUrl(sourceSystemRequest.getSchemaUrl())
.withOdsDataMappingID(sourceSystemRequest.getOdsDataMappingID())
.withOdsTranslatorType(
Expand All @@ -162,12 +159,12 @@ private SourceSystem buildSourceSystem(
.withLtcCollectionManagementSystem(sourceSystemRequest.getLtcCollectionManagementSystem());
}

public JsonApiWrapper createSourceSystem(SourceSystemRequest sourceSystemRequest, String userId,
public JsonApiWrapper createSourceSystem(SourceSystemRequest sourceSystemRequest, Agent user,
String path)
throws NotFoundException, ProcessingFailedException {
validateMappingExists(sourceSystemRequest.getOdsDataMappingID());
String handle = createHandle(sourceSystemRequest);
var sourceSystem = buildSourceSystem(sourceSystemRequest, 1, userId, handle,
var sourceSystem = buildSourceSystem(sourceSystemRequest, 1, user, handle,
Date.from(Instant.now()));
repository.createSourceSystem(sourceSystem);
createCronJob(sourceSystem);
Expand Down Expand Up @@ -270,7 +267,7 @@ private void validateMappingExists(String mappingId) throws NotFoundException {
}

public JsonApiWrapper updateSourceSystem(String id, SourceSystemRequest sourceSystemRequest,
String userId, String path, boolean trigger)
Agent user, String path, boolean trigger)
throws NotFoundException, ProcessingFailedException {
var currentSourceSystemOptional = repository.getActiveSourceSystem(id);
if (currentSourceSystemOptional.isEmpty()) {
Expand All @@ -279,7 +276,7 @@ public JsonApiWrapper updateSourceSystem(String id, SourceSystemRequest sourceSy
}
var currentSourceSystem = currentSourceSystemOptional.get();
var sourceSystem = buildSourceSystem(sourceSystemRequest,
currentSourceSystem.getSchemaVersion() + 1, userId, id,
currentSourceSystem.getSchemaVersion() + 1, user, id,
currentSourceSystem.getSchemaDateCreated());
if (isEquals(sourceSystem, currentSourceSystem)) {
log.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public static Agent getAgent(Authentication authentication) throws ForbiddenExce
}
fullName.append(claims.get("family_name"));
}
var nameString = fullName.toString().isEmpty() ? null : fullName.toString();
return new Agent()
.withType(Type.SCHEMA_PERSON)
.withSchemaName(fullName.toString())
.withSchemaName(nameString)
.withId((String) claims.get("orcid"));
} else {
log.error("Missing ORCID in token");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import static eu.dissco.orchestration.backend.testutils.TestUtils.MAPPER;
import static eu.dissco.orchestration.backend.testutils.TestUtils.MAPPING_PATH;
import static eu.dissco.orchestration.backend.testutils.TestUtils.MAPPING_URI;
import static eu.dissco.orchestration.backend.testutils.TestUtils.OBJECT_CREATOR;
import static eu.dissco.orchestration.backend.testutils.TestUtils.PREFIX;
import static eu.dissco.orchestration.backend.testutils.TestUtils.SUFFIX;
import static eu.dissco.orchestration.backend.testutils.TestUtils.givenAgent;
import static eu.dissco.orchestration.backend.testutils.TestUtils.givenClaims;
import static eu.dissco.orchestration.backend.testutils.TestUtils.givenDataMapping;
import static eu.dissco.orchestration.backend.testutils.TestUtils.givenDataMappingRequest;
Expand Down Expand Up @@ -83,7 +83,7 @@ void testUpdateDataMapping() throws Exception {
// Given
givenAuthentication();
var requestBody = givenDataMappingRequestJson();
given(service.updateDataMapping(BARE_HANDLE, givenDataMappingRequest(), OBJECT_CREATOR,
given(service.updateDataMapping(BARE_HANDLE, givenDataMappingRequest(), givenAgent(),
"null/data-mapping")).willReturn(
givenDataMappingSingleJsonApiWrapper());

Expand Down
Loading

0 comments on commit e26ed7d

Please sign in to comment.