-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[Doc] Add documentation for headless enterprise features in ra-core documentation #10973
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8c7c847
Add documentation for headless enterprise features in ra-core documen…
djhi a41f301
Improve aces control documentation
fzaninotto 9f9b365
Fix syntax
fzaninotto 147ac5b
Apply review suggestions
djhi 7b7d989
Add installation steps
djhi a95edd0
[no ci] fix more anchors
slax57 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 hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,94 @@ | ||
--- | ||
title: "<LockStatusBase>" | ||
--- | ||
|
||
`<LockStatusBase>` displays the lock status of the current record. It allows to visually indicate whether the record is locked or not, by the current user or not, and provides an easy way to lock or unlock the record. | ||
|
||
This feature requires a valid [Enterprise Edition](https://marmelab.com/ra-enterprise/) subscription. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install --save @react-admin/ra-core-ee | ||
# or | ||
yarn add @react-admin/ra-core-ee | ||
``` | ||
|
||
## Usage | ||
|
||
```tsx | ||
import React from 'react'; | ||
import { Lock, LockOpen, LoaderCircle } from 'lucide-react'; | ||
import { LockStatusBase } from '@react-admin/ra-core-ee'; | ||
|
||
export const LockStatus = () => { | ||
return ( | ||
<LockStatusBase | ||
{...props} | ||
render={({ | ||
doLock, | ||
doUnlock, | ||
isLocking, | ||
isPending, | ||
isUnlocking, | ||
lockStatus, | ||
message, | ||
}) => { | ||
if (isPending) { | ||
return null; | ||
} | ||
|
||
if (lockStatus === 'lockedByUser') { | ||
return ( | ||
<button | ||
title={message} | ||
disabled={isUnlocking} | ||
onClick={( | ||
e: React.MouseEvent<HTMLButtonElement> | ||
) => { | ||
e.stopPropagation(); | ||
doUnlock(); | ||
}} | ||
> | ||
{isUnlocking ? ( | ||
<LoaderCircle className="h-4 w-4 animate-spin" /> | ||
) : ( | ||
<Lock className="h-4 w-4" /> | ||
)} | ||
</button> | ||
); | ||
} | ||
if (lockStatus === 'lockedByAnotherUser') { | ||
return ( | ||
<Lock className="h-4 w-4 text-error" /> | ||
); | ||
} | ||
if (lockStatus === 'unlocked') { | ||
return ( | ||
<button | ||
title={message} | ||
disabled={isLocking} | ||
onClick={( | ||
e: React.MouseEvent<HTMLButtonElement> | ||
) => { | ||
e.stopPropagation(); | ||
doLock(); | ||
}} | ||
color="warning" | ||
> | ||
{isLocking ? ( | ||
<LoaderCircle className="h-4 w-4 animate-spin" /> | ||
) : ( | ||
<LockOpen className="h-4 w-4" /> | ||
)} | ||
</button> | ||
); | ||
} | ||
return null; | ||
}} | ||
/> | ||
); | ||
}; | ||
``` | ||
|
||
In addition to the [`useLockCallbacks`](./useLockCallbacks.md) parameters, `<LockStatusBase>` accepts a `render` prop. The function passed to the `render` prop will be called with the result of the `useLockCallbacks` hook. |
127 changes: 127 additions & 0 deletions
127
docs_headless/src/content/docs/canAccessWithPermissions.md
This file contains hidden or 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,127 @@ | ||
--- | ||
title: "canAccessWithPermissions" | ||
--- | ||
|
||
`canAccessWithPermissions` is a helper function that facilitates the implementation of [Access Control](./Permissions.md#access-control) policies based on an underlying list of user roles and permissions. | ||
|
||
It is a builder block to implement the `authProvider.canAccess()` method, which is called by ra-core to check whether the current user has the right to perform a given action on a given resource or record. | ||
|
||
This feature requires a valid [Enterprise Edition](https://marmelab.com/ra-enterprise/) subscription. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install --save @react-admin/ra-core-ee | ||
# or | ||
yarn add @react-admin/ra-core-ee | ||
``` | ||
slax57 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Usage | ||
|
||
`canAccessWithPermissions` is a pure function that you can call from your `authProvider.canAccess()` implementation. | ||
|
||
```tsx | ||
import { canAccessWithPermissions } from '@react-admin/ra-core-ee'; | ||
|
||
const authProvider = { | ||
// ... | ||
canAccess: async ({ action, resource, record }) => { | ||
const permissions = myGetPermissionsFunction(); | ||
return canAccessWithPermissions({ | ||
permissions, | ||
action, | ||
resource, | ||
record, | ||
}); | ||
} | ||
// ... | ||
}; | ||
``` | ||
|
||
The `permissions` parameter must be an array of permissions. A *permission* is an object that represents access to a subset of the application. It is defined by a `resource` (usually a noun) and an `action` (usually a verb), with sometimes an additional `record`. | ||
|
||
Here are a few examples of permissions: | ||
|
||
- `{ action: "*", resource: "*" }`: allow everything | ||
- `{ action: "read", resource: "*" }`: allow read actions on all resources | ||
- `{ action: "read", resource: ["companies", "people"] }`: allow read actions on a subset of resources | ||
- `{ action: ["read", "create", "edit", "export"], resource: "companies" }`: allow all actions except delete on companies | ||
- `{ action: ["write"], resource: "game.score", record: { "id": "123" } }`: allow write action on the score of the game with id 123 | ||
|
||
:::tip | ||
When the `record` field is omitted, the permission is valid for all records. | ||
::: | ||
|
||
In most cases, the permissions are derived from user roles, which are fetched at login and stored in memory or in localStorage. Check the [`getPermissionsFromRoles`](./getPermissionsFromRoles.md) function to merge the permissions from multiple roles into a single flat array of permissions. | ||
|
||
## Parameters | ||
|
||
This function takes an object as argument with the following fields: | ||
|
||
| Name | Optional | Type | Description | ||
| - | - | - | - | | ||
| `permissions` | Required | `Array<Permission>` | An array of permissions for the current user | ||
| `action` | Required | `string` | The action for which to check users has the execution right | ||
| `resource` | Required | `string` | The resource for which to check users has the execution right | ||
| `record` | Required | `string` | The record for which to check users has the execution right | ||
|
||
`canAccessWithPermissions` expects the `permissions` to be a flat array of permissions. It is your responsibility to fetch these permissions (usually during login). If the permissions are spread into several role definitions, you can merge them into a single array using the [`getPermissionsFromRoles`](./getPermissionsFromRoles.md) function. | ||
|
||
## Building RBAC | ||
|
||
The following example shows how to implement Role-based Access Control (RBAC) in `authProvider.canAccess()` using `canAccessWithPermissions` and `getPermissionsFromRoles`. The role permissions are defined in the code, and the user roles are returned by the authentication endpoint. Additional user permissions can also be returned by the authentication endpoint. | ||
|
||
The `authProvider` stores the permissions in `localStorage`, so that returning users can access their permissions without having to log in again. | ||
|
||
```tsx | ||
// in roleDefinitions.ts | ||
export const roleDefinitions = { | ||
admin: [ | ||
{ action: '*', resource: '*' } | ||
], | ||
reader: [ | ||
{ action: ['list', 'show', 'export'], resource: '*' }, | ||
{ action: 'read', resource: 'posts.*' }, | ||
{ action: 'read', resource: 'comments.*' }, | ||
], | ||
accounting: [ | ||
{ action: '*', resource: 'sales' }, | ||
], | ||
}; | ||
|
||
// in authProvider.ts | ||
import { canAccessWithPermissions, getPermissionsFromRoles } from '@react-admin/ra-core-ee'; | ||
import { roleDefinitions } from './roleDefinitions'; | ||
|
||
const authProvider = { | ||
login: async ({ username, password }) => { | ||
const request = new Request('https://mydomain.com/authenticate', { | ||
method: 'POST', | ||
body: JSON.stringify({ username, password }), | ||
headers: new Headers({ 'Content-Type': 'application/json' }), | ||
}); | ||
const response = await fetch(request); | ||
if (response.status < 200 || response.status >= 300) { | ||
throw new Error(response.statusText); | ||
} | ||
const { user: { roles, permissions }} = await response.json(); | ||
// merge the permissions from the roles with the extra permissions | ||
const permissions = getPermissionsFromRoles({ | ||
roleDefinitions, | ||
userPermissions, | ||
userRoles | ||
}); | ||
localStorage.setItem('permissions', JSON.stringify(permissions)); | ||
}, | ||
canAccess: async ({ action, resource, record }) => { | ||
const permissions = JSON.parse(localStorage.getItem('permissions')); | ||
return canAccessWithPermissions({ | ||
permissions, | ||
action, | ||
resource, | ||
record, | ||
}); | ||
} | ||
// ... | ||
}; | ||
``` |
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.
Uh oh!
There was an error while loading. Please reload this page.