Enabled when file src/access.ts exists.
The src/access.ts file have to export default method. The exported method will be executed when the project is initialized. This method needs to return an object. Each value of the object defines a permission.
Eg.:
// src/access.tsexport default function(initialState) {const { userId, role } = initialState;return {canReadFoo: true,canUpdateFoo: role ==='admin',canDeleteFoo: foo => {return foo.ownerId === userId;},};}
You can use initialState data provided by @umijs/plugin-initial-state plugin to initialize user permissions.
The default logic is that if you do not specify permission, you have access permission. If you want to modify it to not specify permission, you have no access permission, please open it.
export default {access: {strictMode: true,},}
With the @umijs/plugin-layout plug-in, you can easily implement permission control for certain pages. As shown below, users can access this page only if they have the canReadPageA permission (defined in src/access.ts). Otherwise, the error page which is built in the @umijs/plugin-layout plugin will be rendered by default.
The access key in route object is string containing corresponding permission name.
// config/route.tsexport const routes = [{path: '/pageA',component: 'PageA',access: 'canReadPageA'}]
useAccess hook<Access> component.Use useAccess hook to obtain permission set defined in the 'src/access.ts`. Example usage:
// pages/pageA.tsimport React from 'react';import { useAccess } from 'umi';const PageA = props => {const { foo } = props;const access = useAccess();if (access.canReadFoo) {// user has permission canReadFoo}return <div>...</div>;};export default PageA;
Provided <Access> component can be used to control access to elements in the application.
| Property | Description | Type | Default | | --- | --- | --- | --- | --- | | accessible | If truthy, children will be rendered, otherwise fallback will be rendered. | boolean | false | | children | Node, which is rendered, when accessible has truthy value. | ReactNode | - | | fallback | Node, which is rendered, when accessible has falsy value. | ReactNode | - |
By default, no content will be rendered.
The complete example:
import React from 'react';import { useAccess, Access } from 'umi';const PageA = props => {const {foo} = props;const access = useAccess();if (access.canReadFoo) {// user has permission canReadFoo}return (<div><Accessaccessible={access.canReadFoo}fallback={<div>Can not read foo content.</div>}>Foo content.</Access><Accessaccessible={access.canUpdateFoo}fallback={<div>Can not update foo.</div>}>Update foo.</Access><Accessaccessible={access.canDeleteFoo(foo)}fallback={<div>Can not delete foo.</div>}>Delete foo.</Access></div>);};