Procházet zdrojové kódy

authority injection (#702)

* authority injection

* Delete nonsense

* Remove the login page guest role

* Formatted as eslint format
陈帅 před 8 roky
rodič
revize
42c5de12f6

+ 1 - 1
src/components/Authorized/CheckPermissions.js

@@ -46,7 +46,7 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => {
   // Function 处理
   if (typeof authority === 'function') {
     try {
-      const bool = authority();
+      const bool = authority(currentAuthority);
       if (bool) {
         return target;
       }

+ 1 - 1
src/components/Authorized/index.d.ts

@@ -1,7 +1,7 @@
 import * as React from 'react';
 import { RouteProps } from 'react-router';
 
-type authorityFN = () => string;
+type authorityFN = (currentAuthority?: string) => boolean;
 
 type authority = string | Array<string> | authorityFN | Promise<any>;
 

+ 6 - 5
src/components/Authorized/index.md

@@ -17,6 +17,7 @@ order: 15
 
 权限组件默认 export RenderAuthorized 函数,它接收当前权限作为参数,返回一个权限对象,该对象提供以下几种使用方式。
 
+
 ### Authorized
 
 最基础的权限控制。
@@ -24,14 +25,14 @@ order: 15
 | 参数      | 说明                                      | 类型         | 默认值 |
 |----------|------------------------------------------|-------------|-------|
 | children    | 正常渲染的元素,权限判断通过时展示           | ReactNode  | - |
-| authority   | 准入权限/权限判断         | `string | array | Promise | () => boolean` | - |
+| authority   | 准入权限/权限判断         | `string | array | Promise | (currentAuthority) => boolean` | - |
 | noMatch     | 权限异常渲染元素,权限判断不通过时展示        | ReactNode  | - |
 
 ### Authorized.AuthorizedRoute
 
 | 参数      | 说明                                      | 类型         | 默认值 |
 |----------|------------------------------------------|-------------|-------|
-| authority     | 准入权限/权限判断         | `string | array | Promise | () => boolean` | - |
+| authority     | 准入权限/权限判断         | `string | array | Promise | (currentAuthority) => boolean` | - |
 | redirectPath  | 权限异常时重定向的页面路由                | string  | - |
 
 其余参数与 `Route` 相同。
@@ -42,7 +43,7 @@ order: 15
 
 | 参数      | 说明                                      | 类型         | 默认值 |
 |----------|------------------------------------------|-------------|-------|
-| authority     | 准入权限/权限判断         | `string | Promise | () => boolean` | - |
+| authority     | 准入权限/权限判断         | `string | Promise | (currentAuthority) => boolean` | - |
 | error  | 权限异常时渲染元素                |  ReactNode | <Exception type="403" /> |
 
 ### Authorized.check
@@ -52,6 +53,6 @@ order: 15
 
 | 参数      | 说明                                      | 类型         | 默认值 |
 |----------|------------------------------------------|-------------|-------|
-| authority     | 准入权限/权限判断         | `string | Promise | () => boolean` | - |
-| target     | 权限判断通过时渲染的元素         | `string | array | Promise | () => boolean` | - |
+| authority     | 准入权限/权限判断         | `string | Promise | (currentAuthority) => boolean` | - |
+| target     | 权限判断通过时渲染的元素         | ReactNode | - |
 | Exception  | 权限异常时渲染元素                |  ReactNode | - |

+ 6 - 9
src/models/login.js

@@ -1,5 +1,7 @@
+import { routerRedux } from 'dva/router';
 import { fakeAccountLogin } from '../services/api';
 import { setAuthority } from '../utils/authority';
+import { reloadAuthorized } from '../utils/Authorized';
 
 export default {
   namespace: 'login',
@@ -17,11 +19,8 @@ export default {
       });
       // Login successfully
       if (response.status === 'ok') {
-        // 非常粗暴的跳转,登陆成功之后权限会变成user或admin,会自动重定向到主页
-        // Login success after permission changes to admin or user
-        // The refresh will automatically redirect to the home page
-        // yield put(routerRedux.push('/'));
-        window.location.reload();
+        reloadAuthorized();
+        yield put(routerRedux.push('/'));
       }
     },
     *logout(_, { put, select }) {
@@ -33,9 +32,6 @@ export default {
         urlParams.searchParams.set('redirect', pathname);
         window.history.replaceState(null, 'login', urlParams.href);
       } finally {
-        // yield put(routerRedux.push('/user/login'));
-        // Login out after permission changes to admin or user
-        // The refresh will automatically redirect to the login page
         yield put({
           type: 'changeLoginStatus',
           payload: {
@@ -43,7 +39,8 @@ export default {
             currentAuthority: 'guest',
           },
         });
-        window.location.reload();
+        reloadAuthorized();
+        yield put(routerRedux.push('/user/login'));
       }
     },
   },

+ 0 - 1
src/router.js

@@ -24,7 +24,6 @@ function RouterConfig({ history, app }) {
           <AuthorizedRoute
             path="/user"
             render={props => <UserLayout {...props} />}
-            authority="guest"
             redirectPath="/"
           />
           <AuthorizedRoute

+ 8 - 1
src/utils/Authorized.js

@@ -1,5 +1,12 @@
 import RenderAuthorized from '../components/Authorized';
 import { getAuthority } from './authority';
 
-const Authorized = RenderAuthorized(getAuthority());
+let Authorized = RenderAuthorized(getAuthority()); // eslint-disable-line
+
+// Reload the rights component
+const reloadAuthorized = () => {
+  Authorized = RenderAuthorized(getAuthority());
+};
+
+export { reloadAuthorized };
 export default Authorized;