소스 검색

Authorize the routes

afc163 7 년 전
부모
커밋
631483e6c8
4개의 변경된 파일27개의 추가작업 그리고 2개의 파일을 삭제
  1. 7 1
      config/router.config.js
  2. 1 0
      package.json
  3. 18 0
      src/pages/Authorized.js
  4. 1 1
      src/pages/Dashboard/Monitor.js

+ 7 - 1
config/router.config.js

@@ -14,6 +14,7 @@ module.exports = [
   {
     path: '/',
     component: '../layouts/BasicLayout',
+    Routes: ['src/pages/Authorized'],
     routes: [
       // dashboard
       { path: '/', redirect: '/dashboard/analysis' },
@@ -22,7 +23,12 @@ module.exports = [
         name: 'dashboard',
         icon: 'dashboard',
         routes: [
-          { path: '/dashboard/analysis', name: 'analysis', component: './Dashboard/Analysis' },
+          {
+            path: '/dashboard/analysis',
+            authority: ['admin'],
+            name: 'analysis',
+            component: './Dashboard/Analysis',
+          },
           { path: '/dashboard/monitor', name: 'monitor', component: './Dashboard/Monitor' },
           { path: '/dashboard/workplace', name: 'workplace', component: './Dashboard/Workplace' },
         ],

+ 1 - 0
package.json

@@ -48,6 +48,7 @@
     "react-document-title": "^2.0.3",
     "react-dom": "^16.4.2",
     "react-fittext": "^1.0.0",
+    "react-router-config": "^1.0.0-beta.4",
     "react-router-dom": "^4.3.1",
     "umi": "^2.0.0-beta.10"
   },

+ 18 - 0
src/pages/Authorized.js

@@ -0,0 +1,18 @@
+import React from 'react';
+import RenderAuthorized from '@/components/Authorized';
+import Exception from '@/components/Exception';
+import { matchRoutes } from 'react-router-config';
+
+const Authorized = RenderAuthorized('user');
+const noMatch = <Exception type="403" style={{ minHeight: 500, height: '80%' }} />;
+
+export default ({ children, route, location }) => {
+  const branch =
+    matchRoutes(route.routes, location.pathname).filter(item => item.match.isExact)[0] || {};
+  const { authority } = branch.route || {};
+  return (
+    <Authorized authority={authority} noMatch={noMatch}>
+      {children}
+    </Authorized>
+  );
+};

+ 1 - 1
src/pages/Dashboard/Monitor.js

@@ -18,7 +18,7 @@ const targetTime = new Date().getTime() + 3900000;
 // use permission as a parameter
 const havePermissionAsync = new Promise(resolve => {
   // Call resolve on behalf of passed
-  setTimeout(() => resolve(), 1000);
+  setTimeout(() => resolve(), 100);
 });
 @Secured(havePermissionAsync)
 @connect(({ monitor, loading }) => ({