-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10258 from marmelab/access-control-views-loading
Access control: pessimistic rendering in CRUD views
- Loading branch information
Showing
25 changed files
with
1,275 additions
and
448 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from 'react'; | ||
import { AuthProvider } from '../types'; | ||
import { CoreAdminContext } from '../core'; | ||
import { TestMemoryRouter, WithPermissions } from '..'; | ||
|
||
export default { | ||
title: 'ra-core/auth/WithPermissions', | ||
}; | ||
|
||
export const NoAuthProvider = () => ( | ||
<TestMemoryRouter> | ||
<CoreAdminContext> | ||
<WithPermissions component={StateInspector} /> | ||
</CoreAdminContext> | ||
</TestMemoryRouter> | ||
); | ||
|
||
export const NoAuthProviderGetPermissions = ({ | ||
loading = () => <p>Loading...</p>, | ||
}: { | ||
loading: React.ComponentType; | ||
}) => ( | ||
<TestMemoryRouter> | ||
<CoreAdminContext | ||
authProvider={{ | ||
login: () => Promise.reject('bad method'), | ||
logout: () => Promise.reject('bad method'), | ||
checkAuth: () => | ||
new Promise(resolve => setTimeout(resolve, 300)), | ||
checkError: () => Promise.reject('bad method'), | ||
}} | ||
> | ||
<WithPermissions component={StateInspector} loading={loading} /> | ||
</CoreAdminContext> | ||
</TestMemoryRouter> | ||
); | ||
|
||
export const WithAuthProviderAndGetPermissions = ({ | ||
loading = () => <p>Loading...</p>, | ||
authProvider = { | ||
login: () => Promise.reject('bad method'), | ||
logout: () => Promise.reject('bad method'), | ||
checkAuth: () => new Promise(resolve => setTimeout(resolve, 300)), | ||
checkError: () => Promise.reject('bad method'), | ||
getPermissions: () => | ||
new Promise(resolve => setTimeout(resolve, 300, 'admin')), | ||
}, | ||
}: { | ||
loading: React.ComponentType; | ||
authProvider?: AuthProvider; | ||
}) => ( | ||
<TestMemoryRouter> | ||
<CoreAdminContext authProvider={authProvider}> | ||
<WithPermissions component={StateInspector} loading={loading} /> | ||
</CoreAdminContext> | ||
</TestMemoryRouter> | ||
); | ||
|
||
const StateInspector = ({ permissions }: { permissions: any }) => ( | ||
<div> | ||
{permissions === 'admin' ? ( | ||
<p>Sensitive data</p> | ||
) : ( | ||
<p>Non sensitive data</p> | ||
)} | ||
</div> | ||
); |
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 |
---|---|---|
@@ -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'>; | ||
}; |
Oops, something went wrong.