How to automatically pass parameter to CanParams in Access control
I have an application with routing enabled and customerId a parameter of the routes :
ex : /customer/:customerId/users or /customer/:customerId/locations/:locationId
And I want the :customerId parameter of route to be used automatically in CanParams.params of my accessControlProvider to apply customer permissions.
2 Replies
Hi @quick691. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources, there isn't a built-in way to automatically pass route parameters like
:customerId
to the CanParams.params
in the accessControlProvider
. However, I can suggest a few approaches to achieve what you're looking for.
Using CanAccess with Router Integrations
Refine's router integrations can infer some parameters from the current route. As mentioned in the documentation:
Refine's router integrations can inferRefine's Authorization documentation indicates that this works for the basic parameters, but doesn't specifically mention custom route parameters likeresource
,action
, andparams.id
props from the current route and pass them to the<CanAccess />
component.
:customerId
.
Approaches to Solve Your Issue
1. Explicitly Pass Parameters in CanAccess Component
You can explicitly pass the customer ID to the CanAccess
component:
2. Use useCan Hook with Route Parameters
Similarly, you can use the useCan
hook and pass the route parameters:
3. Custom Access Control Provider
According to the Access Control Provider documentation, you can access the resource object in the can
method. You might be able to extend this to extract route parameters:
Limitations
It's worth noting that there was a feature request (GitHub issue #4668) to allow custom params to be passed on the accessControl property on refine buttons, but the Refine team suggested using the CanAccess
component as a wrapper instead:
This approach disables the built-in access control on the button and relies on the parent CanAccess
component instead.
Unfortunately, there doesn't appear to be a way to automatically pass custom route parameters to all access control checks without explicitly providing them in each CanAccess
component or useCan
hook call.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@347285614531444756> Kapa.ai is still learning and improving, please let me know how I did by reacting below.