-
-
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
Access control: pessimistic rendering in CRUD views #10258
Merged
fzaninotto
merged 14 commits into
access-control-resources
from
access-control-views-loading
Oct 4, 2024
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
40adf7d
Introduce useIsAuthPending
djhi 18620e6
Ensure List check auth states pessimistically
djhi 483d84c
Ensure InfiniteList check auth states pessimistically
djhi 99ab061
Ensure Create check auth states pessimistically
djhi bb78db5
Ensure Edit check auth states pessimistically
djhi 75c9ba5
Ensure Show check auth states pessimistically
djhi 6076cf4
Ensure devs can provide their own loading
djhi 41e9853
Ensure a ResourceContext is added only when needed
djhi 5a2df6c
Add stories and tests for ListBase
djhi 4e98cb6
Add stories and tests for InfiniteListBase
djhi 7008b31
Add stories and tests for Create
djhi 0fee4fd
Add stories and tests for Edit
djhi 3a6c3a8
Add stories and tests for Show
djhi a60d5e6
Ensure dashboard waits for all auth calls resolutions
djhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { useResourceContext } from '../core'; | ||
import { HintedString } from '../types'; | ||
import useAuthProvider from './useAuthProvider'; | ||
|
||
/** | ||
* A hook that returns true if the authProvider is currently checking the authentication status or the user's access rights. | ||
* @param params | ||
* @param params.action The action to check access for | ||
* @param params.resource The resource to check access for (optional). Defaults to the resource of the current ResourceContext. | ||
* @returns {boolean} true if the authProvider is currently checking the authentication status or the user's access rights, false otherwise. | ||
*/ | ||
export const useIsAuthPending = (params: UseIsAuthPendingParams) => { | ||
const { action, ...props } = params; | ||
const queryClient = useQueryClient(); | ||
const authProvider = useAuthProvider(); | ||
const resource = useResourceContext(props); | ||
|
||
if (!authProvider) { | ||
return false; | ||
} | ||
|
||
const authQueryState = queryClient.getQueryState(['auth', 'checkAuth', {}]); | ||
const canAccessQueryState = queryClient.getQueryState([ | ||
'auth', | ||
'canAccess', | ||
{ action, resource }, | ||
]); | ||
|
||
if ( | ||
authQueryState?.status === 'pending' || | ||
(authProvider.canAccess && canAccessQueryState?.status === 'pending') | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export type UseIsAuthPendingParams = { | ||
resource?: string; | ||
action: HintedString<'list' | 'create' | 'edit' | 'show' | 'delete'>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
} from 'ra-core'; | ||
|
||
import { CreateView, CreateViewProps } from './CreateView'; | ||
import { Loading } from '../layout'; | ||
|
||
/** | ||
* Page component for the Create view | ||
|
@@ -81,6 +82,7 @@ export const Create = < | |
disableAuthentication={disableAuthentication} | ||
hasEdit={hasEdit} | ||
hasShow={hasShow} | ||
loading={defaultLoading} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stupid me |
||
> | ||
<CreateView {...rest} /> | ||
</CreateBase> | ||
|
@@ -97,3 +99,5 @@ export interface CreateProps< | |
ResultRecordType | ||
>, | ||
CreateViewProps {} | ||
|
||
const defaultLoading = <Loading />; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Otherwise it's a BC. Same for other controllers.
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.
No because the controller already handles it