index.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 {isNull, 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. hidden: true,
  82. },
  83. {
  84. key: 'mail',
  85. title: '邮箱',
  86. type: 'string',
  87. width: 150
  88. },
  89. {
  90. key: 'password',
  91. title: '密码',
  92. type: 'password',
  93. width: 150,
  94. addable: true,
  95. hidden: true,
  96. },
  97. {
  98. key: 'attributes',
  99. title: '更多属性',
  100. type: 'attributes',
  101. render: (v: string)=>(<UserAttributesText value={v}/>),
  102. hidden: true,
  103. required: false,
  104. },
  105. {
  106. key: 'lastSyncDate',
  107. title: '同步时间',
  108. type: 'string',
  109. editable: false,
  110. addable: false,
  111. },
  112. {
  113. key: 'remark',
  114. title: '备注',
  115. type: 'textarea',
  116. required: false,
  117. }
  118. ]
  119. const toObjKeys = ['givenName','sn', 'objectClass']
  120. const delKeys = ['cn','mail','userPassword','uid']
  121. const parseUser = (user: LDAP.User) => {
  122. const attrs = safeParse(user.attributes, [])
  123. const attrList = []
  124. for (const attr of attrs) {
  125. if (!attr.Name){
  126. continue
  127. }
  128. attr.Value = Array.isArray(attr.Values) ? attr.Values[0] : undefined
  129. if (attr.Name == 'objectClass'){
  130. attr.Value = attr.Values || []
  131. }
  132. if (toObjKeys.includes(attr.Name)){
  133. (user as any)[attr.Name] = attr.Value
  134. }else if (!delKeys.includes(attr.Name)){
  135. attrList.push(attr)
  136. }
  137. }
  138. if (!user.organize){
  139. const index = user.dn.indexOf('ou=')
  140. user.organize = user.dn.substring(index)
  141. }
  142. console.log('attrs', user)
  143. return {
  144. ...user,
  145. attributes: attrList,
  146. } as LDAP.User
  147. }
  148. const stringifyUser = (user: Partial<LDAP.User>) => {
  149. const attributes = []
  150. for (const attr of (user.attributes as any[])) {
  151. if (!attr.Name || isNull(attr.Value)){
  152. continue
  153. }
  154. attributes.push({
  155. Name: attr.Name,
  156. Values: !isNull(attr.Value) ? [attr.Value] : []
  157. })
  158. }
  159. for (const k of toObjKeys){
  160. const v = (user as any)[k]
  161. if (!isNull(v)) {
  162. attributes.push({
  163. Name: k,
  164. Values: Array.isArray(v) ? v: [v]
  165. })
  166. }
  167. }
  168. const cn = `${user.givenName}${user.sn}`
  169. attributes.push({
  170. Name: 'uid',
  171. Values: [user.account]
  172. })
  173. attributes.push({
  174. Name: 'mail',
  175. Values: [user.mail]
  176. })
  177. attributes.push({
  178. Name: 'cn',
  179. Values: [cn],
  180. })
  181. user.dn = `cn=${cn},${user.organize}`
  182. return {
  183. ...user,
  184. attributes: JSON.stringify(attributes),
  185. }
  186. }
  187. const serverConfig: ICurdConfig<LDAP.User> = {
  188. editDialogWidth: 700,
  189. labelSpan: 4,
  190. }
  191. /**
  192. * 用户列表的操作
  193. * @constructor
  194. */
  195. export const List = () => {
  196. const server = useRouteLoaderData("LDAPServerUsers") as LDAP.Server
  197. const [loading, setLoading] = useState(false)
  198. const [success,setSuccess] = useState('')
  199. const getList = useCallback((query: any) => {
  200. console.log('server...', server)
  201. return LDAPApis.getUsers({
  202. ...query,
  203. serverKey: server.key,
  204. }).then(res => {
  205. console.log('server', res)
  206. return res.data.data
  207. })
  208. }, [server])
  209. const getDetail = (data: Partial<LDAP.User>) => {
  210. if (!data.id) {
  211. return Promise.resolve({} as LDAP.User)
  212. }
  213. return LDAPApis.getUserDetail(data.id).then(res => {
  214. if (res.data?.data) {
  215. const user = parseUser(res.data.data);
  216. console.log('user', user)
  217. return user;
  218. }
  219. throw new Error('该服务不存在!')
  220. })
  221. }
  222. const onSaveUser = (data: Partial<LDAP.User>) => {
  223. const user = stringifyUser(data)
  224. user.serverKey = server.key
  225. return LDAPApis.saveUser(user)
  226. .then(res => {
  227. return res.data.data as LDAP.User;
  228. })
  229. }
  230. const syncUsers = () => {
  231. setLoading(true)
  232. LDAPApis.syncUsers({serverKey: server.key})
  233. .then(res => {
  234. console.log('sync users', res)
  235. if (res.data?.data) {
  236. setSuccess(`操作成功:同步:${res.data.data.count}条数据!`)
  237. } else {
  238. Message.warning(res.data.msg || '同步异常!')
  239. }
  240. }).finally(() => {
  241. setLoading(false)
  242. })
  243. }
  244. const operationRender = (record: LDAP.User, _: number) => {
  245. return (<>
  246. <Button size="small">
  247. <Link to={`server/${record.id}/user`}>重置密码</Link>
  248. </Button>
  249. </>)
  250. }
  251. const config = useMemo(() => {
  252. return {
  253. ...serverConfig,
  254. operationRender
  255. } as ICurdConfig<LDAP.User>
  256. }, [])
  257. return (<>
  258. {
  259. success ? (<Alert type="success" message={success} style={{margin: 5}} closable={true} onClose={()=>setSuccess('')}/> ) : null
  260. }
  261. <CurdPage columns={columns} getList={getList} getDetail={getDetail}
  262. operationRender={<>
  263. <Button type="primary" icon={<SyncOutlined/>} onClick={syncUsers} loading={loading}>同步</Button>
  264. </>}
  265. onSave={onSaveUser}
  266. config={config}/>
  267. </>)
  268. }