-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
335 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
apps/hcdc-access-service/src/domain/entity/permission-rule/entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import { MongoQuery } from '@casl/ability' | ||
|
||
import { AuthAction, AuthSubject, SubjectEntityMapping } from 'src/domain/auth' | ||
import { | ||
AUTH_ACTION_ALL, | ||
AUTH_SUBJECT_ALL, | ||
AuthAction, | ||
AuthSubject, | ||
SubjectEntityMapping, | ||
} from 'src/domain/auth' | ||
|
||
export type PermissionRule< | ||
TSubject extends keyof typeof AuthSubject = keyof typeof AuthSubject, | ||
> = { | ||
subject: TSubject | ||
action: (typeof AuthAction)[TSubject][number] | ||
subject: TSubject | typeof AUTH_SUBJECT_ALL | ||
action: (typeof AuthAction)[TSubject][number] | typeof AUTH_ACTION_ALL | ||
inverted?: boolean | ||
conditions?: MongoQuery<SubjectEntityMapping[TSubject]> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
apps/hcdc-access-service/src/domain/use-case/bio-product/authorize-populates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { AuthSubject, authorizePopulates } from 'src/domain/auth' | ||
import { BranchAction, BioProduct } from 'src/domain/entity' | ||
import { EEntityPopulatePathUnknown } from 'src/domain/exception' | ||
import { | ||
AuthContextToken, | ||
EntityFindOneOptions, | ||
IAuthContext, | ||
} from 'src/domain/interface' | ||
|
||
@Injectable() | ||
export class BioProductAuthorizePopulatesUseCase { | ||
constructor( | ||
@Inject(AuthContextToken) | ||
private readonly authContext: IAuthContext, | ||
) {} | ||
execute(input: EntityFindOneOptions<BioProduct>['populates']) { | ||
const { ability } = this.authContext.getData() | ||
|
||
return authorizePopulates(ability, input, (path) => { | ||
switch (path) { | ||
case 'branch': | ||
return { subject: AuthSubject.Branch, action: BranchAction.Read } | ||
default: | ||
throw new EEntityPopulatePathUnknown(path) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 17 additions & 4 deletions
21
apps/hcdc-access-service/src/domain/use-case/bio-product/validate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { BioProduct, EntityData } from 'src/domain/entity' | ||
import { BioProduct, BranchAction, EntityData } from 'src/domain/entity' | ||
import { BranchAssertExistsUseCase } from '../branch/assert-exists' | ||
import { AuthContextToken, IAuthContext } from 'src/domain/interface' | ||
import { AuthSubject, assertPermission } from 'src/domain/auth' | ||
|
||
@Injectable() | ||
export class BioProductValidateUseCase { | ||
constructor( | ||
@Inject(AuthContextToken) | ||
private readonly authContext: IAuthContext, | ||
private readonly branchAssertExistsUseCase: BranchAssertExistsUseCase, | ||
) {} | ||
|
||
async execute(input: Partial<Pick<EntityData<BioProduct>, 'branchId'>>) { | ||
async execute(input: Partial<EntityData<BioProduct>>) { | ||
const { ability } = this.authContext.getData() | ||
const { branchId } = input | ||
|
||
if (branchId !== undefined) { | ||
await this.branchAssertExistsUseCase.execute({ _id: branchId }) | ||
const branch = await this.branchAssertExistsUseCase.execute({ | ||
_id: branchId, | ||
}) | ||
assertPermission( | ||
ability, | ||
AuthSubject.Branch, | ||
BranchAction.AssignToSubject, | ||
branch, | ||
) | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
apps/hcdc-access-service/src/domain/use-case/branch/authorize-populates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Inject, Injectable } from '@nestjs/common' | ||
|
||
import { AuthSubject, authorizePopulates } from 'src/domain/auth' | ||
import { BranchAction, Branch } from 'src/domain/entity' | ||
import { EEntityPopulatePathUnknown } from 'src/domain/exception' | ||
import { | ||
AuthContextToken, | ||
EntityFindOneOptions, | ||
IAuthContext, | ||
} from 'src/domain/interface' | ||
|
||
@Injectable() | ||
export class BranchAuthorizePopulatesUseCase { | ||
constructor( | ||
@Inject(AuthContextToken) | ||
private readonly authContext: IAuthContext, | ||
) {} | ||
execute(input: EntityFindOneOptions<Branch>['populates']) { | ||
const { ability } = this.authContext.getData() | ||
|
||
return authorizePopulates(ability, input, (path) => { | ||
switch (path) { | ||
case 'sampleOrigins': | ||
return { subject: AuthSubject.Branch, action: BranchAction.Read } | ||
default: | ||
throw new EEntityPopulatePathUnknown(path) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.