Skip to content

Commit cfb43b6

Browse files
committed
Verify data file owner when creating/updating corpus
1 parent 151e8c0 commit cfb43b6

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/Serval/src/Serval.DataFiles/Features/Corpora/CreateCorpus.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@ public async Task<CreateCorpusResponse> HandleAsync(CreateCorpus request, Cancel
3030
Owner = request.Owner,
3131
Name = request.CorpusConfig.Name,
3232
Language = request.CorpusConfig.Language,
33-
Files = await MapFilesAsync(request.CorpusConfig.Files, cancellationToken),
33+
Files = await MapFilesAsync(request.Owner, request.CorpusConfig.Files, cancellationToken),
3434
};
3535
await corpora.InsertAsync(corpus, cancellationToken);
3636
return new(mapper.Map(corpus));
3737
}
3838

3939
private async Task<IReadOnlyList<CorpusFile>> MapFilesAsync(
40+
string owner,
4041
IReadOnlyList<CorpusFileConfigDto> files,
4142
CancellationToken cancellationToken
4243
)
4344
{
4445
var corpusFiles = new List<CorpusFile>();
4546
foreach (CorpusFileConfigDto file in files)
4647
{
47-
DataFile? dataFile = await dataFiles.GetAsync(file.FileId, cancellationToken);
48+
DataFile? dataFile = await dataFiles.GetAsync(
49+
e => e.Id == file.FileId && e.Owner == owner,
50+
cancellationToken
51+
);
4852
if (dataFile is null)
4953
throw new EntityNotFoundException($"Could not find the DataFile '{file.FileId}'.");
5054
corpusFiles.Add(new CorpusFile { FileRef = file.FileId, TextId = file.TextId });

src/Serval/src/Serval.DataFiles/Features/Corpora/UpdateCorpus.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public async Task<UpdateCorpusResponse> HandleAsync(UpdateCorpus request, Cancel
1717
{
1818
await corpora.CheckOwnerAsync(request.CorpusId, request.Owner, cancellationToken);
1919

20-
IReadOnlyList<CorpusFile> files = await MapFilesAsync(request.Files, cancellationToken);
20+
IReadOnlyList<CorpusFile> files = await MapFilesAsync(request.Owner, request.Files, cancellationToken);
2121

2222
Corpus corpus = await dataAccessContext.WithTransactionAsync(
2323
async ct =>
@@ -29,7 +29,7 @@ public async Task<UpdateCorpusResponse> HandleAsync(UpdateCorpus request, Cancel
2929
);
3030
if (updated is null)
3131
throw new EntityNotFoundException($"Could not find the Corpus '{request.CorpusId}'.");
32-
HashSet<string> corpusFileIds = updated.Files.Select(f => f.FileRef).ToHashSet();
32+
HashSet<string> corpusFileIds = [.. updated.Files.Select(f => f.FileRef)];
3333
IDictionary<string, DataFile> corpusDataFilesDict = (
3434
await dataFiles.GetAllAsync(f => corpusFileIds.Contains(f.Id), ct)
3535
).ToDictionary(f => f.Id);
@@ -54,14 +54,18 @@ await eventRouter.PublishAsync(
5454
}
5555

5656
private async Task<IReadOnlyList<CorpusFile>> MapFilesAsync(
57+
string owner,
5758
IReadOnlyList<CorpusFileConfigDto> files,
5859
CancellationToken cancellationToken
5960
)
6061
{
6162
var corpusFiles = new List<CorpusFile>();
6263
foreach (CorpusFileConfigDto file in files)
6364
{
64-
DataFile? dataFile = await dataFiles.GetAsync(file.FileId, cancellationToken);
65+
DataFile? dataFile = await dataFiles.GetAsync(
66+
e => e.Id == file.FileId && e.Owner == owner,
67+
cancellationToken
68+
);
6569
if (dataFile is null)
6670
throw new EntityNotFoundException($"Could not find the DataFile '{file.FileId}'.");
6771
corpusFiles.Add(new CorpusFile { FileRef = file.FileId, TextId = file.TextId });

0 commit comments

Comments
 (0)