Skip to content

Commit

Permalink
some small improvements and leads how to solve the nested relational …
Browse files Browse the repository at this point in the history
…field access based filters
  • Loading branch information
martin-trajanovski committed Dec 13, 2024
1 parent 8432254 commit e98d0b4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
49 changes: 49 additions & 0 deletions src/datasets/datasets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,55 @@ export class DatasetsService {
if (fieldValue) {
fieldValue.$lookup.as = field;

// TODO: Should implement something similar like addAccessBasedFilters in the controller including the access based checks on each relational field
// For example if we have proposals included we should check something like:
/*
const ability = this.caslAbilityFactory.proposalInstanceAccess(user);
const canViewAny = ability.can(Action.ProposalReadAny, Proposal);
const canViewOwner = ability.can(Action.ProposalReadManyOwner, Proposal);
const canViewAccess = ability.can(
Action.ProposalReadManyAccess,
Proposal,
);
const canViewPublic = ability.can(
Action.ProposalReadManyPublic,
Proposal,
);
if (!canViewAny) {
if (canViewAccess) {
fieldValue.$lookup.pipeline = [
{
$match: {
$or: [
{ ownerGroup: { $in: user.currentGroups } },
{ accessGroups: { $in: user.currentGroups } },
{ sharedWith: { $in: [user.email] } },
{ isPublished: true },
],
},
},
];
} else if (canViewOwner) {
fieldValue.$lookup.pipeline = [
{
$match: {
ownerGroup: { $in: user.currentGroups }
}
},
];
} else if (canViewPublic) {
fieldValue.$lookup.pipeline = [
{
$match: {
isPublished: true
}
},
];
}
}
*/

pipeline.push(fieldValue);
}
});
Expand Down
19 changes: 13 additions & 6 deletions src/datasets/datasets.v4.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,9 @@ export class DatasetsV4Controller {
}

addAccessBasedFilters(
request: Request,
user: JWTUser,
filter: IDatasetFiltersV4<DatasetDocument, IDatasetFields>,
): IDatasetFiltersV4<DatasetDocument, IDatasetFields> {
const user: JWTUser = request.user as JWTUser;

const ability = this.caslAbilityFactory.datasetInstanceAccess(user);
const canViewAny = ability.can(Action.DatasetReadAny, DatasetClass);
const canViewOwner = ability.can(Action.DatasetReadManyOwner, DatasetClass);
Expand Down Expand Up @@ -391,7 +389,10 @@ export class DatasetsV4Controller {
queryFilter: string,
) {
const parsedFilter = JSON.parse(queryFilter ?? "{}");
const mergedFilters = this.addAccessBasedFilters(request, parsedFilter);
const mergedFilters = this.addAccessBasedFilters(
request.user as JWTUser,
parsedFilter,
);

const datasets = await this.datasetsService.findAllComplete(mergedFilters);

Expand Down Expand Up @@ -570,7 +571,10 @@ export class DatasetsV4Controller {
): Promise<OutputDatasetDto | null> {
const parsedFilter = JSON.parse(queryFilter ?? "{}");

const mergedFilters = this.addAccessBasedFilters(request, parsedFilter);
const mergedFilters = this.addAccessBasedFilters(
request.user as JWTUser,
parsedFilter,
);

const foundDataset =
await this.datasetsService.findOneComplete(mergedFilters);
Expand Down Expand Up @@ -628,7 +632,10 @@ export class DatasetsV4Controller {
) {
const parsedFilter = JSON.parse(queryFilter ?? "{}");

const finalFilters = this.addAccessBasedFilters(request, parsedFilter);
const finalFilters = this.addAccessBasedFilters(
request.user as JWTUser,
parsedFilter,
);

return this.datasetsService.count(finalFilters);
}
Expand Down

0 comments on commit e98d0b4

Please sign in to comment.