index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {CurdColumn, CurdPage} from "../../../../components/curd";
  2. import {useCallback, useMemo, useState} from "react";
  3. import {LDAP, LDAPApis} from "../../../../api/ldap.ts";
  4. import {ICurdConfig} from "../../../../components/curd/types.ts";
  5. import {Alert, Button} from "antd";
  6. import './index.less'
  7. import {Link} from "react-router-dom";
  8. import {useRouteLoaderData} from "react-router";
  9. import {SyncOutlined} from "@ant-design/icons";
  10. import {isNullOrTrue, Message} from "auto-antd";
  11. import {UserAttributesText} from "../components/UserAttributes.tsx";
  12. import {safeParse} from "../../../../utils/json.ts";
  13. const columns: CurdColumn[] = [
  14. {
  15. key: 'id',
  16. title: 'ID',
  17. type: 'string',
  18. disabled: true,
  19. required: false,
  20. width: 50,
  21. addable: false,
  22. },
  23. {
  24. key: 'account',
  25. title: '账号(uid)',
  26. type: 'string',
  27. editable: false,
  28. placeholder: '账号,全局唯一,不可重复',
  29. required: true,
  30. width: 120,
  31. addable: true,
  32. },
  33. {
  34. key: 'givenName',
  35. title: '姓',
  36. type: 'string',
  37. editable: true,
  38. required: true,
  39. width: 120,
  40. hidden: true,
  41. addable: true,
  42. },
  43. {
  44. key: 'sn',
  45. title: '名',
  46. type: 'string',
  47. editable: true,
  48. required: true,
  49. width: 120,
  50. hidden: true,
  51. addable: true,
  52. },
  53. {
  54. key: 'objectClass',
  55. title: 'objectClass',
  56. type: 'select',
  57. editable: true,
  58. required: true,
  59. width: 120,
  60. hidden: true,
  61. addable: true,
  62. option:['top','inetOrgPerson','organizationalPerson','person'],
  63. multiple: true,
  64. },
  65. {
  66. key: 'dn',
  67. title: 'DN',
  68. type: 'string',
  69. placeholder: 'eg. cn=Abc,dc=tonyandmoney,dc=cn',
  70. width: 150,
  71. addable: false,
  72. editable: false,
  73. editHide: true,
  74. },
  75. {
  76. key: 'organize',
  77. title: '上级(DN)',
  78. type: 'string',
  79. placeholder: 'eg. ou=users,dc=tonyandmoney,dc=cn',
  80. width: 150,
  81. },
  82. {
  83. key: 'mail',
  84. title: '邮箱',
  85. type: 'string',
  86. width: 150
  87. },
  88. {
  89. key: 'password',
  90. title: '密码',
  91. type: 'password',
  92. width: 150,
  93. addable: true,
  94. },
  95. {
  96. key: 'attributes',
  97. title: '更多属性',
  98. type: 'attributes',
  99. render: (v: string)=>(<UserAttributesText value={v}/>),
  100. hidden: true,
  101. required: false,
  102. },
  103. {
  104. key: 'remark',
  105. title: '备注',
  106. type: 'textarea',
  107. required: false,
  108. }
  109. ]
  110. const toObjKeys = ['givenName','sn', 'objectClass']
  111. const delKeys = ['cn','mail','userPassword','uid']
  112. const parseUser = (user: LDAP.User) => {
  113. const attrs = safeParse(user.attributes, [])
  114. console.log('attrs', attrs)
  115. const attrList = []
  116. for (const attr of attrs) {
  117. attr.Value = Array.isArray(attr.Values) ? attr.Values[0] : undefined
  118. if (attr.Name == 'objectClass'){
  119. attr.Value = attr.Values || []
  120. }
  121. if (toObjKeys.includes(attr.Name)){
  122. (user as any)[attr.Name] = attr.Value
  123. }else if (!delKeys.includes(attr.Name)){
  124. attrList.push(attr)
  125. }
  126. }
  127. return {
  128. ...user,
  129. attributes: attrList,
  130. } as LDAP.User
  131. }
  132. const stringifyUser = (user: Partial<LDAP.User>) => {
  133. const attributes = []
  134. for (const attr of (user.attributes as any[])) {
  135. attributes.push({
  136. Name: attr.Name,
  137. Values: isNullOrTrue(attr.Value) ? [attr.Values] : []
  138. })
  139. }
  140. for (const k of toObjKeys){
  141. const v = (user as any)[k]
  142. if (isNullOrTrue(v)) {
  143. attributes.push({
  144. Name: k,
  145. Values: [v]
  146. })
  147. }
  148. }
  149. return {
  150. ...user,
  151. attributes: JSON.stringify(attributes),
  152. }
  153. }
  154. const serverConfig: ICurdConfig<LDAP.User> = {
  155. editDialogWidth: 700,
  156. labelSpan: 4,
  157. }
  158. /**
  159. * 用户列表的操作
  160. * @constructor
  161. */
  162. export const List = () => {
  163. const server = useRouteLoaderData("LDAPServerUsers") as LDAP.Server
  164. const [loading, setLoading] = useState(false)
  165. const [success,setSuccess] = useState('')
  166. const getList = useCallback((query: any) => {
  167. console.log('server...', server)
  168. return LDAPApis.getUsers({
  169. ...query,
  170. serverKey: server.key,
  171. }).then(res => {
  172. console.log('server', res)
  173. return res.data.data
  174. })
  175. }, [server])
  176. const getDetail = (data: Partial<LDAP.User>) => {
  177. if (!data.id) {
  178. return Promise.resolve({} as LDAP.User)
  179. }
  180. return LDAPApis.getUserDetail(data.id).then(res => {
  181. if (res.data?.data) {
  182. const user = parseUser(res.data.data);
  183. console.log('user', user)
  184. return user;
  185. }
  186. throw new Error('该服务不存在!')
  187. })
  188. }
  189. const onSaveUser = (data: Partial<LDAP.User>) => {
  190. const user = stringifyUser(data)
  191. if (data.id){
  192. return LDAPApis.saveUser(user)
  193. .then(res => {
  194. return res.data.data as LDAP.User;
  195. })
  196. }
  197. return LDAPApis.add(user)
  198. .then(res => {
  199. return res.data.data as LDAP.User;
  200. })
  201. }
  202. const syncUsers = () => {
  203. setLoading(true)
  204. LDAPApis.syncUsers({serverKey: server.key})
  205. .then(res => {
  206. console.log('sync users', res)
  207. if (res.data?.data) {
  208. setSuccess(`操作成功:同步:${res.data.data.count}条数据!`)
  209. } else {
  210. Message.warning(res.data.msg || '同步异常!')
  211. }
  212. }).finally(() => {
  213. setLoading(false)
  214. })
  215. }
  216. const operationRender = (record: LDAP.User, _: number) => {
  217. return (<>
  218. <Button size="small">
  219. <Link to={`server/${record.id}/user`}>重置密码</Link>
  220. </Button>
  221. </>)
  222. }
  223. const config = useMemo(() => {
  224. return {
  225. ...serverConfig,
  226. operationRender
  227. } as ICurdConfig<LDAP.User>
  228. }, [])
  229. return (<>
  230. {
  231. success ? (<Alert type="success" message={success} style={{margin: 5}} closable={true} onClose={()=>setSuccess('')}/> ) : null
  232. }
  233. <CurdPage columns={columns} getList={getList} getDetail={getDetail}
  234. operationRender={<>
  235. <Button type="primary" icon={<SyncOutlined/>} onClick={syncUsers} loading={loading}>同步</Button>
  236. </>}
  237. onSave={onSaveUser}
  238. config={config}/>
  239. </>)
  240. }