secured.md 1.4 KB
Newer Older
jim's avatar
jim committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
---
order: 1
title: 
  zh-CN: 注解
  en-US: secured
---

```jsx
import RenderAuthorized from 'ant-design-pro/lib/Authorized';
import { Alert } from 'antd';

const { Secured } = RenderAuthorized('user');
const noMatch = <Alert message="No permission." type="error" showIcon />;

const havePermission = () => {
  return false;
};

const havePermissionAsync = new Promise((resolve, reject) => {
  // Call reslove on behalf of passed
  setTimeout(() => reslove(), 1000);
});

@Secured('admin', noMatch)
class TestSecuredString extends React.Component {
  render() {
    <Alert message="user Passed!" type="success" showIcon />;
  }
}

@Secured(['user', 'admin'], noMatch)
class TestSecuredArray extends React.Component {
  render() {
    <Alert
      message="Use Array as a parameter passed!"
      type="success"
      showIcon
    />;
  }
}

@Secured(havePermission, noMatch)
class TestSecuredFunction extends React.Component {
  render() {
    <Alert
      message="Use function as a parameter passed!"
      type="success"
      showIcon
    />;
  }
}

@Secured(havePermissionAsync, noMatch)
class TestSecuredPromise extends React.Component {
  render() {
    <Alert
      message="Use Promise as a parameter passed!"
      type="success"
      showIcon
    />;
  }
}

ReactDOM.render(
  <div>
    <TestSecuredString />
    <TestSecuredArray />
    <TestSecuredFunction />
    <TestSecuredPromise />
  </div>,
  mountNode,
);
```