|
|
@@ -11,40 +11,40 @@ using Novell.Directory.Ldap;
|
|
|
namespace Admin.NET.Core;
|
|
|
|
|
|
/// <summary>
|
|
|
-/// 系统域登录信息配置表服务
|
|
|
+/// 系统域登录配置服务 🧩
|
|
|
/// </summary>
|
|
|
-[ApiDescriptionSettings(Order = 100)]
|
|
|
+[ApiDescriptionSettings(Order = 485)]
|
|
|
public class SysLdapService : IDynamicApiController, ITransient
|
|
|
{
|
|
|
private readonly SqlSugarRepository<SysLdap> _sysLdapRep;
|
|
|
- private readonly SqlSugarRepository<SysUserLdap> _sysUserLdapRep;
|
|
|
|
|
|
- public SysLdapService(SqlSugarRepository<SysLdap> rep, SqlSugarRepository<SysUserLdap> sysUserLdapRep)
|
|
|
+ public SysLdapService(SqlSugarRepository<SysLdap> sysLdapRep)
|
|
|
{
|
|
|
- _sysLdapRep = rep;
|
|
|
- _sysUserLdapRep = sysUserLdapRep;
|
|
|
+ _sysLdapRep = sysLdapRep;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 获取系统域登录信息配置分页列表
|
|
|
+ /// 获取系统域登录配置分页列表 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
+ [DisplayName("获取系统域登录配置分页列表")]
|
|
|
public async Task<SqlSugarPagedList<SysLdap>> Page(SysLdapInput input)
|
|
|
{
|
|
|
return await _sysLdapRep.AsQueryable()
|
|
|
- .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u => u.Host.Contains(input.SearchKey.Trim()))
|
|
|
- .WhereIF(!string.IsNullOrWhiteSpace(input.Host), u => u.Host.Contains(input.Host.Trim()))
|
|
|
+ .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u => u.Host.Contains(input.SearchKey.Trim()))
|
|
|
+ .WhereIF(!string.IsNullOrWhiteSpace(input.Host), u => u.Host.Contains(input.Host.Trim()))
|
|
|
.OrderBy(u => u.CreateTime, OrderByType.Desc)
|
|
|
.ToPagedListAsync(input.Page, input.PageSize);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 增加系统域登录信息配置
|
|
|
+ /// 增加系统域登录配置 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[ApiDescriptionSettings(Name = "Add"), HttpPost]
|
|
|
+ [DisplayName("增加系统域登录配置")]
|
|
|
public async Task<long> Add(AddSysLdapInput input)
|
|
|
{
|
|
|
var entity = input.Adapt<SysLdap>();
|
|
|
@@ -54,11 +54,12 @@ public class SysLdapService : IDynamicApiController, ITransient
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 更新系统域登录信息配置
|
|
|
+ /// 更新系统域登录配置 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[ApiDescriptionSettings(Name = "Update"), HttpPost]
|
|
|
+ [DisplayName("更新系统域登录配置")]
|
|
|
public async Task Update(UpdateSysLdapInput input)
|
|
|
{
|
|
|
var entity = input.Adapt<SysLdap>();
|
|
|
@@ -66,94 +67,194 @@ public class SysLdapService : IDynamicApiController, ITransient
|
|
|
{
|
|
|
entity.BindPass = CryptogramUtil.Encrypt(input.BindPass); // 加密
|
|
|
}
|
|
|
+
|
|
|
await _sysLdapRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 删除系统域登录信息配置
|
|
|
+ /// 删除系统域登录配置 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
|
|
|
+ [DisplayName("删除系统域登录配置")]
|
|
|
public async Task Delete(DeleteSysLdapInput input)
|
|
|
{
|
|
|
var entity = await _sysLdapRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
|
|
|
- await _sysLdapRep.FakeDeleteAsync(entity); // 假删除
|
|
|
- //await _rep.DeleteAsync(entity); // 真删除
|
|
|
+ await _sysLdapRep.FakeDeleteAsync(entity); // 假删除
|
|
|
+ //await _rep.DeleteAsync(entity); // 真删除
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 获取系统域登录信息配置详情
|
|
|
+ /// 获取系统域登录配置详情 🔖
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
- [ApiDescriptionSettings(Name = "Detail")]
|
|
|
+ [DisplayName("获取系统域登录配置详情")]
|
|
|
public async Task<SysLdap> GetDetail([FromQuery] DetailSysLdapInput input)
|
|
|
{
|
|
|
return await _sysLdapRep.GetFirstAsync(u => u.Id == input.Id);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 获取系统域登录信息配置列表
|
|
|
+ /// 获取系统域登录配置列表 🔖
|
|
|
/// </summary>
|
|
|
- /// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
- [ApiDescriptionSettings(Name = "List")]
|
|
|
- public async Task<List<SysLdap>> GetList([FromQuery] SysLdapInput input)
|
|
|
+ [DisplayName("获取系统域登录配置列表")]
|
|
|
+ public async Task<List<SysLdap>> GetList()
|
|
|
{
|
|
|
return await _sysLdapRep.AsQueryable().Select<SysLdap>().ToListAsync();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 账号验证
|
|
|
+ /// 验证账号
|
|
|
/// </summary>
|
|
|
/// <param name="account">域用户</param>
|
|
|
/// <param name="password">密码</param>
|
|
|
/// <param name="tenantId">租户</param>
|
|
|
/// <returns></returns>
|
|
|
[NonAction]
|
|
|
- public async Task<bool> Auth(long tenantId, string account, string password)
|
|
|
+ public async Task<bool> AuthAccount(long tenantId, string account, string password)
|
|
|
{
|
|
|
- var ldap = await _sysLdapRep.GetFirstAsync(u => u.TenantId == tenantId) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
|
|
|
+ var sysLdap = await _sysLdapRep.GetFirstAsync(u => u.TenantId == tenantId) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
|
|
|
var ldapConn = new LdapConnection();
|
|
|
try
|
|
|
{
|
|
|
- ldapConn.Connect(ldap.Host, ldap.Port);
|
|
|
- ldapConn.Bind(ldap.Version, ldap.BindDn, ldap.BindPass);
|
|
|
- var userEntitys = ldapConn.Search(ldap.BaseDn, LdapConnection.ScopeSub, $"{ldap.AuthFilter}={account}", null, false);
|
|
|
+ ldapConn.Connect(sysLdap.Host, sysLdap.Port);
|
|
|
+ ldapConn.Bind(sysLdap.Version, sysLdap.BindDn, sysLdap.BindPass);
|
|
|
+ var ldapSearchResults = ldapConn.Search(sysLdap.BaseDn, LdapConnection.ScopeSub, sysLdap.AuthFilter.Replace("$s", account), null, false);
|
|
|
string dn = string.Empty;
|
|
|
- while (userEntitys.HasMore())
|
|
|
+ while (ldapSearchResults.HasMore())
|
|
|
{
|
|
|
- var entity = userEntitys.Next();
|
|
|
- var sAMAccountName = entity.GetAttribute(ldap.AuthFilter)?.StringValue;
|
|
|
+ var ldapEntry = ldapSearchResults.Next();
|
|
|
+ var sAMAccountName = ldapEntry.GetAttribute(sysLdap.AuthFilter)?.StringValue;
|
|
|
if (!string.IsNullOrEmpty(sAMAccountName))
|
|
|
{
|
|
|
- dn = entity.Dn;
|
|
|
+ dn = ldapEntry.Dn;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
if (string.IsNullOrEmpty(dn)) throw Oops.Oh(ErrorCodeEnum.D1002);
|
|
|
- var attr = new LdapAttribute("userPassword", password);
|
|
|
+ // var attr = new LdapAttribute("userPassword", password);
|
|
|
ldapConn.Bind(dn, password);
|
|
|
}
|
|
|
catch (LdapException e)
|
|
|
{
|
|
|
- switch (e.ResultCode)
|
|
|
+ return e.ResultCode switch
|
|
|
{
|
|
|
- case LdapException.NoSuchObject:
|
|
|
- case LdapException.NoSuchAttribute:
|
|
|
- throw Oops.Oh(ErrorCodeEnum.D0009);
|
|
|
- case LdapException.InvalidCredentials:
|
|
|
- return false;
|
|
|
-
|
|
|
- default:
|
|
|
- throw Oops.Oh(e.Message);
|
|
|
- }
|
|
|
+ LdapException.NoSuchObject or LdapException.NoSuchAttribute => throw Oops.Oh(ErrorCodeEnum.D0009),
|
|
|
+ LdapException.InvalidCredentials => false,
|
|
|
+ _ => throw Oops.Oh(e.Message),
|
|
|
+ };
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
ldapConn.Disconnect();
|
|
|
}
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 同步域用户 🔖
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="input"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [DisplayName("同步域用户")]
|
|
|
+ public async Task SyncUser(SyncSysLdapInput input)
|
|
|
+ {
|
|
|
+ var sysLdap = await _sysLdapRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
|
|
|
+ var ldapConn = new LdapConnection();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ldapConn.Connect(sysLdap.Host, sysLdap.Port);
|
|
|
+ ldapConn.Bind(sysLdap.Version, sysLdap.BindDn, sysLdap.BindPass);
|
|
|
+ var ldapSearchResults = ldapConn.Search(sysLdap.BaseDn, LdapConnection.ScopeOne, "(objectClass=*)", null, false);
|
|
|
+ var userLdapList = new List<SysUserLdap>();
|
|
|
+ while (ldapSearchResults.HasMore())
|
|
|
+ {
|
|
|
+ LdapEntry ldapEntry;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ldapEntry = ldapSearchResults.Next();
|
|
|
+ if (ldapEntry == null) continue;
|
|
|
+ }
|
|
|
+ catch (LdapException)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ var attrs = ldapEntry.GetAttributeSet();
|
|
|
+ if (attrs.Count == 0 || attrs.ContainsKey("OU"))
|
|
|
+ SearchDnLdapUser(ldapConn, sysLdap, userLdapList, ldapEntry.Dn);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var sysUserLdap = new SysUserLdap
|
|
|
+ {
|
|
|
+ Account = !attrs.ContainsKey(sysLdap.BindAttrAccount) ? null : attrs.GetAttribute(sysLdap.BindAttrAccount)?.StringValue,
|
|
|
+ EmployeeId = !attrs.ContainsKey(sysLdap.BindAttrEmployeeId) ? null : attrs.GetAttribute(sysLdap.BindAttrEmployeeId)?.StringValue
|
|
|
+ };
|
|
|
+ if (string.IsNullOrEmpty(sysUserLdap.EmployeeId)) continue;
|
|
|
+ userLdapList.Add(sysUserLdap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (userLdapList.Count == 0)
|
|
|
+ return;
|
|
|
+
|
|
|
+ await App.GetRequiredService<SysUserLdapService>().InsertUserLdaps(sysLdap.TenantId!.Value, userLdapList);
|
|
|
+ }
|
|
|
+ catch (LdapException e)
|
|
|
+ {
|
|
|
+ throw e.ResultCode switch
|
|
|
+ {
|
|
|
+ LdapException.NoSuchObject or LdapException.NoSuchAttribute => Oops.Oh(ErrorCodeEnum.D0009),
|
|
|
+ _ => Oops.Oh(e.Message),
|
|
|
+ };
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ ldapConn.Disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 遍历查询域用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="conn"></param>
|
|
|
+ /// <param name="ldap"></param>
|
|
|
+ /// <param name="userLdapList"></param>
|
|
|
+ /// <param name="baseDn"></param>
|
|
|
+ private static void SearchDnLdapUser(LdapConnection conn, SysLdap ldap, List<SysUserLdap> userLdapList, string baseDn)
|
|
|
+ {
|
|
|
+ var ldapSearchResults = conn.Search(baseDn, LdapConnection.ScopeOne, "(objectClass=*)", null, false);
|
|
|
+ while (ldapSearchResults.HasMore())
|
|
|
+ {
|
|
|
+ LdapEntry ldapEntry;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ldapEntry = ldapSearchResults.Next();
|
|
|
+ if (ldapEntry == null) continue;
|
|
|
+ }
|
|
|
+ catch (LdapException)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ var attrs = ldapEntry.GetAttributeSet();
|
|
|
+ if (attrs.Count == 0 || attrs.ContainsKey("OU"))
|
|
|
+ SearchDnLdapUser(conn, ldap, userLdapList, ldapEntry.Dn);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var sysUserLdap = new SysUserLdap
|
|
|
+ {
|
|
|
+ Account = !attrs.ContainsKey(ldap.BindAttrAccount) ? null : attrs.GetAttribute(ldap.BindAttrAccount)?.StringValue,
|
|
|
+ EmployeeId = !attrs.ContainsKey(ldap.BindAttrEmployeeId) ? null : attrs.GetAttribute(ldap.BindAttrEmployeeId)?.StringValue
|
|
|
+ };
|
|
|
+ if (string.IsNullOrEmpty(sysUserLdap.EmployeeId)) continue;
|
|
|
+ userLdapList.Add(sysUserLdap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|