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,9 +1,10 @@
import {Card} from "@/components/card";
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 {useEffect} from "react";
import {useEffect, useMemo} from "react";
import {getPermissionList, savePermissionList} from "@/service/api/user.ts";
import {useRemoteUserList} from "@/hooks/useRemoteUserList.ts";
const DEFAULT_ROLES = [
'root', 'ro', 'fo'
@ -11,6 +12,7 @@ const DEFAULT_ROLES = [
const Permission = () => {
const {t} = useTranslation()
const usernameList = useRemoteUserList();
const [state, setState] = useSetState<{
list: PermissionUserList[];
loading?: boolean;
@ -67,20 +69,28 @@ const Permission = ()=>{
})
}
useEffect(loadAllPermission, [])
const optionList = useMemo(() => (usernameList.map(name => ({label: name, value: name}))), [usernameList])
return (<Card 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>
<TagInput
defaultValue={it.username_list}
<Select
style={{width: '100%', backgroundColor: 'var(--semi-color-fill-0)'}}
filter
multiple
size={'large'}
addOnBlur={true} allowDuplicates={false}
placeholder={t('base.please_enter')}
onChange={users => onUsernameChange(it.role_name,users)}
defaultValue={it.username_list}
optionList={optionList}
defaultActiveFirstOption
allowCreate={true}
placeholder={t('base.please_select')}
onChange={(users) => onUsernameChange(it.role_name, users as string[])}
/>
</div>))}
<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>*/}
</Space>
</Card>)

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

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

View File

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

View File

@ -1,10 +1,14 @@
import {defineConfig} from 'vite'
import react from '@vitejs/plugin-react'
import {resolve} from "path";
import {AppConfig} from './config'
// https://vitejs.dev/config/
export default defineConfig(({mode}) => {
let configs = AppConfig['default'];
if(AppConfig[mode]){
configs = {...configs,...AppConfig[mode]}
}
return {
plugins: [react()],
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_CLIENT_KEY: process.env.AUTH_CLIENT_KEY || 'payment',
AUTH_TOKEN_KEY: process.env.AUTH_TOKEN_KEY || 'payment-auth-token',
...configs
}),
AppMode: JSON.stringify(mode)
},