Authorized.js 843 Bytes
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

afc163's avatar
afc163 committed
7
const Authorized = RenderAuthorized(['admin', 'user']);
afc163's avatar
afc163 committed
8 9 10
const noMatch = <Exception type="403" style={{ minHeight: 500, height: '80%' }} />;

export default ({ children, route, location }) => {
11 12 13 14 15 16 17 18 19
  const routes = matchRoutes(route.routes, location.pathname);
  let authorities = [];
  routes.forEach(item => {
    if (Array.isArray(item.authority)) {
      authorities = authorities.concat(item.authority);
    } else if (typeof item.authority === 'string') {
      authorities.push(item.authority);
    }
  });
afc163's avatar
afc163 committed
20
  return (
21
    <Authorized authority={uniq(authorities)} noMatch={noMatch}>
afc163's avatar
afc163 committed
22 23 24 25
      {children}
    </Authorized>
  );
};