From 42c5de12f69a73bcfeb8c61928ae91e4ac614ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Tue, 23 Jan 2018 14:14:30 +0800 Subject: [PATCH] authority injection (#702) * authority injection * Delete nonsense * Remove the login page guest role * Formatted as eslint format --- src/components/Authorized/CheckPermissions.js | 2 +- src/components/Authorized/index.d.ts | 2 +- src/components/Authorized/index.md | 11 ++++++----- src/models/login.js | 15 ++++++--------- src/router.js | 1 - src/utils/Authorized.js | 9 ++++++++- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/components/Authorized/CheckPermissions.js b/src/components/Authorized/CheckPermissions.js index a62deed4..0a7dbaed 100644 --- a/src/components/Authorized/CheckPermissions.js +++ b/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; } diff --git a/src/components/Authorized/index.d.ts b/src/components/Authorized/index.d.ts index 45df4e8b..4a68fa18 100644 --- a/src/components/Authorized/index.d.ts +++ b/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 | authorityFN | Promise; diff --git a/src/components/Authorized/index.md b/src/components/Authorized/index.md index 1fe99615..51588bb5 100644 --- a/src/components/Authorized/index.md +++ b/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 | | ### 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 | - | diff --git a/src/models/login.js b/src/models/login.js index 5efd5f19..ea9fd30a 100644 --- a/src/models/login.js +++ b/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')); } }, }, diff --git a/src/router.js b/src/router.js index 4ba72976..54eefd17 100644 --- a/src/router.js +++ b/src/router.js @@ -24,7 +24,6 @@ function RouterConfig({ history, app }) { } - authority="guest" redirectPath="/" /> { + Authorized = RenderAuthorized(getAuthority()); +}; + +export { reloadAuthorized }; export default Authorized; -- GitLab