Unverified Commit 1fe0d107 authored by yqwoe's avatar yqwoe Committed by GitHub

Block permission control (#2218)

* feat: use-service hooks * feat: 区块权限控制
parent 5d5790da
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
"lodash": "^4.17.21", "lodash": "^4.17.21",
"million": "^2.5.1-beta.3", "million": "^2.5.1-beta.3",
"moment": "^2.29.4", "moment": "^2.29.4",
"monaco-editor": "^0.41.0",
"omit.js": "^2.0.2", "omit.js": "^2.0.2",
"rc-menu": "^9.8.4", "rc-menu": "^9.8.4",
"rc-util": "^5.33.0", "rc-util": "^5.33.0",
......
...@@ -32,6 +32,7 @@ import {persistStore, persistReducer} from 'redux-persist'; ...@@ -32,6 +32,7 @@ import {persistStore, persistReducer} from 'redux-persist';
import storage from 'redux-persist/lib/storage'; import storage from 'redux-persist/lib/storage';
import {Reducer, StoreEnhancer} from 'redux'; import {Reducer, StoreEnhancer} from 'redux';
import { API } from "./services/data"; import { API } from "./services/data";
import {AccessContextProvider} from '@/hooks/useAccess';
// const isDev = process.env.NODE_ENV === "development"; // const isDev = process.env.NODE_ENV === "development";
const loginPath = API_CONSTANTS.LOGIN_PATH; const loginPath = API_CONSTANTS.LOGIN_PATH;
...@@ -146,9 +147,9 @@ export const layout: RunTimeLayoutConfig = ({initialState}) => { ...@@ -146,9 +147,9 @@ export const layout: RunTimeLayoutConfig = ({initialState}) => {
childrenRender: (children) => { childrenRender: (children) => {
if (initialState?.loading) return <PageLoading/>; if (initialState?.loading) return <PageLoading/>;
return ( return (
<> <AccessContextProvider currentUser={initialState?.currentUser}>
{children} {children}
</> </AccessContextProvider>
); );
}, },
...initialState?.settings, ...initialState?.settings,
......
import {createContext,ReactElement,useContext} from'react'
/***
* 按钮的path和name
*/
export type Block= {
path: string;
name: string;
}
export type AccessContextProps = {
isAdmin: boolean;
blocks: Block[];
}
export const AccessContext = createContext<AccessContextProps>(null!);
type AuthorizedProps = {
path: string;
denied?: ReactElement | null;
children?: ReactElement | null;
}
export function Authorized({
path,
denied = null,
children = null,
}: AuthorizedProps) {
const {isAdmin,blocks=[]} = useContext(AccessContext);
if(isAdmin) return children
if(!blocks.length) return denied;
const authority = blocks.some(block=>block.path === path )
return authority ? children : denied;
}
export const AccessContextProvider = ({children,currentUser}) =>{
const isAdmin = currentUser?.user?.superAdminFlag
let blocks: Block[] = []
const flatTree = (menus) =>{
menus.forEach(({path,children,name,type})=>{
if(type === 'F'){
blocks.push({
path,
name
})
}
if(children.length){
flatTree(children)
}
})
}
flatTree(currentUser?.menuList)
return <AccessContext.Provider value={{isAdmin,blocks}}>
{children}
</AccessContext.Provider>
}
export const useAccess = () => useContext(AccessContext)
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
* limitations under the License. * limitations under the License.
* *
*/ */
import { Authorized } from '@/hooks/useAccess';
import DevOverView from '@/pages/Home/DevOverView'; import DevOverView from '@/pages/Home/DevOverView';
import JobOverView from '@/pages/Home/JobOverView'; import JobOverView from '@/pages/Home/JobOverView';
import { PageContainer } from '@ant-design/pro-components'; import { PageContainer } from '@ant-design/pro-components';
...@@ -24,16 +25,20 @@ import { Col, Row } from 'antd'; ...@@ -24,16 +25,20 @@ import { Col, Row } from 'antd';
export default () => { export default () => {
return ( return (
<PageContainer title={false}> <PageContainer title={false}>
<Row style={{ marginTop: '5px', marginBottom: '10px' }}> <Authorized path="/home/jobOverView">
<Col span={24}> <Row style={{ marginTop: '5px', marginBottom: '10px' }}>
<JobOverView /> <Col span={24}>
</Col> <JobOverView />
</Row> </Col>
<Row> </Row>
<Col span={24}> </Authorized>
<DevOverView /> <Authorized path="/home/devOverView">
</Col> <Row>
</Row> <Col span={24}>
<DevOverView />
</Col>
</Row>
</Authorized>
</PageContainer> </PageContainer>
); );
}; };
...@@ -1196,6 +1196,8 @@ INSERT INTO `dinky_sys_menu` VALUES (28, 9, 'cluster-instance', '/registration/c ...@@ -1196,6 +1196,8 @@ INSERT INTO `dinky_sys_menu` VALUES (28, 9, 'cluster-instance', '/registration/c
INSERT INTO `dinky_sys_menu` VALUES (29, 12, 'group', '/registration/alert/group', './RegCenter/Alert/AlertGroup', NULL, NULL, 'C', 0, NULL, '2023-08-11 14:06:55', '2023-08-11 14:06:55', NULL); INSERT INTO `dinky_sys_menu` VALUES (29, 12, 'group', '/registration/alert/group', './RegCenter/Alert/AlertGroup', NULL, NULL, 'C', 0, NULL, '2023-08-11 14:06:55', '2023-08-11 14:06:55', NULL);
INSERT INTO `dinky_sys_menu` VALUES (30, 9, 'cluster-config', '/registration/cluster/config', './RegCenter/Cluster/Configuration', NULL, NULL, 'C', 0, NULL, '2023-08-11 14:06:55', '2023-08-11 14:06:55', NULL); INSERT INTO `dinky_sys_menu` VALUES (30, 9, 'cluster-config', '/registration/cluster/config', './RegCenter/Cluster/Configuration', NULL, NULL, 'C', 0, NULL, '2023-08-11 14:06:55', '2023-08-11 14:06:55', NULL);
INSERT INTO `dinky_sys_menu` VALUES (31, 12, 'instance', '/registration/alert/instance', './RegCenter/Alert/AlertInstance', NULL, NULL, 'C', 0, NULL, '2023-08-11 14:06:55', '2023-08-11 14:06:55', NULL); INSERT INTO `dinky_sys_menu` VALUES (31, 12, 'instance', '/registration/alert/instance', './RegCenter/Alert/AlertInstance', NULL, NULL, 'C', 0, NULL, '2023-08-11 14:06:55', '2023-08-11 14:06:55', NULL);
INSERT INTO `dinky_sys_menu` VALUES (32, 1, '作业监控', '/home/jobOverView', 'Row', 'show', 'AntCloudOutlined', 'F', 0, NULL, '2023-08-15 16:52:59', '2023-08-15 18:20:23', NULL);
INSERT INTO `dinky_sys_menu` VALUES (33, 1, '数据开发', '/home/devOverView', 'DevOverView', 'show', 'AimOutlined', 'F', 0, NULL, '2023-08-15 16:54:47', '2023-08-15 18:22:49', NULL);
COMMIT; COMMIT;
-- ---------------------------- -- ----------------------------
......
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