Authorized权限

权限组件,通过比对现有权限与准入权限,决定相关元素的展示。

引用方式:

  1. import Authorized from 'ant-design-pro/lib/Authorized';

详细使用方式请参照:独立使用 pro 组件

代码演示

Authorized 权限 - 图1

基本使用

Basic use

  1. import RenderAuthorized from 'ant-design-pro/lib/Authorized';
  2. import { Alert } from 'antd';
  3. const Authorized = RenderAuthorized('user');
  4. const noMatch = <Alert message="No permission." type="error" showIcon />;
  5. ReactDOM.render(
  6. <div>
  7. <Authorized authority="admin" noMatch={noMatch}>
  8. <Alert message="user Passed!" type="success" showIcon />
  9. </Authorized>
  10. </div>,
  11. mountNode,
  12. );

Authorized 权限 - 图2

使用数组作为参数

Use Array as a parameter

  1. import RenderAuthorized from 'ant-design-pro/lib/Authorized';
  2. import { Alert } from 'antd';
  3. const Authorized = RenderAuthorized('user');
  4. const noMatch = <Alert message="No permission." type="error" showIcon />;
  5. ReactDOM.render(
  6. <Authorized authority={['user', 'admin']} noMatch={noMatch}>
  7. <Alert message="Use Array as a parameter passed!" type="success" showIcon />
  8. </Authorized>,
  9. mountNode,
  10. );

Authorized 权限 - 图3

使用方法作为参数

Use Function as a parameter

  1. import RenderAuthorized from 'ant-design-pro/lib/Authorized';
  2. import { Alert } from 'antd';
  3. const Authorized = RenderAuthorized('user');
  4. const noMatch = <Alert message="No permission." type="error" showIcon />;
  5. const havePermission = () => {
  6. return false;
  7. };
  8. ReactDOM.render(
  9. <Authorized authority={havePermission} noMatch={noMatch}>
  10. <Alert
  11. message="Use Function as a parameter passed!"
  12. type="success"
  13. showIcon
  14. />
  15. </Authorized>,
  16. mountNode,
  17. );

Authorized 权限 - 图4

注解基本使用

secured demo used

  1. import RenderAuthorized from 'ant-design-pro/lib/Authorized';
  2. import { Alert } from 'antd';
  3. const { Secured } = RenderAuthorized('user');
  4. @Secured('admin')
  5. class TestSecuredString extends React.Component {
  6. render() {
  7. <Alert message="user Passed!" type="success" showIcon />;
  8. }
  9. }
  10. ReactDOM.render(
  11. <div>
  12. <TestSecuredString />
  13. </div>,
  14. mountNode,
  15. );

API

RenderAuthorized

RenderAuthorized: (currentAuthority: string | () => string) => Authorized

权限组件默认 export RenderAuthorized 函数,它接收当前权限作为参数,返回一个权限对象,该对象提供以下几种使用方式。

Authorized

最基础的权限控制。

参数说明类型默认值
children正常渲染的元素,权限判断通过时展示ReactNode-
authority准入权限/权限判断string | array | Promise | (currentAuthority) => boolean | Promise-
noMatch权限异常渲染元素,权限判断不通过时展示ReactNode-

Authorized.AuthorizedRoute

参数说明类型默认值
authority准入权限/权限判断string | array | Promise | (currentAuthority) => boolean | Promise-
redirectPath权限异常时重定向的页面路由string-

其余参数与 Route 相同。

Authorized.Secured

注解方式,@Authorized.Secured(authority, error)

参数说明类型默认值
authority准入权限/权限判断string | Promise | (currentAuthority) => boolean | Promise-
error权限异常时渲染元素ReactNode

Authorized.check

函数形式的 Authorized,用于某些不能被 HOC 包裹的组件。 Authorized.check(authority, target, Exception)注意:传入一个 Promise 时,无论正确还是错误返回的都是一个 ReactClass。

参数说明类型默认值
authority准入权限/权限判断string | Promise | (currentAuthority) => boolean | Promise-
target权限判断通过时渲染的元素ReactNode-
Exception权限异常时渲染元素ReactNode-