Search code examples
typescriptnestjsprismacasl

Checking access with CASL throws an error - PrismaORM, NestJs, Typescript


In a middleware I'm setting up a condition to access users, being the users to be in the same appartment than the authenticated user. This condition is the following : can(DirectoryAction.VIEW, 'DirectoryUser', { roles: { some: { role: { unitId: CASL_ROLE.unitId } } } }); DirectoryAction is an enum containing actions such as view, delete, or update. DirectoryUser is the name of my user object. CASL_ROLE is the name I gave to the authenticated user's role. Consider unitId to be the appartment ID.

No exception at compilation, typescript error detected in studio code. It's a pure runtime error : --> "equals" does not supports comparison of arrays and objects

Prisma schemas :

model DirectoryUser {
    id          BigInt   @id @default(autoincrement())
    userName    String   @map("user_name")
    password    String
    roles   DirectoryRoleUserMapping[]

    @@map("directory_users")
}

model DirectoryRoleUserMapping {
    id  BigInt          @id @default(autoincrement())
    roleId  BigInt          @map("role_id")
    role    DirectoryRole   @relation(fields: [roleId], references: [id], onDelete: Cascade, onUpdate: Cascade)
    userId  BigInt      @map("user_id")
    user    DirectoryUser   @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)

    @@unique([roleId, userId])
    @@map("directory_role_user_mappings")
}

model DirectoryRole {
    id     BigInt    @id @default(autoincrement())
    name   String
    unitId BigInt?   @map("unit_id")
    unit   BmsUnit?  @relation(fields: [unitId], references: [id], onDelete: Cascade, onUpdate: Cascade)
    rank   BigInt

    users  DirectoryRoleUserMapping[]

    @@unique([name, unitId])
    @@map("directory_roles")
}

Changind the condition for a less complicated one as (only the condition here) : { roles: { some: { roleId: CASL_ROLE.id } } } Does not throw an error anymore, but the condition don't follow anymore the specifications. It seems that anytime I set an object instead of a pair key: value in the "some", this error shows up. f.e., this condition : { roles: { some: { role: { id: CASL_ROLE.id } } } } Throws an error, but the condition verifies the same thing as the one just before.

Ask more infos if needed ! Thks a lot for your attention !


Solution

  • Sorry for the delay, the answer to this post is available in the issue linked below. Thks to Stalniy (code owner of CASL) for his quick answer at the time.

    https://github.com/stalniy/casl/issues/779