Commit 42c5de12 authored by 陈帅's avatar 陈帅 Committed by ddcat1115

authority injection (#702)

* authority injection

* Delete nonsense

* Remove the login page guest role

* Formatted as eslint format
parent b4fd0e05
...@@ -46,7 +46,7 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => { ...@@ -46,7 +46,7 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => {
// Function 处理 // Function 处理
if (typeof authority === 'function') { if (typeof authority === 'function') {
try { try {
const bool = authority(); const bool = authority(currentAuthority);
if (bool) { if (bool) {
return target; return target;
} }
......
import * as React from 'react'; import * as React from 'react';
import { RouteProps } from 'react-router'; import { RouteProps } from 'react-router';
type authorityFN = () => string; type authorityFN = (currentAuthority?: string) => boolean;
type authority = string | Array<string> | authorityFN | Promise<any>; type authority = string | Array<string> | authorityFN | Promise<any>;
......
...@@ -17,6 +17,7 @@ order: 15 ...@@ -17,6 +17,7 @@ order: 15
权限组件默认 export RenderAuthorized 函数,它接收当前权限作为参数,返回一个权限对象,该对象提供以下几种使用方式。 权限组件默认 export RenderAuthorized 函数,它接收当前权限作为参数,返回一个权限对象,该对象提供以下几种使用方式。
### Authorized ### Authorized
最基础的权限控制。 最基础的权限控制。
...@@ -24,14 +25,14 @@ order: 15 ...@@ -24,14 +25,14 @@ order: 15
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|----------|------------------------------------------|-------------|-------| |----------|------------------------------------------|-------------|-------|
| children | 正常渲染的元素,权限判断通过时展示 | ReactNode | - | | children | 正常渲染的元素,权限判断通过时展示 | ReactNode | - |
| authority | 准入权限/权限判断 | `string | array | Promise | () => boolean` | - | | authority | 准入权限/权限判断 | `string | array | Promise | (currentAuthority) => boolean` | - |
| noMatch | 权限异常渲染元素,权限判断不通过时展示 | ReactNode | - | | noMatch | 权限异常渲染元素,权限判断不通过时展示 | ReactNode | - |
### Authorized.AuthorizedRoute ### Authorized.AuthorizedRoute
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|----------|------------------------------------------|-------------|-------| |----------|------------------------------------------|-------------|-------|
| authority | 准入权限/权限判断 | `string | array | Promise | () => boolean` | - | | authority | 准入权限/权限判断 | `string | array | Promise | (currentAuthority) => boolean` | - |
| redirectPath | 权限异常时重定向的页面路由 | string | - | | redirectPath | 权限异常时重定向的页面路由 | string | - |
其余参数与 `Route` 相同。 其余参数与 `Route` 相同。
...@@ -42,7 +43,7 @@ order: 15 ...@@ -42,7 +43,7 @@ order: 15
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|----------|------------------------------------------|-------------|-------| |----------|------------------------------------------|-------------|-------|
| authority | 准入权限/权限判断 | `string | Promise | () => boolean` | - | | authority | 准入权限/权限判断 | `string | Promise | (currentAuthority) => boolean` | - |
| error | 权限异常时渲染元素 | ReactNode | <Exception type="403" /> | | error | 权限异常时渲染元素 | ReactNode | <Exception type="403" /> |
### Authorized.check ### Authorized.check
...@@ -52,6 +53,6 @@ order: 15 ...@@ -52,6 +53,6 @@ order: 15
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|----------|------------------------------------------|-------------|-------| |----------|------------------------------------------|-------------|-------|
| authority | 准入权限/权限判断 | `string | Promise | () => boolean` | - | | authority | 准入权限/权限判断 | `string | Promise | (currentAuthority) => boolean` | - |
| target | 权限判断通过时渲染的元素 | `string | array | Promise | () => boolean` | - | | target | 权限判断通过时渲染的元素 | ReactNode | - |
| Exception | 权限异常时渲染元素 | ReactNode | - | | Exception | 权限异常时渲染元素 | ReactNode | - |
import { routerRedux } from 'dva/router';
import { fakeAccountLogin } from '../services/api'; import { fakeAccountLogin } from '../services/api';
import { setAuthority } from '../utils/authority'; import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';
export default { export default {
namespace: 'login', namespace: 'login',
...@@ -17,11 +19,8 @@ export default { ...@@ -17,11 +19,8 @@ export default {
}); });
// Login successfully // Login successfully
if (response.status === 'ok') { if (response.status === 'ok') {
// 非常粗暴的跳转,登陆成功之后权限会变成user或admin,会自动重定向到主页 reloadAuthorized();
// Login success after permission changes to admin or user yield put(routerRedux.push('/'));
// The refresh will automatically redirect to the home page
// yield put(routerRedux.push('/'));
window.location.reload();
} }
}, },
*logout(_, { put, select }) { *logout(_, { put, select }) {
...@@ -33,9 +32,6 @@ export default { ...@@ -33,9 +32,6 @@ export default {
urlParams.searchParams.set('redirect', pathname); urlParams.searchParams.set('redirect', pathname);
window.history.replaceState(null, 'login', urlParams.href); window.history.replaceState(null, 'login', urlParams.href);
} finally { } 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({ yield put({
type: 'changeLoginStatus', type: 'changeLoginStatus',
payload: { payload: {
...@@ -43,7 +39,8 @@ export default { ...@@ -43,7 +39,8 @@ export default {
currentAuthority: 'guest', currentAuthority: 'guest',
}, },
}); });
window.location.reload(); reloadAuthorized();
yield put(routerRedux.push('/user/login'));
} }
}, },
}, },
......
...@@ -24,7 +24,6 @@ function RouterConfig({ history, app }) { ...@@ -24,7 +24,6 @@ function RouterConfig({ history, app }) {
<AuthorizedRoute <AuthorizedRoute
path="/user" path="/user"
render={props => <UserLayout {...props} />} render={props => <UserLayout {...props} />}
authority="guest"
redirectPath="/" redirectPath="/"
/> />
<AuthorizedRoute <AuthorizedRoute
......
import RenderAuthorized from '../components/Authorized'; import RenderAuthorized from '../components/Authorized';
import { getAuthority } from './authority'; 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; export default Authorized;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment