Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to reload permissions on demand #8954

Closed
mmateja opened this issue May 29, 2023 · 2 comments
Closed

Ability to reload permissions on demand #8954

mmateja opened this issue May 29, 2023 · 2 comments

Comments

@mmateja
Copy link
Contributor

mmateja commented May 29, 2023

Problem

My project has scoped permissioning model. There are multiple contexts and users have specific permissions assigned within the scope of a context. Here's a sample result one could get by calling auth provider's getPermissions() method:

{
    company_1: ["permission-a", "permission-b"],
    company_2: ["permission-c"]
}

In React Admin, permissions are loaded once, on the page load. This behavior generally makes sense since permissions don't change most of the time.

However, in my use case admins can create new companies (a.k.a scopes) in which permissions need to be recalculated for the application permissioning to work correctly in the newly created context.

Proposed solution

Extend the usePermissions hook to give the ability to refetch permissions when necessary.

const { refetch } = usePermissions();

Alternatives

To get the expected behavior, I had to implement my own permission management outside of React Admin.
It works well but I thought that this seems like a pretty small change that could save me some code and is generic enough to be useful to the others.

I would be willing to create a pull request with such change but I would like to get your feedback not to waste my time doing work that will not get accepted. Please let me know what you think. :)

Update
The implementation seems to be trivial. Here's a PR: #8955
It also seems to be in line with what has been implemented in the useGetIdentity() hook:
https://marmelab.com/react-admin/useGetIdentity.html#refreshing-the-identity

@slax57
Copy link
Contributor

slax57 commented May 30, 2023

Thank you for your suggestion.
This is something that can be done in user-land, by using the queryClient to invalidate the ['auth', 'getPermissions'] queries, which will trigger a refetch.

However, I agree the implementation is almost for free, so I'll accept the PR 🙂
Thanks

@slax57
Copy link
Contributor

slax57 commented May 30, 2023

Resolved via #8955

@slax57 slax57 closed this as completed May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants