-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Fix useCanAccess
causes extra rerender + use useCanAccess
in the simple demo
#10278
Conversation
useCanAccess
in the simple demouseCanAccess
causes extra rerender + use useCanAccess
in the simple demo
@@ -70,7 +76,7 @@ const UserCreate = () => { | |||
validate={[required(), isValidName, unique()]} | |||
/> | |||
</TabbedForm.Tab> | |||
{permissions === 'admin' && ( | |||
{canManageUsers ? ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could not use <CanAccess>
here because <TabbedForm>
still scans its children, so having <CanAccess>
as one of its child couldn't work.
@@ -153,7 +152,7 @@ const PostCreate = () => { | |||
<TextInput source="url" defaultValue="" /> | |||
</SimpleFormIterator> | |||
</ArrayInput> | |||
{permissions === 'admin' && ( | |||
<CanAccess action="manage_post_authors"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This action name is actually an action and a resource... Why not use action="edit" resource="users"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my first implementation I did this, but then I realized that for such simple access rules we don't need that much complexity, and it allows me to omit the resource
prop completely, making the code easier to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up implementing a mixed approach: keeping a 'custom' action
for things like 'batch_create', and using resource
for fields like 'posts.authors' and 'users.role'.
Seems I need to fix the E2E tests. Back to WIP. |
All green, back to RFR! |
examples/simple/src/authProvider.tsx
Outdated
getPermissions: () => { | ||
const role = localStorage.getItem('role'); | ||
return Promise.resolve(role ?? ''); | ||
return Promise.resolve(); | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this now, right? The getPermissions
method is optional now
Problems
usePermissions
. But React Admin v5.3 will bring a better replacement feature calleduseCanAccess
, which we should demonstrate in this demo.useCanAccess
causes an unnecessary extra rerender to components using it (e.g.EditButton
orShowButton
) when there is noauthProvider
or noauthProvider.canAccess
implementation.Solution
usePermissions
byuseCanAccess
/<CanAccess>
in the simple demo.useCanAccess
only when there is an actualauthProvider.canAccess
implementation (otherwise it creates an extra rerender because of the query status update).How To Test
make run
).Try signing in as:
useCanAccess
unit testsTodo
useCanAccess
query when there is noauthProvider.canAccess
as it causes unnecessary rerenders!authProvider.canAccess
is implemented to returntrue
by default, and call a fn stored in memory that will actually implement thecanAccess
for a given roleAdditional Checks
master
for a bugfix, ornext
for a featureAlso, please make sure to read the contributing guidelines.
Additional Notes
Current use of the permissions in the demo
role
<Resource name="users" {...users} />
<Resource name="users" {...users} />
<ArrayInput source="authors">
<ArrayInput source="authors">
<SaveButton label="user.action.save_and_add" />
<TabbedForm.Tab label="user.form.security" path="security">
<TextInput source="id" InputProps={{ disabled: true }} />
<TabbedForm.Tab label="user.form.security" path="security">
filters.push(<TextInput source="role" />)
rowClick = 'edit'
<SimpleList secondaryText={record.role}>
<TextField source="role" />
<TabbedShowLayout.Tab label="user.form.security">
Issues found while testing
<TabbedForm.Tab label="user.form.security" path="security">
the value prop is required at runtime
<CanAccess>
, because it still clones childrenuseCanAccess
insteadmaster