@@ -186,15 +186,13 @@ public async Task SyncUser(SyncSysLdapInput input)
186
186
}
187
187
188
188
var attrs = ldapEntry . GetAttributeSet ( ) ;
189
+ string deptCode = GetDepartmentCode ( attrs , sysLdap . BindAttrCode ) ;
189
190
if ( attrs . Count == 0 || attrs . ContainsKey ( "OU" ) )
190
- SearchDnLdapUser ( ldapConn , sysLdap , userLdapList , ldapEntry . Dn ) ;
191
+ SearchDnLdapUser ( ldapConn , sysLdap , userLdapList , ldapEntry . Dn , deptCode ) ;
191
192
else
192
193
{
193
- var sysUserLdap = new SysUserLdap
194
- {
195
- Account = ! attrs . ContainsKey ( sysLdap . BindAttrAccount ) ? null : attrs . GetAttribute ( sysLdap . BindAttrAccount ) ? . StringValue ,
196
- EmployeeId = ! attrs . ContainsKey ( sysLdap . BindAttrEmployeeId ) ? null : attrs . GetAttribute ( sysLdap . BindAttrEmployeeId ) ? . StringValue
197
- } ;
194
+ var sysUserLdap = CreateSysUserLdap ( attrs , sysLdap . BindAttrAccount , sysLdap . BindAttrEmployeeId , deptCode ) ;
195
+
198
196
if ( string . IsNullOrEmpty ( sysUserLdap . EmployeeId ) ) continue ;
199
197
userLdapList . Add ( sysUserLdap ) ;
200
198
}
@@ -219,16 +217,48 @@ public async Task SyncUser(SyncSysLdapInput input)
219
217
}
220
218
}
221
219
220
+ /// <summary>
221
+ /// 获取部门代码
222
+ /// </summary>
223
+ /// <param name="attrs"></param>
224
+ /// <param name="bindAttrCode"></param>
225
+ /// <returns></returns>
226
+ private static string GetDepartmentCode ( LdapAttributeSet attrs , string bindAttrCode )
227
+ {
228
+ return bindAttrCode == "objectGUID"
229
+ ? new Guid ( attrs . GetAttribute ( bindAttrCode ) ? . ByteValue ) . ToString ( )
230
+ : attrs . GetAttribute ( bindAttrCode ) ? . StringValue ?? "0" ;
231
+ }
232
+
233
+ /// <summary>
234
+ /// 创建同步对象
235
+ /// </summary>
236
+ /// <param name="attrs"></param>
237
+ /// <param name="bindAttrAccount"></param>
238
+ /// <param name="bindAttrEmployeeId"></param>
239
+ /// <param name="deptCode"></param>
240
+ /// <returns></returns>
241
+ private static SysUserLdap CreateSysUserLdap ( LdapAttributeSet attrs , string bindAttrAccount , string bindAttrEmployeeId , string deptCode )
242
+ {
243
+ return new SysUserLdap
244
+ {
245
+ Account = ! attrs . ContainsKey ( bindAttrAccount ) ? null : attrs . GetAttribute ( bindAttrAccount ) ? . StringValue ,
246
+ EmployeeId = ! attrs . ContainsKey ( bindAttrEmployeeId ) ? null : attrs . GetAttribute ( bindAttrEmployeeId ) ? . StringValue ,
247
+ DeptCode = deptCode
248
+ } ;
249
+ }
250
+
222
251
/// <summary>
223
252
/// 遍历查询域用户
224
253
/// </summary>
225
- /// <param name="conn "></param>
226
- /// <param name="ldap "></param>
254
+ /// <param name="ldapConn "></param>
255
+ /// <param name="sysLdap "></param>
227
256
/// <param name="userLdapList"></param>
228
257
/// <param name="baseDn"></param>
229
- private static void SearchDnLdapUser ( LdapConnection conn , SysLdap ldap , List < SysUserLdap > userLdapList , string baseDn )
258
+ /// <param name="deptCode"></param>
259
+ private static void SearchDnLdapUser ( LdapConnection ldapConn , SysLdap sysLdap , List < SysUserLdap > userLdapList , string baseDn , string deptCode )
230
260
{
231
- var ldapSearchResults = conn . Search ( baseDn , LdapConnection . ScopeOne , "(objectClass=*)" , null , false ) ;
261
+ var ldapSearchResults = ldapConn . Search ( baseDn , LdapConnection . ScopeOne , "(objectClass=*)" , null , false ) ;
232
262
while ( ldapSearchResults . HasMore ( ) )
233
263
{
234
264
LdapEntry ldapEntry ;
@@ -243,18 +273,131 @@ private static void SearchDnLdapUser(LdapConnection conn, SysLdap ldap, List<Sys
243
273
}
244
274
245
275
var attrs = ldapEntry . GetAttributeSet ( ) ;
276
+ deptCode = GetDepartmentCode ( attrs , sysLdap . BindAttrCode ) ;
277
+
246
278
if ( attrs . Count == 0 || attrs . ContainsKey ( "OU" ) )
247
- SearchDnLdapUser ( conn , ldap , userLdapList , ldapEntry . Dn ) ;
279
+ SearchDnLdapUser ( ldapConn , sysLdap , userLdapList , ldapEntry . Dn , deptCode ) ;
248
280
else
249
281
{
250
- var sysUserLdap = new SysUserLdap
251
- {
252
- Account = ! attrs . ContainsKey ( ldap . BindAttrAccount ) ? null : attrs . GetAttribute ( ldap . BindAttrAccount ) ? . StringValue ,
253
- EmployeeId = ! attrs . ContainsKey ( ldap . BindAttrEmployeeId ) ? null : attrs . GetAttribute ( ldap . BindAttrEmployeeId ) ? . StringValue
254
- } ;
282
+ var sysUserLdap = CreateSysUserLdap ( attrs , sysLdap . BindAttrAccount , sysLdap . BindAttrEmployeeId , deptCode ) ;
283
+
255
284
if ( string . IsNullOrEmpty ( sysUserLdap . EmployeeId ) ) continue ;
256
285
userLdapList . Add ( sysUserLdap ) ;
257
286
}
258
287
}
259
288
}
289
+
290
+ /// <summary>
291
+ /// 同步域组织 🔖
292
+ /// </summary>
293
+ /// <param name="input"></param>
294
+ /// <returns></returns>
295
+ [ DisplayName ( "同步域组织" ) ]
296
+ public async Task SyncDept ( SyncSysLdapInput input )
297
+ {
298
+ var sysLdap = await _sysLdapRep . GetFirstAsync ( u => u . Id == input . Id ) ?? throw Oops . Oh ( ErrorCodeEnum . D1002 ) ;
299
+ var ldapConn = new LdapConnection ( ) ;
300
+ try
301
+ {
302
+ ldapConn . Connect ( sysLdap . Host , sysLdap . Port ) ;
303
+ ldapConn . Bind ( sysLdap . Version , sysLdap . BindDn , sysLdap . BindPass ) ;
304
+ var ldapSearchResults = ldapConn . Search ( sysLdap . BaseDn , LdapConnection . ScopeOne , "(objectClass=*)" , null , false ) ;
305
+ var listOrgs = new List < SysOrg > ( ) ;
306
+ while ( ldapSearchResults . HasMore ( ) )
307
+ {
308
+ LdapEntry ldapEntry ;
309
+ try
310
+ {
311
+ ldapEntry = ldapSearchResults . Next ( ) ;
312
+ if ( ldapEntry == null ) continue ;
313
+ }
314
+ catch ( LdapException )
315
+ {
316
+ continue ;
317
+ }
318
+
319
+ var attrs = ldapEntry . GetAttributeSet ( ) ;
320
+ if ( attrs . Count == 0 || attrs . ContainsKey ( "OU" ) )
321
+ {
322
+ var sysOrg = CreateSysOrg ( attrs , sysLdap , listOrgs , new SysOrg { Id = 0 , Level = 0 } ) ;
323
+ listOrgs . Add ( sysOrg ) ;
324
+
325
+ SearchDnLdapDept ( ldapConn , sysLdap , listOrgs , ldapEntry . Dn , sysOrg ) ;
326
+ }
327
+ }
328
+
329
+ if ( listOrgs . Count == 0 )
330
+ return ;
331
+
332
+ await App . GetRequiredService < SysOrgService > ( ) . BatchAddOrgs ( listOrgs ) ;
333
+ }
334
+ catch ( LdapException e )
335
+ {
336
+ throw e . ResultCode switch
337
+ {
338
+ LdapException . NoSuchObject or LdapException . NoSuchAttribute => Oops . Oh ( ErrorCodeEnum . D0009 ) ,
339
+ _ => Oops . Oh ( e . Message ) ,
340
+ } ;
341
+ }
342
+ finally
343
+ {
344
+ ldapConn . Disconnect ( ) ;
345
+ }
346
+ }
347
+
348
+ /// <summary>
349
+ /// 遍历查询域用户
350
+ /// </summary>
351
+ /// <param name="ldapConn"></param>
352
+ /// <param name="sysLdap"></param>
353
+ /// <param name="listOrgs"></param>
354
+ /// <param name="baseDn"></param>
355
+ /// <param name="org"></param>
356
+ private static void SearchDnLdapDept ( LdapConnection ldapConn , SysLdap sysLdap , List < SysOrg > listOrgs , string baseDn , SysOrg org )
357
+ {
358
+ var ldapSearchResults = ldapConn . Search ( baseDn , LdapConnection . ScopeOne , "(objectClass=*)" , null , false ) ;
359
+ while ( ldapSearchResults . HasMore ( ) )
360
+ {
361
+ LdapEntry ldapEntry ;
362
+ try
363
+ {
364
+ ldapEntry = ldapSearchResults . Next ( ) ;
365
+ if ( ldapEntry == null ) continue ;
366
+ }
367
+ catch ( LdapException )
368
+ {
369
+ continue ;
370
+ }
371
+
372
+ var attrs = ldapEntry . GetAttributeSet ( ) ;
373
+ if ( attrs . Count == 0 || attrs . ContainsKey ( "OU" ) )
374
+ {
375
+ var sysOrg = CreateSysOrg ( attrs , sysLdap , listOrgs , org ) ;
376
+ listOrgs . Add ( sysOrg ) ;
377
+
378
+ SearchDnLdapDept ( ldapConn , sysLdap , listOrgs , ldapEntry . Dn , sysOrg ) ;
379
+ }
380
+ }
381
+ }
382
+
383
+ /// <summary>
384
+ /// 创建架构对象
385
+ /// </summary>
386
+ /// <param name="attrs"></param>
387
+ /// <param name="sysLdap"></param>
388
+ /// <param name="listOrgs"></param>
389
+ /// <param name="org"></param>
390
+ /// <returns></returns>
391
+ private static SysOrg CreateSysOrg ( LdapAttributeSet attrs , SysLdap sysLdap , List < SysOrg > listOrgs , SysOrg org )
392
+ {
393
+ return new SysOrg
394
+ {
395
+ Pid = org . Id ,
396
+ Id = YitIdHelper . NextId ( ) ,
397
+ Code = ! attrs . ContainsKey ( sysLdap . BindAttrCode ) ? null : new Guid ( attrs . GetAttribute ( sysLdap . BindAttrCode ) ? . ByteValue ) . ToString ( ) ,
398
+ Level = org . Level + 1 ,
399
+ Name = ! attrs . ContainsKey ( sysLdap . BindAttrAccount ) ? null : attrs . GetAttribute ( sysLdap . BindAttrAccount ) ? . StringValue ,
400
+ OrderNo = listOrgs . Count + 1 ,
401
+ } ;
402
+ }
260
403
}
0 commit comments