Trying to implement billing and subscription , having plans with constraint on ressources ( max_number_of_xxx ) so a user subscribed to plan Y can only create xxx number of that ressource , should I use a guard to check for that or how to manage it the right way , trying to find good example to get inspired from , any advice please
#Using Guard to limit access to ressources based on subscription plans
1 messages · Page 1 of 1 (latest)
What you are describing should be attribute based access control (ABAC). Yet, because it is also very much tied to business rules and logic, it probably wouldn't fit too well within a guard. Although you could finagle the logic into a guard, guards are more for basic authorization. So, if the rules stay simple, you can use a guard, but if it gets any where near complicated, especially in terms of the responses, it's better to have the logic in your services.
I'd suggest looking into CASL and how ABAC could work with Nest, creating a set of rules for the user at login, storing them (best in a cache) and recalling the cached rules for requests by that user. You then have this permissions object attached to the user and it can be forwarded in your app or you could also have a "local storage" with nestjs-cls to make the user object with the "can" or "cannot" rules available for each request and across function boundaries.