Within a middleware, I am implementing a condition to grant access to users who reside in the same apartment as the authenticated user. The condition is as follows: can(DirectoryAction.VIEW, 'DirectoryUser', { roles: { some: { role: { unitId: CASL_ROLE.unitId } } } }); Here, DirectoryAction is an enum containing actions like view, delete, or update. DirectoryUser represents the user object, and CASL_ROLE refers to the role of the authenticated user with unitId being the apartment ID.
I encountered no exceptions during compilation, but a TypeScript error was detected in the Visual Studio code. This error specifically states that "equals" does not support the comparison of arrays and objects at runtime.
The Prisma schemas are outlined as:
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")
}
By simplifying the condition to { roles: { some: { roleId: CASL_ROLE.id } } }, the error is resolved but the condition no longer adheres to the specified requirements. It appears that anytime an object is set instead of a key-value pair within the "some" field, this error surfaces. For example, the condition { roles: { some: { role: { id: CASL_ROLE.id } } } } triggers an error, even though it essentially checks for the same criteria as the previous one.
Feel free to ask for more information if needed! Thank you for your attention!