123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import {CurdColumn, CurdPage} from "../../../../components/curd";
- import {useCallback, useMemo, useState} from "react";
- import {LDAP, LDAPApis} from "../../../../api/ldap.ts";
- import {ICurdConfig} from "../../../../components/curd/types.ts";
- import {Alert, Button} from "antd";
- import './index.less'
- import {Link} from "react-router-dom";
- import {useRouteLoaderData} from "react-router";
- import {SyncOutlined} from "@ant-design/icons";
- import {isNullOrTrue, Message} from "auto-antd";
- import {UserAttributesText} from "../components/UserAttributes.tsx";
- import {safeParse} from "../../../../utils/json.ts";
- const columns: CurdColumn[] = [
- {
- key: 'id',
- title: 'ID',
- type: 'string',
- disabled: true,
- required: false,
- width: 50,
- addable: false,
- },
- {
- key: 'account',
- title: '账号(uid)',
- type: 'string',
- editable: false,
- placeholder: '账号,全局唯一,不可重复',
- required: true,
- width: 120,
- addable: true,
- },
- {
- key: 'givenName',
- title: '姓',
- type: 'string',
- editable: true,
- required: true,
- width: 120,
- hidden: true,
- addable: true,
- },
- {
- key: 'sn',
- title: '名',
- type: 'string',
- editable: true,
- required: true,
- width: 120,
- hidden: true,
- addable: true,
- },
- {
- key: 'objectClass',
- title: 'objectClass',
- type: 'select',
- editable: true,
- required: true,
- width: 120,
- hidden: true,
- addable: true,
- option:['top','inetOrgPerson','organizationalPerson','person'],
- multiple: true,
- },
- {
- key: 'dn',
- title: 'DN',
- type: 'string',
- placeholder: 'eg. cn=Abc,dc=tonyandmoney,dc=cn',
- width: 150,
- addable: false,
- editable: false,
- editHide: true,
- },
- {
- key: 'organize',
- title: '上级(DN)',
- type: 'string',
- placeholder: 'eg. ou=users,dc=tonyandmoney,dc=cn',
- width: 150,
- },
- {
- key: 'mail',
- title: '邮箱',
- type: 'string',
- width: 150
- },
- {
- key: 'password',
- title: '密码',
- type: 'password',
- width: 150,
- addable: true,
- },
- {
- key: 'attributes',
- title: '更多属性',
- type: 'attributes',
- render: (v: string)=>(<UserAttributesText value={v}/>),
- hidden: true,
- required: false,
- },
- {
- key: 'remark',
- title: '备注',
- type: 'textarea',
- required: false,
- }
- ]
- const toObjKeys = ['givenName','sn', 'objectClass']
- const delKeys = ['cn','mail','userPassword','uid']
- const parseUser = (user: LDAP.User) => {
- const attrs = safeParse(user.attributes, [])
- console.log('attrs', attrs)
- const attrList = []
- for (const attr of attrs) {
- attr.Value = Array.isArray(attr.Values) ? attr.Values[0] : undefined
- if (attr.Name == 'objectClass'){
- attr.Value = attr.Values || []
- }
- if (toObjKeys.includes(attr.Name)){
- (user as any)[attr.Name] = attr.Value
- }else if (!delKeys.includes(attr.Name)){
- attrList.push(attr)
- }
- }
- return {
- ...user,
- attributes: attrList,
- } as LDAP.User
- }
- const stringifyUser = (user: Partial<LDAP.User>) => {
- const attributes = []
- for (const attr of (user.attributes as any[])) {
- attributes.push({
- Name: attr.Name,
- Values: isNullOrTrue(attr.Value) ? [attr.Values] : []
- })
- }
- for (const k of toObjKeys){
- const v = (user as any)[k]
- if (isNullOrTrue(v)) {
- attributes.push({
- Name: k,
- Values: [v]
- })
- }
- }
- return {
- ...user,
- attributes: JSON.stringify(attributes),
- }
- }
- const serverConfig: ICurdConfig<LDAP.User> = {
- editDialogWidth: 700,
- labelSpan: 4,
- }
- /**
- * 用户列表的操作
- * @constructor
- */
- export const List = () => {
- const server = useRouteLoaderData("LDAPServerUsers") as LDAP.Server
- const [loading, setLoading] = useState(false)
- const [success,setSuccess] = useState('')
- const getList = useCallback((query: any) => {
- console.log('server...', server)
- return LDAPApis.getUsers({
- ...query,
- serverKey: server.key,
- }).then(res => {
- console.log('server', res)
- return res.data.data
- })
- }, [server])
- const getDetail = (data: Partial<LDAP.User>) => {
- if (!data.id) {
- return Promise.resolve({} as LDAP.User)
- }
- return LDAPApis.getUserDetail(data.id).then(res => {
- if (res.data?.data) {
- const user = parseUser(res.data.data);
- console.log('user', user)
- return user;
- }
- throw new Error('该服务不存在!')
- })
- }
- const onSaveUser = (data: Partial<LDAP.User>) => {
- const user = stringifyUser(data)
- if (data.id){
- return LDAPApis.saveUser(user)
- .then(res => {
- return res.data.data as LDAP.User;
- })
- }
- return LDAPApis.add(user)
- .then(res => {
- return res.data.data as LDAP.User;
- })
- }
- const syncUsers = () => {
- setLoading(true)
- LDAPApis.syncUsers({serverKey: server.key})
- .then(res => {
- console.log('sync users', res)
- if (res.data?.data) {
- setSuccess(`操作成功:同步:${res.data.data.count}条数据!`)
- } else {
- Message.warning(res.data.msg || '同步异常!')
- }
- }).finally(() => {
- setLoading(false)
- })
- }
- const operationRender = (record: LDAP.User, _: number) => {
- return (<>
- <Button size="small">
- <Link to={`server/${record.id}/user`}>重置密码</Link>
- </Button>
- </>)
- }
- const config = useMemo(() => {
- return {
- ...serverConfig,
- operationRender
- } as ICurdConfig<LDAP.User>
- }, [])
- return (<>
- {
- success ? (<Alert type="success" message={success} style={{margin: 5}} closable={true} onClose={()=>setSuccess('')}/> ) : null
- }
- <CurdPage columns={columns} getList={getList} getDetail={getDetail}
- operationRender={<>
- <Button type="primary" icon={<SyncOutlined/>} onClick={syncUsers} loading={loading}>同步</Button>
- </>}
- onSave={onSaveUser}
- config={config}/>
- </>)
- }
|