question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@casl/mongoose Accessible Records plugin's doesn't work with classes as subject type

See original GitHub issue

Describe the bug I’m using NestJS+CASL+mongoose (+ casl/mongoose). I have Record and User modules. In ability/ability.factory.ts I define ability for mongoose’s models and all the checks work correctly both for the RecordModel and its instances from DB. I also have a simple endpoint in records/records.controller.ts that retrieves all the Records from the DB that the logged user can access (In the actual project I get the logged user through JWT, but in this example I mock it). I get the logged user’s ability using AbilityFactory and pass it to records/records.service.ts’s method find. However, using .accessibleBy(ability) method on the RecordModel throws ForbiddenError saying Cannot execute "read" on "Record" despite ability’s permissions being sufficient (see “To reproduce” section). I have also included some console.logs and comments in records.controller and records.service.

To Reproduce I’ve created a repository with a simple NestJS project with an example of the issue (I couldn’t, however, supply it with proper tests). Clone it, install with npm ci and start with npm run start:dev. A GET request to http://localhost:3000/records/ results in a 403 Forbidden, but is supposed to be a 200 with array of Records. I’ve also created a test Mongo cluster with mock data and included the connection URL in the code.

Expected behavior Since ability is able to Read Records, .accessibleBy() must not throw ForbiddenError and return proper Query object.

CASL Version @casl/ability - v6.0.0 @casl/mongoose - v7.0.0

Environment: nodejs: v16.16.0 typescript: v4.7.4

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
stalniycommented, Aug 6, 2022

OK, found the cause. It appears that casl/mongoose doesnt work in case you use classes as subject types. It expects to work with strings only

2reactions
stalniycommented, Aug 28, 2022

fixed in @casl/mongoose@7.1.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

CASL Mongoose
This package integrates CASL and MongoDB. In other words, it allows to fetch records based on CASL rules from MongoDB and answer questions...
Read more >
CASL Mongoose AccessibleRecords plugin throws ...
Turned out to be a bug: @casl/mongoose doesnt work in case you use classes as subject types.
Read more >
Casl/mongoose and official mongoose types #436 - GitHub
I am using the @casl/ability and the @casl/mongoose package to manage database permissions within nodejs. My code runs just fine but as soon...
Read more >
stalniy-casl/casl - Gitter
Hi there! Is it possible to access subject itself in AbilityBuilder.define method?
Read more >
@casl/mongoose - npm
Warning: make sure that you add that plugin before calling mongoose.model(...) method. Models which were defined before adding plugin will not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found