Authorized.js 1.15 KB
Newer Older
afc163's avatar
afc163 committed
1 2 3 4
import React from 'react';
import RenderAuthorized from '@/components/Authorized';
import Exception from '@/components/Exception';
import { matchRoutes } from 'react-router-config';
5
import uniq from 'lodash/uniq';
afc163's avatar
afc163 committed
6 7
import { formatMessage } from 'umi/locale';
import Link from 'umi/link';
afc163's avatar
afc163 committed
8

afc163's avatar
afc163 committed
9
const Authorized = RenderAuthorized(['admin', 'user']);
afc163's avatar
afc163 committed
10 11

export default ({ children, route, location }) => {
12 13 14
  const routes = matchRoutes(route.routes, location.pathname);
  let authorities = [];
  routes.forEach(item => {
afc163's avatar
afc163 committed
15 16 17 18
    if (Array.isArray(item.route.authority)) {
      authorities = authorities.concat(item.route.authority);
    } else if (typeof item.route.authority === 'string') {
      authorities.push(item.route.authority);
19 20
    }
  });
afc163's avatar
afc163 committed
21 22 23 24 25 26 27 28 29
  const noMatch = (
    <Exception
      type="403"
      desc={formatMessage({ id: 'app.exception.description.403' }, {})}
      style={{ minHeight: 500, height: '80%' }}
      linkElement={Link}
      backText={formatMessage({ id: 'app.exception.back' })}
    />
  );
afc163's avatar
afc163 committed
30
  return (
afc163's avatar
afc163 committed
31 32 33 34
    <Authorized
      authority={authorities.length === 0 ? undefined : uniq(authorities)}
      noMatch={noMatch}
    >
afc163's avatar
afc163 committed
35 36 37 38
      {children}
    </Authorized>
  );
};