Restrict an entier ressources with usePermission

Hi everyone !! I try to display ressources according to certains roles return by the API. I see in the doc that we can limit editing according to roles or limit certain view but how can we limit an entiere ressources ?
Q
quickest-silver435d ago
Hi,
maybe you can create <HavePermission> component and you can wrap your resources
const HavePermission: React.FC<React.PropsWithChildren> = ({ children }) => {
const resource = useResource()
const { data: permissionsData } = usePermissions()

// do business logic here to check if user has permission to access this resource

// if not, return null or redirect to another page

// if yes, return children
return <>{children}</>
}
const HavePermission: React.FC<React.PropsWithChildren> = ({ children }) => {
const resource = useResource()
const { data: permissionsData } = usePermissions()

// do business logic here to check if user has permission to access this resource

// if not, return null or redirect to another page

// if yes, return children
return <>{children}</>
}
`
<Refine
// ...
resources={[
{
name: 'products',
list: () => (
<HavePermission>
<ProductList />
</HavePermission>
),
},
]}
/>
<Refine
// ...
resources={[
{
name: 'products',
list: () => (
<HavePermission>
<ProductList />
</HavePermission>
),
},
]}
/>
for more fine tuned permission handling, probably this https://refine.dev/docs/api-reference/core/providers/accessControl-provider/ is a better option
N
national-gold435d ago
Thank you @alicanerdurmaz, is it possible t o do it on pages of a ressouces object instead of do it a it page ? For example, specifie an option like we specify a DataProvider
resources={
// ...other ressources
[
{
name: "users",
list: UsersList,
show: UsersShow,
edit: UserEdit,
options: {
// **refine** will use the `users` data provider for this resource
dataProviderName: "users"
}
}
]
}
resources={
// ...other ressources
[
{
name: "users",
list: UsersList,
show: UsersShow,
edit: UserEdit,
options: {
// **refine** will use the `users` data provider for this resource
dataProviderName: "users"
}
}
]
}
Q
quickest-silver435d ago
unfortunately not 😦 refine handles this with more finely-tuned with accessControlProvider for example; when you use ths code, all products pages will give 404 and will remove on sider.
accessControlProvider={{
can: async ({ resource, action, params }) => {
if (resource === 'products') {
return Promise.resolve({
can: false,
reason: 'Unauthorized',
})
}

return Promise.resolve({ can: true })
},
}}
accessControlProvider={{
can: async ({ resource, action, params }) => {
if (resource === 'products') {
return Promise.resolve({
can: false,
reason: 'Unauthorized',
})
}

return Promise.resolve({ can: true })
},
}}
N
national-gold435d ago
Ok thank you @alicanerdurmaz, have a nice day 💪
Q
quickest-silver435d ago
you too 🙏
More Posts
logingoogle auth not workingHow to mock and test SelectionsHi. Im having a hard time on jest testing the Select. Is there a sample reference online? Thank youinference + hasura + chakraHello guys, Im trying to work with inference with hasura and chakra, but it dont work, can somebodyLinking to other resourcesI'll explain this with the API URLs directly, as it will be easier for me, I have multiple resourcesTailwind ConfigurationHello, all! I trying to run Tailwind on my Refine project, and and even though I configured the UI AuthPage in refine-chakra-ui does not work with refine-nextjs-routerIt seems like AuthPage in refine-chakra-ui does not work with refine-nextjs-router. Can you please hadd forgot-password to @pankod/refine-nextjs-routerThe example in @pankod/refine-nextjs-router does not offer the forgot-password. Is it possible to aImported React Component Mangled (Leaflet )Hello, I am trying to use Leaflet to show a map where a lat/long is located, unfortunately it is notInferred List View Issues: (Realtime Support/Empty List)I followed the instructions here, https://refine.dev/docs/advanced-tutorials/data-provider/supabase/TypeError: Cannot convert undefined or null to objectI get this error always🔥Finefoods example: A lot of error at first startI just get finefoods example and try to start on my Mac. It started successfully but hundred warningFormatting error in inferencer SyntaxError: Declaration or statement expected. (100:21)I don't know what to do...Cannot display data from useTableTable with Ecologist is displaying correctly, but the other one with Applications is not, what is thI think .env is not working or im just missing something?Hey guys good day, I solved my problem just now about the Google log-in auth and it is good but I do