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 @@
"lodash": "^4.17.21",
"million": "^2.5.1-beta.3",
"moment": "^2.29.4",
"monaco-editor": "^0.41.0",
"omit.js": "^2.0.2",
"rc-menu": "^9.8.4",
"rc-util": "^5.33.0",
......
......@@ -32,6 +32,7 @@ import {persistStore, persistReducer} from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import {Reducer, StoreEnhancer} from 'redux';
import { API } from "./services/data";
import {AccessContextProvider} from '@/hooks/useAccess';
// const isDev = process.env.NODE_ENV === "development";
const loginPath = API_CONSTANTS.LOGIN_PATH;
......@@ -146,9 +147,9 @@ export const layout: RunTimeLayoutConfig = ({initialState}) => {
childrenRender: (children) => {
if (initialState?.loading) return <PageLoading/>;
return (
<>
<AccessContextProvider currentUser={initialState?.currentUser}>
{children}
</>
</AccessContextProvider>
);
},
...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 @@
* limitations under the License.
*
*/
import { Authorized } from '@/hooks/useAccess';
import DevOverView from '@/pages/Home/DevOverView';
import JobOverView from '@/pages/Home/JobOverView';
import { PageContainer } from '@ant-design/pro-components';
......@@ -24,16 +25,20 @@ import { Col, Row } from 'antd';
export default () => {
return (
<PageContainer title={false}>
<Row style={{ marginTop: '5px', marginBottom: '10px' }}>
<Col span={24}>
<JobOverView />
</Col>
</Row>
<Row>
<Col span={24}>
<DevOverView />
</Col>
</Row>
<Authorized path="/home/jobOverView">
<Row style={{ marginTop: '5px', marginBottom: '10px' }}>
<Col span={24}>
<JobOverView />
</Col>
</Row>
</Authorized>
<Authorized path="/home/devOverView">
<Row>
<Col span={24}>
<DevOverView />
</Col>
</Row>
</Authorized>
</PageContainer>
);
};
......@@ -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 (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 (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;
-- ----------------------------
......
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