In offical document about authorization , i follow this guide and use casl
lib
Let's assume that if i want to validate the request man is the owner of an article, i must get the article data from the database, and compare userId
between user
and article.userId
, so i must inject a repoistory dependency like this
@Injectable()
export class DeleteHolePolicyHandler implements IPolicyHandler {
@Inject()
private readonly treeholeDaoService: TreeholeDaoService
async handle(ability: AppAbility, req: Request) {
const hole = await this.treeholeDaoService.findById(req.body.id)
return res
}
}
but i got an error, it shows me this.treeholeDaoService
is undefined.
so what should i do that can make it work?
this is reproduce link
You can't inject dependencies since it's an in-place property declared using new. As stated in the docs :
Since we must instantiate the policy handler in-place using the new keyword,
ReadArticlePolicyHandler
class cannot use the Dependency Injection. This can be addressed with theModuleRef#get
method (read more here ). Basically, instead of registering functions and instances through the@CheckPolicies()
decorator, you must allow passing aType<IPolicyHandler>
. Then, inside your guard, you could retrieve an instance using a type reference:moduleRef.get(YOUR_HANDLER_TYPE)
or even dynamically instantiate it using the ModuleRef#create method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.