Skip to content

Commit

Permalink
feat: add public endpoints for better separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-trajanovski committed Dec 16, 2024
1 parent 8bad5cb commit caf3d1a
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 43 deletions.
23 changes: 20 additions & 3 deletions src/casl/casl-ability.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,34 @@ export class CaslAbilityFactory {
/* unauthenticated users
**/

can(Action.DatasetReadManyPublic, DatasetClass);
can(Action.DatasetReadOnePublic, DatasetClass, {
isPublished: true,
});
// -
can(Action.DatasetAttachmentReadPublic, DatasetClass, {
isPublished: true,
});
// -
can(Action.DatasetOrigdatablockReadPublic, DatasetClass, {
isPublished: true,
});
// -
can(Action.DatasetDatablockReadPublic, DatasetClass, {
isPublished: true,
});

cannot(Action.DatasetCreate, DatasetClass);
can(Action.DatasetRead, DatasetClass);
cannot(Action.DatasetRead, DatasetClass);
cannot(Action.DatasetUpdate, DatasetClass);
// -
cannot(Action.DatasetAttachmentCreate, DatasetClass);
can(Action.DatasetAttachmentRead, DatasetClass);
cannot(Action.DatasetAttachmentRead, DatasetClass);
cannot(Action.DatasetAttachmentUpdate, DatasetClass);
cannot(Action.DatasetAttachmentDelete, DatasetClass);
// -
cannot(Action.DatasetOrigdatablockCreate, DatasetClass);
can(Action.DatasetOrigdatablockRead, DatasetClass);
cannot(Action.DatasetOrigdatablockRead, DatasetClass);
cannot(Action.DatasetOrigdatablockUpdate, DatasetClass);
// -
cannot(Action.DatasetDatablockCreate, DatasetClass);
Expand Down
56 changes: 40 additions & 16 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,11 @@ export class DatasetsController {

// GET /datasets
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadManyPublic, DatasetClass),
)
@UseInterceptors(MainDatasetsPublicInterceptor)
@Get()
Expand Down Expand Up @@ -946,8 +949,11 @@ export class DatasetsController {

// GET /datasets/fullquery
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadManyPublic, DatasetClass),
)
@UseInterceptors(SubDatasetsPublicInterceptor, FullQueryInterceptor)
@Get("/fullquery")
Expand Down Expand Up @@ -1009,8 +1015,11 @@ export class DatasetsController {

// GET /fullfacets
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadManyPublic, DatasetClass),
)
@UseInterceptors(SubDatasetsPublicInterceptor)
@Get("/fullfacet")
Expand Down Expand Up @@ -1067,8 +1076,11 @@ export class DatasetsController {

// GET /datasets/metadataKeys
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadManyPublic, DatasetClass),
)
@UseInterceptors(SubDatasetsPublicInterceptor)
@Get("/metadataKeys")
Expand Down Expand Up @@ -1146,8 +1158,11 @@ export class DatasetsController {

// GET /datasets/findOne
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadOnePublic, DatasetClass),
)
@Get("/findOne")
@ApiOperation({
Expand Down Expand Up @@ -1221,8 +1236,11 @@ export class DatasetsController {

// GET /datasets/count
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadManyPublic, DatasetClass),
)
@Get("/count")
@ApiOperation({
Expand Down Expand Up @@ -1262,8 +1280,11 @@ export class DatasetsController {
// GET /datasets/:id
//@UseGuards(PoliciesGuard)
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadOnePublic, DatasetClass),
)
@Get("/:pid")
@ApiParam({
Expand Down Expand Up @@ -1628,8 +1649,11 @@ export class DatasetsController {

// GET /datasets/:id/thumbnail
@UseGuards(PoliciesGuard)
@CheckPolicies("datasets", (ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass),
@CheckPolicies(
"datasets",
(ability: AppAbility) =>
ability.can(Action.DatasetRead, DatasetClass) ||
ability.can(Action.DatasetReadOnePublic, DatasetClass),
)
// @UseGuards(PoliciesGuard)
@Get("/:pid/thumbnail")
Expand Down
Loading

0 comments on commit caf3d1a

Please sign in to comment.