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 {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'
'root', 'ro', 'fo'
]
const Permission = ()=>{
const Permission = () => {
const {t} = useTranslation()
const [state,setState] = useSetState<{
list:PermissionUserList[];
loading?:boolean;
const usernameList = useRemoteUserList();
const [state, setState] = useSetState<{
list: PermissionUserList[];
loading?: boolean;
}>({
list:[]
list: []
})
const onUsernameChange = (role_name:string,username_list: string[])=>{
const index = state.list.findIndex(it=>it.role_name == role_name)
if(index != -1){
const onUsernameChange = (role_name: string, username_list: string[]) => {
const index = state.list.findIndex(it => it.role_name == role_name)
if (index != -1) {
state.list[index] = {
role_name,username_list
role_name, username_list
};
setState({
list:state.list
list: state.list
})
}
}
const saveRoles = ()=>{
const saveRoles = () => {
setState({
loading:true
loading: true
})
savePermissionList(state.list).then(()=>{
Toast.success({content:`Save Success`,duration: 3,})
}).catch(e=>{
savePermissionList(state.list).then(() => {
Toast.success({content: `Save Success`, duration: 3,})
}).catch(e => {
Toast.error({
content:`Save Error:${e.message}`,
content: `Save Error:${e.message}`,
duration: 3,
})
}).finally(()=>{
setState({loading:false})
}).finally(() => {
setState({loading: false})
})
}
const loadAllPermission = ()=>{
const loadAllPermission = () => {
setState({
loading:true
loading: true
})
getPermissionList().then(list=>{
const roles:{
[key:string]:string[]
getPermissionList().then(list => {
const roles: {
[key: string]: string[]
} = {}
// array to object
list.forEach(it=>{
list.forEach(it => {
roles[it.role_name] = it.username_list
})
const permissionList:PermissionUserList[] = [];
DEFAULT_ROLES.forEach(role_name=>{
const permissionList: PermissionUserList[] = [];
DEFAULT_ROLES.forEach(role_name => {
permissionList.push({
role_name,username_list: roles[role_name]
role_name, username_list: roles[role_name]
})
})
setState({list:permissionList})
}).finally(()=>{
setState({loading:false})
setState({list: permissionList})
}).finally(() => {
setState({loading: false})
})
}
useEffect(loadAllPermission,[])
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}
{state.list.map(it => (<div key={it.role_name} style={{marginBottom: 20}}>
<div className="permission-title" style={{marginBottom: 5}}>{it.role_name.toUpperCase()}</div>
<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)
},