Authorized.js 966 Bytes
Newer Older
afc163's avatar
afc163 committed
1 2 3
import React from 'react';
import RenderAuthorized from '@/components/Authorized';
import Exception from '@/components/Exception';
Tan90Qian's avatar
Tan90Qian committed
4
import { getAuthority } from '@/utils/authority';
afc163's avatar
afc163 committed
5 6
import { formatMessage } from 'umi/locale';
import Link from 'umi/link';
陈帅's avatar
陈帅 committed
7
import Redirect from 'umi/redirect';
afc163's avatar
afc163 committed
8

陈帅's avatar
陈帅 committed
9 10
const Authority = getAuthority();
const Authorized = RenderAuthorized(Authority);
afc163's avatar
afc163 committed
11

陈帅's avatar
陈帅 committed
12 13
export default ({ children }) => {
  let noMatch = (
afc163's avatar
afc163 committed
14 15
    <Exception
      type="403"
afc163's avatar
afc163 committed
16
      desc={formatMessage({ id: 'app.exception.description.403' })}
afc163's avatar
afc163 committed
17
      linkElement={Link}
陈帅's avatar
陈帅 committed
18 19
      redirect="/user/login"
      backText="back to login"
afc163's avatar
afc163 committed
20 21
    />
  );
陈帅's avatar
陈帅 committed
22 23
  // if Authority === ['guest'] redirect to /user/login
  // You can implement the logic here.
陈帅's avatar
陈帅 committed
24
  if (Authority === 'guest' || Authority.join('') === 'guest') {
陈帅's avatar
陈帅 committed
25 26
    noMatch = <Redirect to="/user/login" />;
  }
afc163's avatar
afc163 committed
27
  return (
陈帅's avatar
陈帅 committed
28
    <Authorized authority={children.props.route.authority} noMatch={noMatch}>
afc163's avatar
afc163 committed
29 30 31 32
      {children}
    </Authorized>
  );
};