Auth.js 1.12 KB
Newer Older
xiaohuoni's avatar
xiaohuoni 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
import React from 'react';
import pathToRegexp from 'path-to-regexp';
import Authorized from '@/utils/Authorized';
import Exception403 from './Exception/403';

export default ({ children, location }) => {
  /* eslint-disable no-underscore-dangle */
  const { routerData } = window.g_app._store.getState().menu;
  const getRouteAuthority = (pathname, routeData) => {
    const routes = routeData.slice(); // clone

    const getAuthority = (routeDatas, path) => {
      let authorities;
      routeDatas.forEach(route => {
        // check partial route
        if (pathToRegexp(`${route.path}(.*)`).test(path)) {
          if (route.authority) {
            authorities = route.authority;
          }
          // is exact route?
          if (!pathToRegexp(route.path).test(path) && route.routes) {
            authorities = getAuthority(route.routes, path);
          }
        }
      });
      return authorities;
    };

    return getAuthority(routes, pathname);
  };

  return (
    <Authorized
      authority={getRouteAuthority(location.pathname, routerData)}
      noMatch={<Exception403 />}
    >
      {children}
    </Authorized>
  );
};