Skip to content

Commit bdc6af5

Browse files
committed
Apply security to query method
1 parent fc4161a commit bdc6af5

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

project-management/src/main/java/life/qbic/projectmanagement/application/api/AsyncProjectServiceImpl.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,25 @@ public Mono<ProjectCreationResponse> create(ProjectCreationRequest request)
8484
@Override
8585
public Flux<SamplePreview> getSamplePreviews(String projectId, String experimentId, int offset,
8686
int limit, List<SortOrder> sortOrders, String filter) {
87-
return Flux.defer(() -> {
88-
try {
89-
return Flux.fromIterable(
90-
sampleInfoService.queryPreview(ExperimentId.parse(experimentId), offset, limit,
91-
sortOrders, filter));
92-
} catch (Exception e) {
93-
log.error("Error getting sample previews", e);
94-
return Flux.error(new RequestFailedException("Error getting sample previews"));
95-
}
96-
}).subscribeOn(scheduler);
87+
SecurityContext securityContext = SecurityContextHolder.getContext();
88+
return applySecurityContextMany(Flux.defer(() ->
89+
fetchSamplePreviews(projectId, experimentId, offset, limit, sortOrders, filter)))
90+
.subscribeOn(scheduler)
91+
.transform(original -> writeSecurityContextMany(original, securityContext))
92+
.retryWhen(defaultRetryStrategy());
93+
}
94+
95+
private Flux<SamplePreview> fetchSamplePreviews(String projectId, String experimentId, int offset,
96+
int limit, List<SortOrder> sortOrders, String filter) {
97+
try {
98+
return Flux.fromIterable(
99+
sampleInfoService.queryPreview(ProjectId.parse(projectId),
100+
ExperimentId.parse(experimentId), offset, limit,
101+
sortOrders, filter));
102+
} catch (Exception e) {
103+
log.error("Error getting sample previews", e);
104+
return Flux.error(new RequestFailedException("Error getting sample previews"));
105+
}
97106
}
98107

99108
@Override

project-management/src/main/java/life/qbic/projectmanagement/application/sample/SampleInformationService.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,19 @@ public Collection<Sample> retrieveSamplesForExperiment(ProjectId projectId, Stri
7979
return sampleRepository.findSamplesByExperimentId(ExperimentId.parse(experimentId));
8080
}
8181

82+
/**
83+
* @deprecated Use {@link #retrieveSamplesByIds(ProjectId, Collection)} instead.
84+
*/
85+
@Deprecated(since = "1.10.0", forRemoval = true)
8286
public List<Sample> retrieveSamplesByIds(Collection<SampleId> sampleIds) {
8387
return sampleRepository.findSamplesBySampleId(sampleIds.stream().toList());
8488
}
8589

90+
@PreAuthorize("hasPermission(#projectId, 'life.qbic.projectmanagement.domain.model.project.Project', 'READ')")
91+
public List<Sample> retrieveSamplesByIds(ProjectId projectId, Collection<SampleId> sampleIds) {
92+
return sampleRepository.findSamplesBySampleId(sampleIds.stream().toList());
93+
}
94+
8695
@PreAuthorize("hasPermission(#projectId, 'life.qbic.projectmanagement.domain.model.project.Project', 'READ')")
8796
public List<Sample> retrieveSampleForBatch(ProjectId projectId, String batchId) {
8897
return sampleRepository.findSamplesByBatchId(BatchId.parse(batchId));
@@ -106,7 +115,9 @@ public List<Sample> retrieveSamplesForBatch(BatchId batchId) {
106115
* @param sortOrders the sort orders to apply
107116
* @return the results in the provided range
108117
* @since 1.0.0
118+
* @deprecated Use {@link #queryPreview(ProjectId, ExperimentId, int, int, List, String)} instead.
109119
*/
120+
@Deprecated(since = "1.10.0", forRemoval = true)
110121
public List<SamplePreview> queryPreview(ExperimentId experimentId, int offset, int limit,
111122
List<SortOrder> sortOrders, String filter) {
112123
// returned by JPA -> UnmodifiableRandomAccessList
@@ -118,6 +129,40 @@ public List<SamplePreview> queryPreview(ExperimentId experimentId, int offset, i
118129
return new ArrayList<>(previewList);
119130
}
120131

132+
/**
133+
* Queries {@link SamplePreview}s with a provided offset and limit that supports pagination.
134+
* Applies the Spring Security context as well.
135+
*
136+
* @param projectId the project ID that contains the information (required to apply the security
137+
* context)
138+
* @param offset the offset for the search result to start
139+
* @param limit the maximum number of results that should be returned
140+
* @param sortOrders the sort orders to apply
141+
* @return the results in the provided range
142+
* @since 1.10.0
143+
*/
144+
@PreAuthorize("hasPermission(#projectId, 'life.qbic.projectmanagement.domain.model.project.Project', 'READ')")
145+
public List<SamplePreview> queryPreview(ProjectId projectId, ExperimentId experimentId,
146+
int offset, int limit,
147+
List<SortOrder> sortOrders, String filter) {
148+
// returned by JPA -> UnmodifiableRandomAccessList
149+
List<SamplePreview> previewList = samplePreviewLookup.queryByExperimentId(experimentId,
150+
offset,
151+
limit,
152+
sortOrders, filter);
153+
// the list must be modifiable for spring security to filter it
154+
return new ArrayList<>(previewList);
155+
}
156+
157+
@PreAuthorize("hasPermission(#projectId, 'life.qbic.projectmanagement.domain.model.project.Project', 'READ')")
158+
public Optional<Sample> findSample(ProjectId projectId, SampleId sampleId) {
159+
return sampleRepository.findSample(sampleId);
160+
}
161+
162+
/**
163+
* @deprecated Use {@link #findSample(ProjectId, SampleId)} instead.
164+
*/
165+
@Deprecated(since = "1.10.0", forRemoval = true)
121166
public Optional<Sample> findSample(SampleId sampleId) {
122167
return sampleRepository.findSample(sampleId);
123168
}

0 commit comments

Comments
 (0)