Skip to content

Commit

Permalink
extract domain auth + entity
Browse files Browse the repository at this point in the history
  • Loading branch information
wermarter committed Feb 15, 2024
1 parent 4bf5b7d commit 910d96f
Show file tree
Hide file tree
Showing 102 changed files with 309 additions and 378 deletions.
5 changes: 2 additions & 3 deletions apps/hcdc-access-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"lint": "tsc && rimraf dist"
},
"dependencies": {
"@casl/ability": "^6.5.0",
"@casl/mongoose": "^7.2.2",
"@diut/common": "workspace:*",
"@diut/hcdc": "workspace:*",
"@diut/nestjs-infra": "workspace:*",
"@diut/services": "workspace:*",
"@casl/ability": "^6.5.0",
"@casl/mongoose": "^7.2.2",
"@nestjs/cli": "^10.3.2",
"@nestjs/common": "^10.3.3",
"@nestjs/config": "^3.2.0",
Expand All @@ -28,7 +28,6 @@
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.3",
"@nestjs/swagger": "^7.3.0",
"@ucast/mongo2js": "^1.3.4",
"argon2": "^0.31.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down
22 changes: 22 additions & 0 deletions apps/hcdc-access-service/src/domain/auth/setup-subject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import '@casl/mongoose'

declare module '@casl/mongoose' {
interface RecordTypes {
BioProduct: true
Branch: true
Diagnosis: true
Doctor: true
Instrument: true
Patient: true
PatientType: true
PrintForm: true
Role: true
Sample: true
SampleType: true
Test: true
TestCategory: true
TestCombo: true
TestElement: true
User: true
}
}
40 changes: 9 additions & 31 deletions apps/hcdc-access-service/src/domain/auth/utils.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
import {
MongoAbility,
subject as assignSubject,
buildMongoQueryMatcher,
createMongoAbility,
} from '@casl/ability'
import { $or, or } from '@ucast/mongo2js'
import { MongoAbility } from '@casl/ability'
import type { FilterQuery } from 'mongoose'
import { accessibleBy } from '@casl/mongoose'
import type { PopulatePath } from '@diut/nestjs-infra'

import { AuthAction, AuthActionUnionType } from './action'
import {
AUTH_ACTION_ALL,
AUTH_SUBJECT_ALL,
AuthAction,
AuthActionUnionType,
AuthSubject,
AuthSubjectUnionType,
BaseEntity,
SubjectEntityMapping,
} from './subject'
checkPermission,
} from '@diut/hcdc'

import { EAuthzPermissionDenied } from 'src/domain/exception'
import { AUTH_ACTION_ALL, AUTH_SUBJECT_ALL } from './constants'
import { BaseEntity, PermissionRule } from '../entity'
import { EntityFindOneOptions } from '../interface'

const conditionsMatcher = buildMongoQueryMatcher({ $or }, { or })

export function createAbility(permissionRules: PermissionRule[]) {
return createMongoAbility(permissionRules, { conditionsMatcher })
}

export function checkPermission<TSubject extends keyof typeof AuthSubject>(
ability: MongoAbility,
subject: TSubject | typeof AUTH_SUBJECT_ALL,
action: (typeof AuthAction)[TSubject][number] | typeof AUTH_ACTION_ALL,
object?: Partial<SubjectEntityMapping[TSubject]> | null,
) {
if (object != undefined) {
return ability.can(action, assignSubject(subject, object))
}

return ability.can(action, subject)
}

export function assertPermission<TSubject extends keyof typeof AuthSubject>(
ability: MongoAbility,
subject: TSubject | typeof AUTH_SUBJECT_ALL,
Expand Down
13 changes: 2 additions & 11 deletions apps/hcdc-access-service/src/domain/entity/base-entity.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { BaseEntity } from '@diut/hcdc'
import { ApiPropertyOptions } from '@nestjs/swagger'
import { omit } from 'lodash'

export type BaseEntity = {
_id: string

createdAt: Date
updatedAt: Date

isDeleted: boolean
deletedAt?: Date
}

export const exampleBaseEntity = {
_id: exampleMongoObjectId,
createdAt: {
Expand Down
14 changes: 0 additions & 14 deletions apps/hcdc-access-service/src/domain/entity/bio-product/auth.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { BioProduct } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { BioProduct } from './entity'

export const exampleBioProduct = {
displayIndex: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
14 changes: 0 additions & 14 deletions apps/hcdc-access-service/src/domain/entity/branch/auth.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/branch/example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectIds } from '@diut/nestjs-infra'
import { exampleMongoObjectIds } from '@diut/common'
import { Branch, BranchType } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { Branch, BranchType } from './entity'

export const exampleBranch = {
displayIndex: {
Expand Down
2 changes: 0 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/branch/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
14 changes: 0 additions & 14 deletions apps/hcdc-access-service/src/domain/entity/diagnosis/auth.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { Diagnosis } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { Diagnosis } from './entity'

export const exampleDiagnosis = {
displayIndex: {
Expand Down
2 changes: 0 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/diagnosis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
14 changes: 0 additions & 14 deletions apps/hcdc-access-service/src/domain/entity/doctor/auth.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/doctor/example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { Doctor } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { Doctor } from './entity'

export const exampleDoctor = {
displayIndex: {
Expand Down
2 changes: 0 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/doctor/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
14 changes: 0 additions & 14 deletions apps/hcdc-access-service/src/domain/entity/instrument/auth.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { Instrument } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { Instrument } from './entity'

export const exampleInstrument = {
displayIndex: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
14 changes: 0 additions & 14 deletions apps/hcdc-access-service/src/domain/entity/patient-type/auth.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { PatientType } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { PatientType } from './entity'

export const examplePatientType = {
displayIndex: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
14 changes: 0 additions & 14 deletions apps/hcdc-access-service/src/domain/entity/patient/auth.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/patient/example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { Patient, PatientGender } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { Patient, PatientGender } from './entity'

export const examplePatient = {
externalId: {
Expand Down
2 changes: 0 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/patient/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PermissionRule } from '@diut/hcdc'

import { AuthActionValues, AuthSubjectValues } from 'src/domain/auth'
import { EntityDataExample } from '../base-entity'
import { PermissionRule } from './entity'

export const examplePermissionRule = {
subject: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './entity'
export * from './example'
15 changes: 0 additions & 15 deletions apps/hcdc-access-service/src/domain/entity/print-form/auth.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { PrintForm, PrintTemplate } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { PrintForm, PrintTemplate } from './entity'

export const examplePrintForm = {
displayIndex: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './example'
export * from './auth'
16 changes: 0 additions & 16 deletions apps/hcdc-access-service/src/domain/entity/role/auth.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/role/example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exampleMongoObjectId } from '@diut/nestjs-infra'
import { exampleMongoObjectId } from '@diut/common'
import { Role } from '@diut/hcdc'

import { EntityDataExample } from '../base-entity'
import { Role } from './entity'

export const exampleRole = {
displayIndex: {
Expand Down
2 changes: 0 additions & 2 deletions apps/hcdc-access-service/src/domain/entity/role/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './entity'
export * from './auth'
export * from './example'
Loading

0 comments on commit 910d96f

Please sign in to comment.