Authorized.js 1.22 KB
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
import { matchRoutes } from 'react-router-config';
Tan90Qian's avatar
Tan90Qian committed
6
import intersection from 'lodash/intersection';
afc163's avatar
afc163 committed
7 8
import { formatMessage } from 'umi/locale';
import Link from 'umi/link';
afc163's avatar
afc163 committed
9

Tan90Qian's avatar
Tan90Qian committed
10
const Authorized = RenderAuthorized(getAuthority());
afc163's avatar
afc163 committed
11 12

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