update permission

This commit is contained in:
LittleBoy 2024-08-10 11:34:07 +08:00
parent e32b3853a3
commit abc538cbc8
6 changed files with 113 additions and 44 deletions

15
config.ts Normal file
View File

@ -0,0 +1,15 @@
export const AppConfig: {
[key:string]: {
ldapApiUrl: string,
ldapApiKey: string
}
} = {
default:{
ldapApiUrl: 'https://test-api.hkchc.team',
ldapApiKey: 'MPCbsNa6l2RJ7D1Zo6D03qtVF1P93st3'
},
production:{
ldapApiUrl: 'https://test-api.hkchc.team',
ldapApiKey: 'MPCbsNa6l2RJ7D1Zo6D03qtVF1P93st3'
}
}

View File

@ -0,0 +1,35 @@
import {useEffect, useState} from "react";
function getRemoteUserNameList() {
return new Promise<string[][]>((resolve, reject) => {
fetch(`${AppConfig.ldapApiUrl}/api/v1/hkchc/user/ldap/get_staff_list`, {
method: 'GET',
headers: {
Apikey: AppConfig.ldapApiKey
},
redirect: 'follow'
})
.then(response => response.json())
.then(ret => {
const result = ret as APIResponse<string[][]>;
if (result.code === 0) {
resolve(result.data!)
} else {
reject(result.message)
}
})
.catch(reject);
})
}
export function useRemoteUserList() {
const [usernameList, setUserList] = useState<string[]>([])
useEffect(()=>{
getRemoteUserNameList().then(data=>{
setUserList(data.flat())
})
},[])
return usernameList
}

View File

@ -1,86 +1,96 @@
import {Card} from "@/components/card"; import {Card} from "@/components/card";
import {useSetState} from "ahooks"; import {useSetState} from "ahooks";
import {Button, Space, TagInput, Toast} from "@douyinfe/semi-ui"; import {Button, Select, Space, Toast} from "@douyinfe/semi-ui";
import {useTranslation} from "react-i18next"; import {useTranslation} from "react-i18next";
import {useEffect} from "react"; import {useEffect, useMemo} from "react";
import {getPermissionList, savePermissionList} from "@/service/api/user.ts"; import {getPermissionList, savePermissionList} from "@/service/api/user.ts";
import {useRemoteUserList} from "@/hooks/useRemoteUserList.ts";
const DEFAULT_ROLES = [ const DEFAULT_ROLES = [
'root','ro','fo' 'root', 'ro', 'fo'
] ]
const Permission = ()=>{ const Permission = () => {
const {t} = useTranslation() const {t} = useTranslation()
const [state,setState] = useSetState<{ const usernameList = useRemoteUserList();
list:PermissionUserList[]; const [state, setState] = useSetState<{
loading?:boolean; list: PermissionUserList[];
loading?: boolean;
}>({ }>({
list:[] list: []
}) })
const onUsernameChange = (role_name:string,username_list: string[])=>{ const onUsernameChange = (role_name: string, username_list: string[]) => {
const index = state.list.findIndex(it=>it.role_name == role_name) const index = state.list.findIndex(it => it.role_name == role_name)
if(index != -1){ if (index != -1) {
state.list[index] = { state.list[index] = {
role_name,username_list role_name, username_list
}; };
setState({ setState({
list:state.list list: state.list
}) })
} }
} }
const saveRoles = ()=>{ const saveRoles = () => {
setState({ setState({
loading:true loading: true
}) })
savePermissionList(state.list).then(()=>{ savePermissionList(state.list).then(() => {
Toast.success({content:`Save Success`,duration: 3,}) Toast.success({content: `Save Success`, duration: 3,})
}).catch(e=>{ }).catch(e => {
Toast.error({ Toast.error({
content:`Save Error:${e.message}`, content: `Save Error:${e.message}`,
duration: 3, duration: 3,
}) })
}).finally(()=>{ }).finally(() => {
setState({loading:false}) setState({loading: false})
}) })
} }
const loadAllPermission = ()=>{ const loadAllPermission = () => {
setState({ setState({
loading:true loading: true
}) })
getPermissionList().then(list=>{ getPermissionList().then(list => {
const roles:{ const roles: {
[key:string]:string[] [key: string]: string[]
} = {} } = {}
// array to object // array to object
list.forEach(it=>{ list.forEach(it => {
roles[it.role_name] = it.username_list roles[it.role_name] = it.username_list
}) })
const permissionList:PermissionUserList[] = []; const permissionList: PermissionUserList[] = [];
DEFAULT_ROLES.forEach(role_name=>{ DEFAULT_ROLES.forEach(role_name => {
permissionList.push({ permissionList.push({
role_name,username_list: roles[role_name] role_name, username_list: roles[role_name]
}) })
}) })
setState({list:permissionList}) setState({list: permissionList})
}).finally(()=>{ }).finally(() => {
setState({loading:false}) setState({loading: false})
}) })
} }
useEffect(loadAllPermission,[]) useEffect(loadAllPermission, [])
const optionList = useMemo(() => (usernameList.map(name => ({label: name, value: name}))), [usernameList])
return (<Card style={{marginBottom: 20}}> return (<Card style={{marginBottom: 20}}>
{state.list.map(it=>(<div key={it.role_name} style={{marginBottom:20}}> {state.list.map(it => (<div key={it.role_name} style={{marginBottom: 20}}>
<div className="permission-title" style={{marginBottom:5}}>{it.role_name.toUpperCase()}</div> <div className="permission-title" style={{marginBottom: 5}}>{it.role_name.toUpperCase()}</div>
<TagInput <Select
defaultValue={it.username_list} style={{width: '100%', backgroundColor: 'var(--semi-color-fill-0)'}}
filter
multiple
size={'large'} size={'large'}
addOnBlur={true} allowDuplicates={false} defaultValue={it.username_list}
placeholder={t('base.please_enter')} optionList={optionList}
onChange={users => onUsernameChange(it.role_name,users)} defaultActiveFirstOption
allowCreate={true}
placeholder={t('base.please_select')}
onChange={(users) => onUsernameChange(it.role_name, users as string[])}
/> />
</div>))} </div>))}
<Space> <Space>
<Button loading={state.loading} onClick={saveRoles} theme={'solid'}>{state.loading ? 'Loading' :t('base.save')}</Button> <Button
loading={state.loading} onClick={saveRoles}
theme={'solid'}>{state.loading ? 'Loading' : t('base.save')}</Button>
{/*<div>{state.message||''}</div>*/} {/*<div>{state.message||''}</div>*/}
</Space> </Space>
</Card>) </Card>)

3
src/vite-env.d.ts vendored
View File

@ -18,8 +18,11 @@ declare const AppConfig: {
SSO_AUTH_CLIENT_KEY: string; SSO_AUTH_CLIENT_KEY: string;
// 登录凭证 token key // 登录凭证 token key
AUTH_TOKEN_KEY: string; AUTH_TOKEN_KEY: string;
ldapApiUrl:string;
ldapApiKey: string;
}; };
declare const AppMode: 'test' | 'production' | 'development'; declare const AppMode: 'test' | 'production' | 'development';
declare const AppMode: 'test' | 'production' | 'development';
declare type BasicComponentProps = { declare type BasicComponentProps = {
children?: React.ReactNode; children?: React.ReactNode;

View File

@ -8,6 +8,7 @@
"strict": true "strict": true
}, },
"include": [ "include": [
"vite.config.ts" "config.ts",
"vite.config.ts",
] ]
} }

View File

@ -1,10 +1,14 @@
import {defineConfig} from 'vite' import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react'
import {resolve} from "path"; import {resolve} from "path";
import {AppConfig} from './config'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig(({mode}) => { export default defineConfig(({mode}) => {
let configs = AppConfig['default'];
if(AppConfig[mode]){
configs = {...configs,...AppConfig[mode]}
}
return { return {
plugins: [react()], plugins: [react()],
base: mode == 'for-wm' ? './' : '/', base: mode == 'for-wm' ? './' : '/',
@ -16,6 +20,7 @@ export default defineConfig(({mode}) => {
SSO_AUTH_URL: process.env.SSO_AUTH_URL || 'https://portal.chuhai.edu.hk', SSO_AUTH_URL: process.env.SSO_AUTH_URL || 'https://portal.chuhai.edu.hk',
SSO_AUTH_CLIENT_KEY: process.env.AUTH_CLIENT_KEY || 'payment', SSO_AUTH_CLIENT_KEY: process.env.AUTH_CLIENT_KEY || 'payment',
AUTH_TOKEN_KEY: process.env.AUTH_TOKEN_KEY || 'payment-auth-token', AUTH_TOKEN_KEY: process.env.AUTH_TOKEN_KEY || 'payment-auth-token',
...configs
}), }),
AppMode: JSON.stringify(mode) AppMode: JSON.stringify(mode)
}, },