From e311ebaf3d0a90186ae222ad4b482add2f29750e Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Fri, 7 Aug 2020 12:38:05 +0800 Subject: [PATCH] Refactor some code of Ldap. --- .../src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj | 2 +- .../Volo/Abp/Ldap/AbpLdapModule.cs | 6 +- .../Volo/Abp/Ldap/LdapManager.cs | 88 +++++++++---------- .../Volo.Abp.Ldap.Tests.csproj | 3 - .../Volo/Abp/Ldap/AbpLdapTestModule.cs | 51 +++++++++++ .../Volo/Abp/Ldap/Authenticate_Tests.cs | 7 +- .../Volo/Abp/Ldap/LdapManager_Tests.cs | 58 +----------- 7 files changed, 99 insertions(+), 116 deletions(-) create mode 100644 framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs diff --git a/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj b/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj index 229f3c6c7d..5339a68da1 100644 --- a/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj +++ b/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj @@ -19,7 +19,7 @@ - + diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs index b35e1b1d59..216eee81b4 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs @@ -1,12 +1,8 @@ using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Autofac; using Volo.Abp.Modularity; namespace Volo.Abp.Ldap { - [DependsOn( - typeof(AbpAutofacModule) - )] public class AbpLdapModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) @@ -15,4 +11,4 @@ namespace Volo.Abp.Ldap Configure(configuration.GetSection("LDAP")); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs index 9e0b8f04a7..77d8d04d91 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs @@ -13,9 +13,8 @@ namespace Volo.Abp.Ldap { public class LdapManager : ILdapManager, ITransientDependency { - private readonly string _searchBase; - private readonly AbpLdapOptions _ldapOptions; - private readonly IHybridServiceScopeFactory _hybridServiceScopeFactory; + protected AbpLdapOptions LdapOptions { get; } + protected IHybridServiceScopeFactory HybridServiceScopeFactory { get; } private readonly string[] _attributes = { @@ -26,40 +25,39 @@ namespace Volo.Abp.Ldap public LdapManager(IOptions ldapSettingsOptions, IHybridServiceScopeFactory hybridServiceScopeFactory) { - _hybridServiceScopeFactory = hybridServiceScopeFactory; - _ldapOptions = ldapSettingsOptions.Value; - _searchBase = _ldapOptions.SearchBase; + HybridServiceScopeFactory = hybridServiceScopeFactory; + LdapOptions = ldapSettingsOptions.Value; } #region Organization /// /// query the specified organizations. - /// + /// /// filter: (&(name=xxx)(objectClass=organizationalUnit)) when name is not null /// filter: (&(objectClass=organizationalUnit)) when name is null - /// + /// /// /// /// - public IList GetOrganizations(string name = null) + public virtual IList GetOrganizations(string name = null) { var conditions = new Dictionary { {"name", name}, {"objectClass", "organizationalUnit"}, }; - return Query(_searchBase, conditions); + return Query(LdapOptions.SearchBase, conditions); } /// /// query the specified organization. - /// + /// /// filter: (&(distinguishedName=xxx)(objectClass=organizationalUnit)) when organizationName is not null - /// + /// /// /// /// - public LdapOrganization GetOrganization(string distinguishedName) + public virtual LdapOrganization GetOrganization(string distinguishedName) { distinguishedName = Check.NotNullOrWhiteSpace(distinguishedName, nameof(distinguishedName)); var conditions = new Dictionary @@ -67,17 +65,17 @@ namespace Volo.Abp.Ldap {"distinguishedName", distinguishedName}, {"objectClass", "organizationalUnit"}, }; - return QueryOne(_searchBase, conditions); + return QueryOne(LdapOptions.SearchBase, conditions); } - public void AddSubOrganization(string organizationName, LdapOrganization parentOrganization) + public virtual void AddSubOrganization(string organizationName, LdapOrganization parentOrganization) { organizationName = Check.NotNullOrWhiteSpace(organizationName, nameof(organizationName)); var dn = $"OU={organizationName},{parentOrganization.DistinguishedName}"; var attributeSet = new LdapAttributeSet { - new LdapAttribute("objectCategory", $"CN=Organizational-Unit,CN=Schema,CN=Configuration,{_ldapOptions.DomainDistinguishedName}"), + new LdapAttribute("objectCategory", $"CN=Organizational-Unit,CN=Schema,CN=Configuration,{LdapOptions.DomainDistinguishedName}"), new LdapAttribute("objectClass", new[] {"top", "organizationalUnit"}), new LdapAttribute("name", organizationName), }; @@ -90,7 +88,7 @@ namespace Volo.Abp.Ldap } } - public void AddSubOrganization(string organizationName, string parentDistinguishedName) + public virtual void AddSubOrganization(string organizationName, string parentDistinguishedName) { organizationName = Check.NotNullOrWhiteSpace(organizationName, nameof(organizationName)); parentDistinguishedName = @@ -110,7 +108,7 @@ namespace Volo.Abp.Ldap #region User /// /// query the specified users. - /// + /// /// filter: (&(name=xxx)(objectCategory=person)(objectClass=user)) when name is not null /// filter: (&(objectCategory=person)(objectClass=user)) when name is null /// @@ -119,13 +117,13 @@ namespace Volo.Abp.Ldap /// /// filter: (&(cn=xxx)(objectCategory=person)(objectClass=user)) when commonName is not null /// filter: (&(objectCategory=person)(objectClass=user)) when commonName is null - /// + /// /// /// /// /// /// - public IList GetUsers(string name = null, string displayName = null, string commonName = null) + public virtual IList GetUsers(string name = null, string displayName = null, string commonName = null) { var conditions = new Dictionary { @@ -135,18 +133,18 @@ namespace Volo.Abp.Ldap {"displayName", displayName}, {"cn", commonName}, }; - return Query(_searchBase, conditions); + return Query(LdapOptions.SearchBase, conditions); } /// /// query the specified User. - /// + /// /// filter: (&(distinguishedName=xxx)(objectCategory=person)(objectClass=user)) when distinguishedName is not null - /// + /// /// /// /// - public LdapUser GetUser(string distinguishedName) + public virtual LdapUser GetUser(string distinguishedName) { distinguishedName = Check.NotNullOrWhiteSpace(distinguishedName, nameof(distinguishedName)); var conditions = new Dictionary @@ -155,19 +153,19 @@ namespace Volo.Abp.Ldap {"objectClass", "user"}, {"distinguishedName", distinguishedName}, }; - return QueryOne(_searchBase, conditions); + return QueryOne(LdapOptions.SearchBase, conditions); } - public void AddUserToOrganization(string userName, string password, LdapOrganization parentOrganization) + public virtual void AddUserToOrganization(string userName, string password, LdapOrganization parentOrganization) { var dn = $"CN={userName},{parentOrganization.DistinguishedName}"; - var mail = $"{userName}@{_ldapOptions.DomainName}"; - sbyte[] encodedBytes = SupportClass.ToSByteArray(Encoding.Unicode.GetBytes($"\"{password}\"")); + var mail = $"{userName}@{LdapOptions.DomainName}"; + var encodedBytes = SupportClass.ToSByteArray(Encoding.Unicode.GetBytes($"\"{password}\"")); var attributeSet = new LdapAttributeSet { new LdapAttribute("instanceType", "4"), - new LdapAttribute("objectCategory", $"CN=Person,CN=Schema,CN=Configuration,{_ldapOptions.DomainDistinguishedName}"), + new LdapAttribute("objectCategory", $"CN=Person,CN=Schema,CN=Configuration,{LdapOptions.DomainDistinguishedName}"), new LdapAttribute("objectClass", new[] {"top", "person", "organizationalPerson", "user"}), new LdapAttribute("name", userName), new LdapAttribute("cn", userName), @@ -187,16 +185,16 @@ namespace Volo.Abp.Ldap } } - public void AddUserToOrganization(string userName, string password, string parentDistinguishedName) + public virtual void AddUserToOrganization(string userName, string password, string parentDistinguishedName) { var dn = $"CN={userName},{parentDistinguishedName}"; - var mail = $"{userName}@{_ldapOptions.DomainName}"; + var mail = $"{userName}@{LdapOptions.DomainName}"; sbyte[] encodedBytes = SupportClass.ToSByteArray(Encoding.Unicode.GetBytes($"\"{password}\"")); var attributeSet = new LdapAttributeSet { new LdapAttribute("instanceType", "4"), - new LdapAttribute("objectCategory", $"CN=Person,CN=Schema,CN=Configuration,{_ldapOptions.DomainDistinguishedName}"), + new LdapAttribute("objectCategory", $"CN=Person,CN=Schema,CN=Configuration,{LdapOptions.DomainDistinguishedName}"), new LdapAttribute("objectClass", new[] {"top", "person", "organizationalPerson", "user"}), new LdapAttribute("name", userName), new LdapAttribute("cn", userName), @@ -221,12 +219,12 @@ namespace Volo.Abp.Ldap #region Authenticate /// - /// Authenticate + /// Authenticate /// /// E.g administrator@yourdomain.com.cn /// /// - public bool Authenticate(string userDomainName, string password) + public virtual bool Authenticate(string userDomainName, string password) { try { @@ -237,7 +235,7 @@ namespace Volo.Abp.Ldap } catch (Exception ex) { - using (var scope = _hybridServiceScopeFactory.CreateScope()) + using (var scope = HybridServiceScopeFactory.CreateScope()) { scope.ServiceProvider .GetRequiredService() @@ -250,20 +248,20 @@ namespace Volo.Abp.Ldap #endregion - private ILdapConnection GetConnection(string bindUserName = null, string bindUserPassword = null) + protected virtual ILdapConnection GetConnection(string bindUserName = null, string bindUserPassword = null) { // bindUserName/bindUserPassword only be used when authenticate - bindUserName = bindUserName ?? _ldapOptions.Credentials.DomainUserName; - bindUserPassword = bindUserPassword ?? _ldapOptions.Credentials.Password; + bindUserName = bindUserName ?? LdapOptions.Credentials.DomainUserName; + bindUserPassword = bindUserPassword ?? LdapOptions.Credentials.Password; - var ldapConnection = new LdapConnection() { SecureSocketLayer = _ldapOptions.UseSsl }; - if (_ldapOptions.UseSsl) + var ldapConnection = new LdapConnection() { SecureSocketLayer = LdapOptions.UseSsl }; + if (LdapOptions.UseSsl) { ldapConnection.UserDefinedServerCertValidationDelegate += (sender, certificate, chain, sslPolicyErrors) => true; } - ldapConnection.Connect(_ldapOptions.ServerHost, _ldapOptions.ServerPort); + ldapConnection.Connect(LdapOptions.ServerHost, LdapOptions.ServerPort); - if (_ldapOptions.UseSsl) + if (LdapOptions.UseSsl) { ldapConnection.Bind(LdapConnection.Ldap_V3, bindUserName, bindUserPassword); } @@ -274,7 +272,7 @@ namespace Volo.Abp.Ldap return ldapConnection; } - private IList Query(string searchBase, Dictionary conditions) where T : class, ILdapEntry + protected virtual IList Query(string searchBase, Dictionary conditions) where T : class, ILdapEntry { var filter = LdapHelps.BuildFilter(conditions); @@ -307,7 +305,7 @@ namespace Volo.Abp.Ldap return result; } - private T QueryOne(string searchBase, Dictionary conditions) where T : class, ILdapEntry + protected virtual T QueryOne(string searchBase, Dictionary conditions) where T : class, ILdapEntry { var filter = LdapHelps.BuildFilter(conditions); @@ -340,4 +338,4 @@ namespace Volo.Abp.Ldap } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.Ldap.Tests/Volo.Abp.Ldap.Tests.csproj b/framework/test/Volo.Abp.Ldap.Tests/Volo.Abp.Ldap.Tests.csproj index 3c1f9c96db..cea5e86688 100644 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo.Abp.Ldap.Tests.csproj +++ b/framework/test/Volo.Abp.Ldap.Tests/Volo.Abp.Ldap.Tests.csproj @@ -9,9 +9,6 @@ - - - diff --git a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs new file mode 100644 index 0000000000..f3932e0a05 --- /dev/null +++ b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs @@ -0,0 +1,51 @@ +using Volo.Abp.Modularity; + +namespace Volo.Abp.Ldap +{ + [DependsOn(typeof(AbpLdapModule))] + public class AbpLdapTestModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + // not use ssl + // "LDAP": { + // "ServerHost": "192.168.101.54", + // "ServerPort": 389, + // "UseSSL": false, + // "Credentials": { + // "DomainUserName": "administrator@yourdomain.com.cn", + // "Password": "yH.20190528" + // }, + // "SearchBase": "CN=Users,DC=yourdomain,DC=com,DC=cn", + // "DomainName": "yourdomain.com.cn", + // "DomainDistinguishedName": "DC=yourdomain,DC=com,DC=cn" + // } + + // use ssl + // "LDAP": { + // "ServerHost": "192.168.101.54", + // "ServerPort": 636, + // "UseSSL": true, + // "Credentials": { + // "DomainUserName": "administrator@yourdomain.com.cn", + // "Password": "yH.20190528" + // }, + // "SearchBase": "CN=Users,DC=yourdomain,DC=com,DC=cn", + // "DomainName": "yourdomain.com.cn", + // "DomainDistinguishedName": "DC=yourdomain,DC=com,DC=cn" + // } + + Configure(settings => + { + settings.ServerHost = "192.168.101.54"; + settings.ServerPort = 636; + settings.UseSsl = true; + settings.Credentials.DomainUserName = "administrator@yourdomain.com.cn"; + settings.Credentials.Password = "yH.20190528"; + settings.SearchBase = "DC=yourdomain,DC=com,DC=cn"; + settings.DomainName = "yourdomain.com.cn"; + settings.DomainDistinguishedName = "DC=yourdomain,DC=com,DC=cn"; + }); + } + } +} diff --git a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/Authenticate_Tests.cs b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/Authenticate_Tests.cs index bf11262a99..46471893ce 100644 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/Authenticate_Tests.cs +++ b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/Authenticate_Tests.cs @@ -6,13 +6,8 @@ using Xunit; namespace Volo.Abp.Ldap { - public class Authenticate_Tests : AbpIntegratedTest { - protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) - { - options.UseAutofac(); - } private readonly ILdapManager _ldapManager; private readonly LdapTestData _testData; @@ -68,4 +63,4 @@ namespace Volo.Abp.Ldap } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapManager_Tests.cs b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapManager_Tests.cs index 3e83d26543..20382e5577 100644 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapManager_Tests.cs +++ b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapManager_Tests.cs @@ -1,25 +1,17 @@ using System; using Shouldly; -using Volo.Abp.Modularity; using Volo.Abp.Testing; using Xunit; namespace Volo.Abp.Ldap { - - public class LdapManager_Tests : AbpIntegratedTest + public class LdapManager_Tests : AbpIntegratedTest { - protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) - { - options.UseAutofac(); - } - private readonly ILdapManager _ldapManager; private readonly LdapTestData _testData; public LdapManager_Tests() { - // ReSharper disable once VirtualMemberCallInConstructor _testData = GetRequiredService(); _ldapManager = GetRequiredService(); } @@ -195,52 +187,6 @@ namespace Volo.Abp.Ldap result.ShouldNotBeNull(); result.ShouldContain(e=>e.Name == randomName); } - - [DependsOn(typeof(AbpLdapModule))] - public class TestModule : AbpModule - { - public override void ConfigureServices(ServiceConfigurationContext context) - { - // not use ssl - // "LDAP": { - // "ServerHost": "192.168.101.54", - // "ServerPort": 389, - // "UseSSL": false, - // "Credentials": { - // "DomainUserName": "administrator@yourdomain.com.cn", - // "Password": "yH.20190528" - // }, - // "SearchBase": "CN=Users,DC=yourdomain,DC=com,DC=cn", - // "DomainName": "yourdomain.com.cn", - // "DomainDistinguishedName": "DC=yourdomain,DC=com,DC=cn" - // } - - // use ssl - // "LDAP": { - // "ServerHost": "192.168.101.54", - // "ServerPort": 636, - // "UseSSL": true, - // "Credentials": { - // "DomainUserName": "administrator@yourdomain.com.cn", - // "Password": "yH.20190528" - // }, - // "SearchBase": "CN=Users,DC=yourdomain,DC=com,DC=cn", - // "DomainName": "yourdomain.com.cn", - // "DomainDistinguishedName": "DC=yourdomain,DC=com,DC=cn" - // } - Configure(settings => - { - settings.ServerHost = "192.168.101.54"; - settings.ServerPort = 636; - settings.UseSsl = true; - settings.Credentials.DomainUserName = "administrator@yourdomain.com.cn"; - settings.Credentials.Password = "yH.20190528"; - settings.SearchBase = "DC=yourdomain,DC=com,DC=cn"; - settings.DomainName = "yourdomain.com.cn"; - settings.DomainDistinguishedName = "DC=yourdomain,DC=com,DC=cn"; - }); - } - } } -} \ No newline at end of file +}