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 1/6] 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 +} From d24d4c8403852eebb414b48f80fa567b8359bee2 Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Fri, 7 Aug 2020 14:31:20 +0800 Subject: [PATCH 2/6] Add LdapSettingProvider. --- .../src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj | 10 ++- .../Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs | 44 ++++++++++++ .../Volo/Abp/Ldap/AbpLdapModule.cs | 31 ++++++++- .../Volo/Abp/Ldap/LdapManager.cs | 4 +- .../Volo/Abp/Ldap/LdapSettingNames.cs | 24 +++++++ .../Volo/Abp/Ldap/LdapSettingProvider.cs | 67 +++++++++++++++++++ .../Abp/Ldap/Localization/LdapResource.cs | 10 +++ .../Volo/Abp/Ldap/Localization/en.json | 28 ++++++++ .../Volo/Abp/Ldap/Localization/tr.json | 28 ++++++++ .../Volo/Abp/Ldap/Localization/zh-Hans.json | 28 ++++++++ .../Volo/Abp/Ldap/Localization/zh-Hant.json | 28 ++++++++ 11 files changed, 298 insertions(+), 4 deletions(-) create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/LdapResource.cs create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json create mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json diff --git a/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj b/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj index 5339a68da1..850bd29303 100644 --- a/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj +++ b/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj @@ -17,9 +17,17 @@ - + + + + + + + + + diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs new file mode 100644 index 0000000000..8e744d414e --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Volo.Abp.Options; +using Volo.Abp.Settings; +using Volo.Abp.Threading; + +namespace Volo.Abp.Ldap +{ + public class AbpAbpLdapOptionsFactory : AbpOptionsFactory + { + protected ISettingProvider SettingProvider { get; } + + public AbpAbpLdapOptionsFactory( + IEnumerable> setups, + IEnumerable> postConfigures, + ISettingProvider settingProvider) + : base(setups, postConfigures) + { + SettingProvider = settingProvider; + } + + public override AbpLdapOptions Create(string name) + { + var options = base.Create(name); + + AsyncHelper.RunSync(() => OverrideOptionsAsync(options)); + + return options; + } + + protected virtual async Task OverrideOptionsAsync(AbpLdapOptions options) + { + options.ServerHost = await SettingProvider.GetOrNullAsync(LdapSettingNames.ServerHost) ?? options.ServerHost; + options.ServerPort = await SettingProvider.GetAsync(LdapSettingNames.ServerPort, options.ServerPort); + options.UseSsl = await SettingProvider.GetAsync(LdapSettingNames.UseSsl, options.UseSsl); + options.SearchBase = await SettingProvider.GetOrNullAsync(LdapSettingNames.SearchBase) ?? options.SearchBase; + options.DomainName = await SettingProvider.GetOrNullAsync(LdapSettingNames.DomainName) ?? options.DomainName; + options.DomainDistinguishedName = await SettingProvider.GetOrNullAsync(LdapSettingNames.DomainDistinguishedName) ?? options.DomainDistinguishedName; + options.Credentials.DomainUserName = await SettingProvider.GetOrNullAsync(LdapSettingNames.Credentials.DomainUserName) ?? options.Credentials.DomainUserName; + options.Credentials.Password = await SettingProvider.GetOrNullAsync(LdapSettingNames.Credentials.Password) ?? options.Credentials.Password; + } + } +} 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 216eee81b4..3978a1b1c1 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs @@ -1,14 +1,43 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Options; +using Volo.Abp.Ldap.Localization; +using Volo.Abp.Localization; using Volo.Abp.Modularity; +using Volo.Abp.Settings; +using Volo.Abp.VirtualFileSystem; namespace Volo.Abp.Ldap { + [DependsOn( + typeof(AbpSettingsModule), + typeof(AbpVirtualFileSystemModule), + typeof(AbpLocalizationModule))] public class AbpLdapModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.Replace(ServiceDescriptor.Transient, AbpAbpLdapOptionsFactory>()); + context.Services.Replace(ServiceDescriptor.Scoped, OptionsManager>()); + var configuration = context.Services.GetConfiguration(); - Configure(configuration.GetSection("LDAP")); + var ldapConfiguration = configuration["LDAP"]; + if (!ldapConfiguration.IsNullOrEmpty()) + { + Configure(configuration.GetSection("LDAP")); + } + + Configure(options => + { + options.FileSets.AddEmbedded(); + }); + + Configure(options => + { + options.Resources + .Add("en") + .AddVirtualJson("/Volo/Abp/Ldap/Localization"); + }); } } } 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 77d8d04d91..acd7996c32 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs @@ -251,8 +251,8 @@ namespace Volo.Abp.Ldap 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 ??= LdapOptions.Credentials.DomainUserName; + bindUserPassword ??= LdapOptions.Credentials.Password; var ldapConnection = new LdapConnection() { SecureSocketLayer = LdapOptions.UseSsl }; if (LdapOptions.UseSsl) diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs new file mode 100644 index 0000000000..cfd851c12f --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs @@ -0,0 +1,24 @@ +namespace Volo.Abp.Ldap +{ + public static class LdapSettingNames + { + public const string ServerHost = "Abp.Ldap.ServerHost"; + + public const string ServerPort = "Abp.Ldap.ServerPort"; + + public const string UseSsl = "Abp.Ldap.UseSsl"; + + public const string SearchBase = "Abp.Ldap.SearchBase"; + + public const string DomainName = "Abp.Ldap.DomainName"; + + public const string DomainDistinguishedName = "Abp.Ldap.DomainDistinguishedName"; + + public static class Credentials + { + public const string DomainUserName = "Abp.Ldap.Credentials.DomainUserName"; + + public const string Password = "Abp.Ldap.Credentials.Password"; + } + } +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs new file mode 100644 index 0000000000..73dad5d73d --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs @@ -0,0 +1,67 @@ +using Volo.Abp.Ldap.Localization; +using Volo.Abp.Localization; +using Volo.Abp.Settings; + +namespace Volo.Abp.Ldap +{ + public class LdapSettingProvider : SettingDefinitionProvider + { + public override void Define(ISettingDefinitionContext context) + { + context.Add( + new SettingDefinition( + LdapSettingNames.ServerHost, + "", + L("DisplayName:Abp.Ldap.ServerHost"), + L("Description:Abp.Ldap.ServerHost")), + + new SettingDefinition( + LdapSettingNames.ServerPort, + "", + L("DisplayName:Abp.Ldap.ServerPort"), + L("Description:Abp.Ldap.ServerPort")), + + new SettingDefinition( + LdapSettingNames.UseSsl, + "", + L("DisplayName:Abp.Ldap.UseSsl"), + L("Description:Abp.Ldap.UseSsl")), + + new SettingDefinition( + LdapSettingNames.SearchBase, + "", + L("DisplayName:Abp.Ldap.SearchBase"), + L("Description:Abp.Ldap.SearchBase")), + + new SettingDefinition( + LdapSettingNames.DomainName, + "", + L("DisplayName:Abp.Ldap.DomainName"), + L("Description:Abp.Ldap.DomainName")), + + new SettingDefinition( + LdapSettingNames.DomainDistinguishedName, + "", + L("DisplayName:Abp.Ldap.DomainDistinguishedName"), + L("Description:Abp.Ldap.DomainDistinguishedName")), + + new SettingDefinition( + LdapSettingNames.Credentials.DomainUserName, + "", + L("DisplayName:Abp.Ldap.Credentials.DomainUserName"), + L("Description:Abp.Ldap.Credentials.DomainUserName")), + + new SettingDefinition( + LdapSettingNames.Credentials.Password, + "", + L("DisplayName:Abp.Ldap.Credentials.Password"), + L("Description:Abp.Ldap.Credentials.Password")) + ); + } + + private static LocalizableString L(string name) + { + return LocalizableString.Create(name); + } + } +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/LdapResource.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/LdapResource.cs new file mode 100644 index 0000000000..e45b6797b5 --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/LdapResource.cs @@ -0,0 +1,10 @@ +using Volo.Abp.Localization; + +namespace Volo.Abp.Ldap.Localization +{ + [LocalizationResourceName("AbpLdap")] + public class LdapResource + { + + } +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json new file mode 100644 index 0000000000..973e059684 --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json @@ -0,0 +1,28 @@ +{ + "culture": "en", + "texts": { + "DisplayName:Abp.Ldap.ServerHost": "ServerHost", + "Description:Abp.Ldap.ServerHost": "ServerHost", + + "DisplayName:Abp.Ldap.ServerPort": "ServerPort", + "Description:Abp.Ldap.ServerPort": "ServerPort", + + "DisplayName:Abp.Ldap.UseSsl": "UseSsl", + "Description:Abp.Ldap.UseSsl": "UseSsl", + + "DisplayName:Abp.Ldap.SearchBase": "SearchBase", + "Description:Abp.Ldap.SearchBase": "SearchBase", + + "DisplayName:Abp.Ldap.DomainName": "DomainName", + "Description:Abp.Ldap.DomainName": "DomainName", + + "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + + "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + + "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", + "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + } +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json new file mode 100644 index 0000000000..eac7a9dbec --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json @@ -0,0 +1,28 @@ +{ + "culture": "tr", + "texts": { + "DisplayName:Abp.Ldap.ServerHost": "ServerHost", + "Description:Abp.Ldap.ServerHost": "ServerHost", + + "DisplayName:Abp.Ldap.ServerPort": "ServerPort", + "Description:Abp.Ldap.ServerPort": "ServerPort", + + "DisplayName:Abp.Ldap.UseSsl": "UseSsl", + "Description:Abp.Ldap.UseSsl": "UseSsl", + + "DisplayName:Abp.Ldap.SearchBase": "SearchBase", + "Description:Abp.Ldap.SearchBase": "SearchBase", + + "DisplayName:Abp.Ldap.DomainName": "DomainName", + "Description:Abp.Ldap.DomainName": "DomainName", + + "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + + "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + + "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", + "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + } +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json new file mode 100644 index 0000000000..94c0df37c1 --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json @@ -0,0 +1,28 @@ +{ + "culture": "zh-Hans", + "texts": { + "DisplayName:Abp.Ldap.ServerHost": "ServerHost", + "Description:Abp.Ldap.ServerHost": "ServerHost", + + "DisplayName:Abp.Ldap.ServerPort": "ServerPort", + "Description:Abp.Ldap.ServerPort": "ServerPort", + + "DisplayName:Abp.Ldap.UseSsl": "UseSsl", + "Description:Abp.Ldap.UseSsl": "UseSsl", + + "DisplayName:Abp.Ldap.SearchBase": "SearchBase", + "Description:Abp.Ldap.SearchBase": "SearchBase", + + "DisplayName:Abp.Ldap.DomainName": "DomainName", + "Description:Abp.Ldap.DomainName": "DomainName", + + "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + + "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + + "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", + "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + } +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json new file mode 100644 index 0000000000..9822a9b6dc --- /dev/null +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json @@ -0,0 +1,28 @@ +{ + "culture": "zh-Hant", + "texts": { + "DisplayName:Abp.Ldap.ServerHost": "ServerHost", + "Description:Abp.Ldap.ServerHost": "ServerHost", + + "DisplayName:Abp.Ldap.ServerPort": "ServerPort", + "Description:Abp.Ldap.ServerPort": "ServerPort", + + "DisplayName:Abp.Ldap.UseSsl": "UseSsl", + "Description:Abp.Ldap.UseSsl": "UseSsl", + + "DisplayName:Abp.Ldap.SearchBase": "SearchBase", + "Description:Abp.Ldap.SearchBase": "SearchBase", + + "DisplayName:Abp.Ldap.DomainName": "DomainName", + "Description:Abp.Ldap.DomainName": "DomainName", + + "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", + + "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", + + "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", + "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + } +} From 4c2aa3a72c532886dad3613fb81dfc99060a150c Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Fri, 7 Aug 2020 14:40:13 +0800 Subject: [PATCH 3/6] Fix build error. --- framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 3978a1b1c1..189af6eb41 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using Volo.Abp.Ldap.Localization; From ed77133847968a660a4e673cf50b0e3c24706749 Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Fri, 7 Aug 2020 21:22:06 +0800 Subject: [PATCH 4/6] Refactor the Volo.Abp.Ldap library. --- .../src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj | 2 +- .../Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs | 19 +- .../Volo/Abp/Ldap/AbpLdapModule.cs | 4 +- .../Volo/Abp/Ldap/AbpLdapOptions.cs | 17 +- .../OrganizationNotExistException.cs | 11 - .../Volo/Abp/Ldap/ILdapManager.cs | 67 +--- .../Volo/Abp/Ldap/LdapCredentials.cs | 9 - .../Volo.Abp.Ldap/Volo/Abp/Ldap/LdapHelps.cs | 34 -- .../Volo/Abp/Ldap/LdapManager.cs | 318 ++---------------- .../Volo/Abp/Ldap/LdapSettingNames.cs | 15 +- .../Volo/Abp/Ldap/LdapSettingProvider.cs | 39 +-- .../Volo/Abp/Ldap/Modeling/ILdapEntry.cs | 11 - .../Abp/Ldap/Modeling/ILdapOrganization.cs | 7 - .../Volo/Abp/Ldap/Modeling/ILdapUser.cs | 11 - .../Volo/Abp/Ldap/Modeling/LdapEntryBase.cs | 24 -- .../Abp/Ldap/Modeling/LdapOrganization.cs | 17 - .../Volo/Abp/Ldap/Modeling/LdapUser.cs | 25 -- framework/src/Volo.Abp.Ldap/readme.md | 171 ---------- .../Volo.Abp.Ldap.Tests.csproj | 1 + .../Volo/Abp/Ldap/AbpLdapTestModule.cs | 46 +-- .../Volo/Abp/Ldap/Authenticate_Tests.cs | 66 ---- .../Volo/Abp/Ldap/LdapHelps_Tests.cs | 84 ----- .../Volo/Abp/Ldap/LdapManager_Tests.cs | 179 +--------- .../Volo/Abp/Ldap/LdapOptions_Tests.cs | 21 ++ .../Volo/Abp/Ldap/LdapTestData.cs | 27 -- 25 files changed, 85 insertions(+), 1140 deletions(-) delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Exceptions/OrganizationNotExistException.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapCredentials.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapHelps.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapEntry.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapOrganization.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapUser.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapEntryBase.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapOrganization.cs delete mode 100644 framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapUser.cs delete mode 100644 framework/src/Volo.Abp.Ldap/readme.md delete mode 100644 framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/Authenticate_Tests.cs delete mode 100644 framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapHelps_Tests.cs create mode 100644 framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapOptions_Tests.cs delete mode 100644 framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapTestData.cs diff --git a/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj b/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj index 850bd29303..849fe3ce3d 100644 --- a/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj +++ b/framework/src/Volo.Abp.Ldap/Volo.Abp.Ldap.csproj @@ -15,7 +15,7 @@ - + diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs index 8e744d414e..c3d3549a12 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Volo.Abp.Options; @@ -31,14 +32,16 @@ namespace Volo.Abp.Ldap protected virtual async Task OverrideOptionsAsync(AbpLdapOptions options) { - options.ServerHost = await SettingProvider.GetOrNullAsync(LdapSettingNames.ServerHost) ?? options.ServerHost; + options.ServerHost = await GetStringValueOrDefault(LdapSettingNames.ServerHost) ?? options.ServerHost; options.ServerPort = await SettingProvider.GetAsync(LdapSettingNames.ServerPort, options.ServerPort); - options.UseSsl = await SettingProvider.GetAsync(LdapSettingNames.UseSsl, options.UseSsl); - options.SearchBase = await SettingProvider.GetOrNullAsync(LdapSettingNames.SearchBase) ?? options.SearchBase; - options.DomainName = await SettingProvider.GetOrNullAsync(LdapSettingNames.DomainName) ?? options.DomainName; - options.DomainDistinguishedName = await SettingProvider.GetOrNullAsync(LdapSettingNames.DomainDistinguishedName) ?? options.DomainDistinguishedName; - options.Credentials.DomainUserName = await SettingProvider.GetOrNullAsync(LdapSettingNames.Credentials.DomainUserName) ?? options.Credentials.DomainUserName; - options.Credentials.Password = await SettingProvider.GetOrNullAsync(LdapSettingNames.Credentials.Password) ?? options.Credentials.Password; + options.UserName = await GetStringValueOrDefault(LdapSettingNames.UserName) ?? options.UserName; + options.Password = await GetStringValueOrDefault(LdapSettingNames.Password) ?? options.Password; + } + + protected virtual async Task GetStringValueOrDefault(string name, string defaultValue = default) + { + var value = await SettingProvider.GetOrNullAsync(LdapSettingNames.ServerHost); + return value.IsNullOrWhiteSpace() ? defaultValue : value; } } } 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 189af6eb41..c842d4d76b 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapModule.cs @@ -22,10 +22,10 @@ namespace Volo.Abp.Ldap context.Services.Replace(ServiceDescriptor.Scoped, OptionsManager>()); var configuration = context.Services.GetConfiguration(); - var ldapConfiguration = configuration["LDAP"]; + var ldapConfiguration = configuration["Ldap"]; if (!ldapConfiguration.IsNullOrEmpty()) { - Configure(configuration.GetSection("LDAP")); + Configure(configuration.GetSection("Ldap")); } Configure(options => diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapOptions.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapOptions.cs index 671d623ff7..38768581f4 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapOptions.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpLdapOptions.cs @@ -6,19 +6,8 @@ public int ServerPort { get; set; } - public bool UseSsl { get; set; } + public string UserName { get; set; } - public string SearchBase { get; set; } - - public string DomainName { get; set; } - - public string DomainDistinguishedName { get; set; } - - public LdapCredentials Credentials { get; set; } - - public AbpLdapOptions() - { - Credentials = new LdapCredentials(); - } + public string Password { get; set; } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Exceptions/OrganizationNotExistException.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Exceptions/OrganizationNotExistException.cs deleted file mode 100644 index 9d7fb220cc..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Exceptions/OrganizationNotExistException.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Volo.Abp.Ldap.Exceptions -{ - public class OrganizationNotExistException : BusinessException - { - public OrganizationNotExistException(string distinguishedName) - : base("LDAP:000001", $"the organization distinguished named {distinguishedName} does not exist.") - { - - } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/ILdapManager.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/ILdapManager.cs index f14e88b409..1943157fa8 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/ILdapManager.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/ILdapManager.cs @@ -1,72 +1,17 @@ -using System.Collections.Generic; -using Volo.Abp.Ldap.Modeling; - -namespace Volo.Abp.Ldap +namespace Volo.Abp.Ldap { public interface ILdapManager { /// - /// query the specified organizations. - /// - /// filter: (&(name=xxx)(objectClass=organizationalUnit)) when name is not null - /// filter: (&(name=*)(objectClass=organizationalUnit)) when name is null - /// - /// - /// - /// - IList GetOrganizations(string name = null); - - /// - /// query the specified organization. - /// - /// filter: (&(distinguishedName=xxx)(objectClass=organizationalUnit)) when organizationName is not null - /// + /// Authenticate with default username/password /// - /// /// - LdapOrganization GetOrganization(string distinguishedName); - - void AddSubOrganization(string organizationName, LdapOrganization parentOrganization); - void AddSubOrganization(string organizationName, string parentDistinguishedName); - - /// - /// query the specified users. - /// - /// filter: (&(name=xxx)(objectCategory=person)(objectClass=user)) when name is not null - /// filter: (&(name=*)(objectCategory=person)(objectClass=user)) when name is null - /// - /// filter: (&(displayName=xxx)(objectCategory=person)(objectClass=user)) when displayName is not null - /// filter: (&(displayName=*)(objectCategory=person)(objectClass=user)) when displayName is null - /// - /// filter: (&(cn=xxx)(objectCategory=person)(objectClass=user)) when commonName is not null - /// filter: (&(cn=*)(objectCategory=person)(objectClass=user)) when commonName is null - /// - /// - /// - /// - /// - /// - IList GetUsers(string name = null, string displayName = null, string commonName = null); - - /// - /// query the specified User. - /// - /// filter: (&(distinguishedName=xxx)(objectCategory=person)(objectClass=user)) when distinguishedName is not null - /// - /// - /// - /// - LdapUser GetUser(string distinguishedName); - - void AddUserToOrganization(string userName, string password, LdapOrganization parentOrganization); - void AddUserToOrganization(string userName, string password, string parentDistinguishedName); + bool Authenticate(); /// - /// Authenticate + /// Authenticate with specified username/password /// - /// E.g administrator@yourdomain.com.cn - /// /// - bool Authenticate(string userDomainName, string password); + bool Authenticate(string username, string password); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapCredentials.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapCredentials.cs deleted file mode 100644 index a2d2eefd74..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapCredentials.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Volo.Abp.Ldap -{ - public class LdapCredentials - { - public string DomainUserName { get; set; } - - public string Password { get; set; } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapHelps.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapHelps.cs deleted file mode 100644 index 49a60a964f..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapHelps.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; - -namespace Volo.Abp.Ldap -{ - public static class LdapHelps - { - public static string BuildCondition(string name, string value) - { - return string.IsNullOrWhiteSpace(value) ? "" : $"({name}={value})"; - } - - public static string BuildFilter(Dictionary conditions) - { - if (null == conditions ) - { - conditions = new Dictionary(); - } - - if (conditions.Keys.Count == 0) - { - conditions.Add("objectClass", "*"); // add default condition - } - - var subFilter = string.Empty; - foreach (var keyValuePair in conditions) - { - subFilter += BuildCondition(keyValuePair.Key, keyValuePair.Value); - } - - return $"(&{subFilter})"; - } - - } -} \ 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 acd7996c32..eeb7ce88b2 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs @@ -1,341 +1,61 @@ using System; using Microsoft.Extensions.Options; using Novell.Directory.Ldap; -using System.Collections.Generic; -using System.Text; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; -using Volo.Abp.ExceptionHandling; -using Volo.Abp.Ldap.Exceptions; -using Volo.Abp.Ldap.Modeling; namespace Volo.Abp.Ldap { public class LdapManager : ILdapManager, ITransientDependency { + public ILogger Logger { get; set; } protected AbpLdapOptions LdapOptions { get; } - protected IHybridServiceScopeFactory HybridServiceScopeFactory { get; } - private readonly string[] _attributes = + public LdapManager(IOptions ldapSettingsOptions) { - "objectCategory", "objectClass", "cn", "name", "distinguishedName", - "ou", - "sAMAccountName", "userPrincipalName", "telephoneNumber", "mail" - }; - - public LdapManager(IOptions ldapSettingsOptions, IHybridServiceScopeFactory hybridServiceScopeFactory) - { - 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 virtual IList GetOrganizations(string name = null) - { - var conditions = new Dictionary - { - {"name", name}, - {"objectClass", "organizationalUnit"}, - }; - return Query(LdapOptions.SearchBase, conditions); - } - - /// - /// query the specified organization. - /// - /// filter: (&(distinguishedName=xxx)(objectClass=organizationalUnit)) when organizationName is not null - /// - /// - /// - /// - public virtual LdapOrganization GetOrganization(string distinguishedName) - { - distinguishedName = Check.NotNullOrWhiteSpace(distinguishedName, nameof(distinguishedName)); - var conditions = new Dictionary - { - {"distinguishedName", distinguishedName}, - {"objectClass", "organizationalUnit"}, - }; - return QueryOne(LdapOptions.SearchBase, conditions); - } - - 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("objectClass", new[] {"top", "organizationalUnit"}), - new LdapAttribute("name", organizationName), - }; - - var newEntry = new LdapEntry(dn, attributeSet); - - using (var ldapConnection = GetConnection()) - { - ldapConnection.Add(newEntry); - } - } - - public virtual void AddSubOrganization(string organizationName, string parentDistinguishedName) - { - organizationName = Check.NotNullOrWhiteSpace(organizationName, nameof(organizationName)); - parentDistinguishedName = - Check.NotNullOrWhiteSpace(parentDistinguishedName, nameof(parentDistinguishedName)); - - var parentOrganization = GetOrganization(parentDistinguishedName); - if (null == parentOrganization) - { - throw new OrganizationNotExistException(parentDistinguishedName); - } - - AddSubOrganization(organizationName, parentOrganization); - } - #endregion - - #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 - /// - /// filter: (&(displayName=xxx)(objectCategory=person)(objectClass=user)) when displayName is not null - /// filter: (&(objectCategory=person)(objectClass=user)) when displayName is null - /// - /// filter: (&(cn=xxx)(objectCategory=person)(objectClass=user)) when commonName is not null - /// filter: (&(objectCategory=person)(objectClass=user)) when commonName is null - /// - /// - /// - /// - /// - /// - public virtual IList GetUsers(string name = null, string displayName = null, string commonName = null) - { - var conditions = new Dictionary - { - {"objectCategory", "person"}, - {"objectClass", "user"}, - {"name", name}, - {"displayName", displayName}, - {"cn", commonName}, - }; - return Query(LdapOptions.SearchBase, conditions); + Logger = NullLogger.Instance; } - /// - /// query the specified User. - /// - /// filter: (&(distinguishedName=xxx)(objectCategory=person)(objectClass=user)) when distinguishedName is not null - /// - /// - /// - /// - public virtual LdapUser GetUser(string distinguishedName) + public virtual bool Authenticate() { - distinguishedName = Check.NotNullOrWhiteSpace(distinguishedName, nameof(distinguishedName)); - var conditions = new Dictionary - { - {"objectCategory", "person"}, - {"objectClass", "user"}, - {"distinguishedName", distinguishedName}, - }; - return QueryOne(LdapOptions.SearchBase, conditions); + return Authenticate(LdapOptions.UserName, LdapOptions.Password); } - public virtual void AddUserToOrganization(string userName, string password, LdapOrganization parentOrganization) - { - var dn = $"CN={userName},{parentOrganization.DistinguishedName}"; - 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("objectClass", new[] {"top", "person", "organizationalPerson", "user"}), - new LdapAttribute("name", userName), - new LdapAttribute("cn", userName), - new LdapAttribute("sAMAccountName", userName), - new LdapAttribute("userPrincipalName", userName), - new LdapAttribute("sn", userName), - new LdapAttribute("displayName", userName), - new LdapAttribute("unicodePwd", encodedBytes), - new LdapAttribute("userAccountControl", "512"), - new LdapAttribute("mail", mail), - }; - var newEntry = new LdapEntry(dn, attributeSet); - - using (var ldapConnection = GetConnection()) - { - ldapConnection.Add(newEntry); - } - } - - public virtual void AddUserToOrganization(string userName, string password, string parentDistinguishedName) - { - var dn = $"CN={userName},{parentDistinguishedName}"; - 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("objectClass", new[] {"top", "person", "organizationalPerson", "user"}), - new LdapAttribute("name", userName), - new LdapAttribute("cn", userName), - new LdapAttribute("sAMAccountName", userName), - new LdapAttribute("userPrincipalName", userName), - new LdapAttribute("sn", userName), - new LdapAttribute("displayName", userName), - new LdapAttribute("unicodePwd", encodedBytes), - new LdapAttribute("userAccountControl", "512"), - new LdapAttribute("mail", mail), - }; - var newEntry = new LdapEntry(dn, attributeSet); - - using (var ldapConnection = GetConnection()) - { - ldapConnection.Add(newEntry); - } - } - - #endregion - - #region Authenticate - - /// - /// Authenticate - /// - /// E.g administrator@yourdomain.com.cn - /// - /// - public virtual bool Authenticate(string userDomainName, string password) + public bool Authenticate(string username, string password) { try { - using (GetConnection(userDomainName, password)) - { - return true; - } + var conn = CreateLdapConnection(); + AuthenticateLdapConnection(conn, username,password); + return true; } catch (Exception ex) { - using (var scope = HybridServiceScopeFactory.CreateScope()) - { - scope.ServiceProvider - .GetRequiredService() - .NotifyAsync(ex); - } - + Logger.LogException(ex); return false; } } - #endregion - - protected virtual ILdapConnection GetConnection(string bindUserName = null, string bindUserPassword = null) + protected virtual ILdapConnection CreateLdapConnection() { - // bindUserName/bindUserPassword only be used when authenticate - bindUserName ??= LdapOptions.Credentials.DomainUserName; - bindUserPassword ??= LdapOptions.Credentials.Password; - - var ldapConnection = new LdapConnection() { SecureSocketLayer = LdapOptions.UseSsl }; - if (LdapOptions.UseSsl) - { - ldapConnection.UserDefinedServerCertValidationDelegate += (sender, certificate, chain, sslPolicyErrors) => true; - } + var ldapConnection = new LdapConnection(); + ConfigureLdapConnection(ldapConnection); ldapConnection.Connect(LdapOptions.ServerHost, LdapOptions.ServerPort); - - if (LdapOptions.UseSsl) - { - ldapConnection.Bind(LdapConnection.Ldap_V3, bindUserName, bindUserPassword); - } - else - { - ldapConnection.Bind(bindUserName, bindUserPassword); - } return ldapConnection; } - protected virtual IList Query(string searchBase, Dictionary conditions) where T : class, ILdapEntry - { - var filter = LdapHelps.BuildFilter(conditions); - - var result = new List(); - - using (var ldapConnection = GetConnection()) - { - var search = ldapConnection.Search(searchBase, LdapConnection.SCOPE_SUB, filter, - _attributes, false, null, null); - LdapMessage message; - while ((message = search.getResponse()) != null) - { - if (!(message is LdapSearchResult searchResultMessage)) - { - continue; - } - var entry = searchResultMessage.Entry; - if (typeof(T) == typeof(LdapOrganization)) - { - result.Add(new LdapOrganization(entry.getAttributeSet()) as T); - } + protected virtual void ConfigureLdapConnection(ILdapConnection connection) + { - if (typeof(T) == typeof(LdapUser)) - { - result.Add(new LdapUser(entry.getAttributeSet()) as T); - } - } - } - return result; } - protected virtual T QueryOne(string searchBase, Dictionary conditions) where T : class, ILdapEntry + protected virtual void AuthenticateLdapConnection(ILdapConnection connection, string username, string password) { - var filter = LdapHelps.BuildFilter(conditions); - - using (var ldapConnection = GetConnection()) - { - var search = ldapConnection.Search(searchBase, LdapConnection.SCOPE_SUB, filter, - _attributes, false, null, null); - - LdapMessage message; - while ((message = search.getResponse()) != null) - { - if (!(message is LdapSearchResult searchResultMessage)) - { - continue; - } - var entry = searchResultMessage.Entry; - if (typeof(T) == typeof(LdapOrganization)) - { - return new LdapOrganization(entry.getAttributeSet()) as T; - } - - if (typeof(T) == typeof(LdapUser)) - { - return new LdapUser(entry.getAttributeSet()) as T; - } - return null; - } - } - return null; + connection.Bind(username, password); } - } } diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs index cfd851c12f..62fa58b49f 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingNames.cs @@ -6,19 +6,8 @@ public const string ServerPort = "Abp.Ldap.ServerPort"; - public const string UseSsl = "Abp.Ldap.UseSsl"; + public const string UserName = "Abp.Ldap.UserName"; - public const string SearchBase = "Abp.Ldap.SearchBase"; - - public const string DomainName = "Abp.Ldap.DomainName"; - - public const string DomainDistinguishedName = "Abp.Ldap.DomainDistinguishedName"; - - public static class Credentials - { - public const string DomainUserName = "Abp.Ldap.Credentials.DomainUserName"; - - public const string Password = "Abp.Ldap.Credentials.Password"; - } + public const string Password = "Abp.Ldap.Password"; } } diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs index 73dad5d73d..4549e1d8a0 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapSettingProvider.cs @@ -17,45 +17,22 @@ namespace Volo.Abp.Ldap new SettingDefinition( LdapSettingNames.ServerPort, - "", + "389", L("DisplayName:Abp.Ldap.ServerPort"), L("Description:Abp.Ldap.ServerPort")), new SettingDefinition( - LdapSettingNames.UseSsl, - "", - L("DisplayName:Abp.Ldap.UseSsl"), - L("Description:Abp.Ldap.UseSsl")), - - new SettingDefinition( - LdapSettingNames.SearchBase, - "", - L("DisplayName:Abp.Ldap.SearchBase"), - L("Description:Abp.Ldap.SearchBase")), - - new SettingDefinition( - LdapSettingNames.DomainName, - "", - L("DisplayName:Abp.Ldap.DomainName"), - L("Description:Abp.Ldap.DomainName")), - - new SettingDefinition( - LdapSettingNames.DomainDistinguishedName, - "", - L("DisplayName:Abp.Ldap.DomainDistinguishedName"), - L("Description:Abp.Ldap.DomainDistinguishedName")), - - new SettingDefinition( - LdapSettingNames.Credentials.DomainUserName, + LdapSettingNames.UserName, "", - L("DisplayName:Abp.Ldap.Credentials.DomainUserName"), - L("Description:Abp.Ldap.Credentials.DomainUserName")), + L("DisplayName:Abp.Ldap.UserName"), + L("Description:Abp.Ldap.UserName")), new SettingDefinition( - LdapSettingNames.Credentials.Password, + LdapSettingNames.Password, "", - L("DisplayName:Abp.Ldap.Credentials.Password"), - L("Description:Abp.Ldap.Credentials.Password")) + L("DisplayName:Abp.Ldap.Password"), + L("Description:Abp.Ldap.Password"), + isEncrypted: true) ); } diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapEntry.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapEntry.cs deleted file mode 100644 index f15775fc06..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapEntry.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Volo.Abp.Ldap.Modeling -{ - public interface ILdapEntry - { - string ObjectCategory { get; set; } - string[] ObjectClass { get; set; } - string Name { get; set; } - string DistinguishedName { get; set; } - string CommonName { get; set; } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapOrganization.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapOrganization.cs deleted file mode 100644 index 07c11000e8..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapOrganization.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Volo.Abp.Ldap.Modeling -{ - public interface ILdapOrganization : ILdapEntry - { - string OrganizationUnit { get; set; } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapUser.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapUser.cs deleted file mode 100644 index 8ef68f6607..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/ILdapUser.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Volo.Abp.Ldap.Modeling -{ - public interface ILdapUser : ILdapEntry - { - string SamAccountName { get; set; } - string UserPrincipalName { get; set; } - string DisplayName { get; set; } - string Email { get; set; } - string Phone { get; set; } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapEntryBase.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapEntryBase.cs deleted file mode 100644 index 6658a983d0..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapEntryBase.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Novell.Directory.Ldap; - -namespace Volo.Abp.Ldap.Modeling -{ - public abstract class LdapEntryBase : ILdapEntry - { - public string ObjectCategory { get; set; } - public string[] ObjectClass { get; set; } - public string Name { get; set; } - public string CommonName { get; set; } - public string DistinguishedName { get; set; } - - protected LdapEntryBase() { } - - protected LdapEntryBase(LdapAttributeSet attributeSet) - { - ObjectCategory = attributeSet.getAttribute("objectCategory")?.StringValue; - ObjectClass = attributeSet.getAttribute("objectClass")?.StringValueArray; - Name = attributeSet.getAttribute("name")?.StringValue; - CommonName = attributeSet.getAttribute("cn")?.StringValue; - DistinguishedName = attributeSet.getAttribute("distinguishedName")?.StringValue; - } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapOrganization.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapOrganization.cs deleted file mode 100644 index e401a2514d..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapOrganization.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Novell.Directory.Ldap; - -namespace Volo.Abp.Ldap.Modeling -{ - public class LdapOrganization : LdapEntryBase, ILdapOrganization - { - public string OrganizationUnit { get; set; } - - public LdapOrganization() { } - - public LdapOrganization(LdapAttributeSet attributeSet) - : base(attributeSet) - { - OrganizationUnit = attributeSet.getAttribute("ou")?.StringValue; - } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapUser.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapUser.cs deleted file mode 100644 index d9f554d512..0000000000 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Modeling/LdapUser.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Novell.Directory.Ldap; - -namespace Volo.Abp.Ldap.Modeling -{ - public class LdapUser : LdapEntryBase, ILdapUser - { - public string SamAccountName { get; set; } - public string UserPrincipalName { get; set; } - public string DisplayName { get; set; } - public string Email { get; set; } - public string Phone { get; set; } - - public LdapUser() { } - - public LdapUser( LdapAttributeSet attributeSet) - : base(attributeSet) - { - SamAccountName = attributeSet.getAttribute("sAMAccountName")?.StringValue; - UserPrincipalName = attributeSet.getAttribute("userPrincipalName")?.StringValue; - DisplayName = attributeSet.getAttribute("displayName")?.StringValue; - Email = attributeSet.getAttribute("mail")?.StringValue; - Phone = attributeSet.getAttribute("telephoneNumber")?.StringValue; - } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ldap/readme.md b/framework/src/Volo.Abp.Ldap/readme.md deleted file mode 100644 index dd9b84087d..0000000000 --- a/framework/src/Volo.Abp.Ldap/readme.md +++ /dev/null @@ -1,171 +0,0 @@ -# Volo.Abp.Ldap - -# Only Authenticate(not read/write AD) - -## Configure - -add section in `appsettings.json` - -### use SSL - -```json -"LDAP": { - "ServerHost": "192.168.101.54", - "ServerPort": 636, - "UseSsl": true -} -``` - -### not use SSL - -```json -"LDAP": { - "ServerHost": "192.168.101.54", - "ServerPort": 389, - "UseSsl": false -} -``` - -## Authenticate - - Injecting `ILdapManager` into a class. For example: - -```csharp -public class TaxAppService : ApplicationService -{ - private readonly ILdapManager _ldapManager; - - public TaxAppService(ILdapManager ldapManager) - { - _ldapManager = ldapManager; - } - - public void Authenticate(string userName, string password) - { - var result = _ldapManager.Authenticate(userName, password); - } -} -``` - -- `userName` must be full domain name. E.g abc@abc.com - -# Read/Write AD - -## Configure - -### use SSL - -```json -"LDAP": { - "ServerHost": "192.168.101.54", - "ServerPort": 636, - "UseSsl": true, - "Credentials": { - "DomainUserName": "administrator@yourdomain.com.cn", - "Password": "yH.20190528" - }, - "SearchBase": "DC=yourdomain,DC=com,DC=cn", - "DomainName": "yourdomain.com.cn", - "DomainDistinguishedName": "DC=yourdomain,DC=com,DC=cn" -} -``` - -### not use SSL - -```json -"LDAP": { - "ServerHost": "192.168.101.54", - "ServerPort": 389, - "UseSsl": false, - "Credentials": { - "DomainUserName": "administrator@yourdomain.com.cn", - "Password": "yH.20190528" - }, - "SearchBase": "DC=yourdomain,DC=com,DC=cn", - "DomainName": "yourdomain.com.cn", - "DomainDistinguishedName": "DC=yourdomain,DC=com,DC=cn" -} -``` - -- `Credentials:DomainUserName` a administrator of AD. - -- `Credentials:Password` the password for the administrator. -- `SearchBase`: where search from AD. -- `DomainName`: name of you domain. no need `www`. -- `DomainDistinguishedName`: distinguished name of root domain. - -## Query Organizations - -```cs -// query all organizations -// filter: (&(objectClass=organizationalUnit)) -_ldapManager.GetOrganizations(); - -// query organizations by name -// filter: (&(name=abc)(objectClass=organizationalUnit)) -_ldapManager.GetOrganizations("abc"); - -``` - -## Query Organization - -```csharp -// query organization by distinguished name -// filter: (&(distinguishedName=abc)(objectClass=organizationalUnit)) -_ldapManager.GetOrganization("abc"); - -``` - -## Add Organization - -```csharp -// use LdapOrganization -_ldapManager.AddSubOrganization("nameA", parentOrganization); - -// or use OrganizationDistinguishedName -_ldapManager.AddSubOrganization("nameA", "OU=Domain Controllers,DC=yourdomain,DC=com,DC=cn"); -``` - -## Query Users - -```cs -// query all users -// filter: (&(objectCategory=person)(objectClass=user)) -_ldapManager.GetUsers(); - -// query organizations by name -// filter: (&(name=abc)(objectCategory=person)(objectClass=user)) -_ldapManager.GetUsers(name : "abc"); - -// query organizations by displayName -// filter: (&(displayName=abc)(objectCategory=person)(objectClass=user)) -_ldapManager.GetUsers(displayName : "abc"); - -// query organization by commonName -// filter: (&(cn=abc)(objectCategory=person)(objectClass=user)) -_ldapManager.GetUsers(commonName : "abc"); - -``` - -## Query User - -```csharp -// query a user by distinguished name -// filter: (&(distinguishedName=abc)(objectCategory=person)(objectClass=user)) -_ldapManager.GetUser("abc"); - -``` - -## Add User - -```csharp -// use LdapOrganization -_ldapManager.AddUserToOrganization("nameA", "passwordA", parentOrganization); - -// or use OrganizationDistinguishedName -_ldapManager.AddUserToOrganization("nameA", "passwordA", "OU=Domain Controllers,DC=yourdomain,DC=com,DC=cn"); -``` - -# More - -See [unit test](../../test/Volo.Abp.Ldap.Tests) \ 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 cea5e86688..d2db584347 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 @@ -10,6 +10,7 @@ + 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 index f3932e0a05..7d6ba4d759 100644 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs +++ b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs @@ -1,50 +1,20 @@ -using Volo.Abp.Modularity; +using Volo.Abp.Autofac; +using Volo.Abp.Modularity; namespace Volo.Abp.Ldap { - [DependsOn(typeof(AbpLdapModule))] + [DependsOn( + typeof(AbpAutofacModule), + typeof(AbpLdapModule), + typeof(AbpTestBaseModule) + )] 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 deleted file mode 100644 index 46471893ce..0000000000 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/Authenticate_Tests.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using Shouldly; -using Volo.Abp.Modularity; -using Volo.Abp.Testing; -using Xunit; - -namespace Volo.Abp.Ldap -{ - public class Authenticate_Tests : AbpIntegratedTest - { - - private readonly ILdapManager _ldapManager; - private readonly LdapTestData _testData; - - public Authenticate_Tests() - { - // ReSharper disable once VirtualMemberCallInConstructor - _testData = GetRequiredService(); - _ldapManager = GetRequiredService(); - } - - [Fact(Skip = "need environment AD ")] - public void Authenticate() - { - var result = _ldapManager.Authenticate(_testData.AdministratorDomainName, _testData.AdministratorPassword); - - result.ShouldBeTrue(); - } - - [Fact(Skip = "need environment AD ")] - public void Authenticate_With_Wrong_Password() - { - var result = _ldapManager.Authenticate("NonExistentNameA", "PasswordA"); - - result.ShouldBeFalse(); - } - - [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 - // } - - // use ssl - // "LDAP": { - // "ServerHost": "192.168.101.54", - // "ServerPort": 636, - // "UseSSL": true - // } - Configure(settings => - { - settings.ServerHost = "192.168.101.54"; - settings.ServerPort = 636; - settings.UseSsl = true; - }); - } - } - } - -} diff --git a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapHelps_Tests.cs b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapHelps_Tests.cs deleted file mode 100644 index 06d6834992..0000000000 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapHelps_Tests.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System.Collections.Generic; -using Shouldly; -using Xunit; - -namespace Volo.Abp.Ldap -{ - public class LdapHelps_Tests - { - - [Fact] - public void BuildCondition_With_Value() - { - // act - var res = LdapHelps.BuildCondition("objectClass", "testNameA"); - - // assert - res.ShouldBe("(objectClass=testNameA)"); - } - - [Fact] - public void BuildCondition_With_Null_Value() - { - // act - var res = LdapHelps.BuildCondition("objectClass", null); - - // assert - res.ShouldBeEmpty(); - } - - [Fact] - public void BuildCondition_With_Empty_Value() - { - // act - var res = LdapHelps.BuildCondition("objectClass", ""); - - // assert - res.ShouldBeEmpty(); - } - - [Fact] - public void BuildCondition_With_WhiteSpace_Value() - { - // act - var res = LdapHelps.BuildCondition("objectClass", " "); - - // assert - res.ShouldBeEmpty(); - } - - [Fact] - public void BuildFilter_With_Null_Condition() - { - // act - var res = LdapHelps.BuildFilter(null); - - // assert - res.ShouldBe("(&(objectClass=*))"); - } - - [Fact] - public void BuildFilter_With_Empty_Condition() - { - // act - var res = LdapHelps.BuildFilter(new Dictionary()); - - // assert - res.ShouldBe("(&(objectClass=*))"); - } - - [Fact] - public void BuildFilter_With_Condition() - { - // act - var conditions = new Dictionary - { - {"objectClass", "testClassA"}, {"objectCategory", "testCategoryA"}, {"name", null} - }; - var res = LdapHelps.BuildFilter(conditions); - - // assert - res.ShouldBe("(&(objectClass=testClassA)(objectCategory=testCategoryA))"); - } - } -} \ 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 20382e5577..c793347f95 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,192 +1,19 @@ -using System; -using Shouldly; -using Volo.Abp.Testing; -using Xunit; +using Volo.Abp.Testing; namespace Volo.Abp.Ldap { public class LdapManager_Tests : AbpIntegratedTest { private readonly ILdapManager _ldapManager; - private readonly LdapTestData _testData; public LdapManager_Tests() { - _testData = GetRequiredService(); _ldapManager = GetRequiredService(); } - [Fact(Skip = "need environment AD ")] - public void GetOrganizations_With_Empty_Condition() + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) { - var result = _ldapManager.GetOrganizations(); - - result.ShouldNotBeNull(); - result.ShouldContain(e => e.Name == _testData.DomainControllersName); - result.ShouldContain(e => e.DistinguishedName == _testData.DomainControllersDistinguishedName); - } - - [Fact(Skip = "need environment AD ")] - public void GetOrganizations_With_Name() - { - var result = _ldapManager.GetOrganizations(_testData.DomainControllersName); - - result.ShouldNotBeNull(); - result.ShouldHaveSingleItem(); - result.ShouldContain(e => e.Name == _testData.DomainControllersName); - result.ShouldContain(e => e.DistinguishedName == _testData.DomainControllersDistinguishedName); - } - - [Fact(Skip = "need environment AD ")] - public void GetOrganizations_With_Non_Existent_Name() - { - var result = _ldapManager.GetOrganizations("NonExistentNameA"); - - result.ShouldNotBeNull(); - result.ShouldBeEmpty(); - } - - [Fact(Skip = "need environment AD ")] - public void GetOrganization() - { - var result = _ldapManager.GetOrganization(_testData.DomainControllersDistinguishedName); - - result.ShouldNotBeNull(); - result.Name.ShouldBe(_testData.DomainControllersName); - } - - [Fact(Skip = "need environment AD ")] - public void GetOrganization_With_Non_Existent_DistinguishedName() - { - var result = _ldapManager.GetOrganization("NonExistentNameA"); - - result.ShouldBeNull(); - } - - [Fact(Skip = "need environment AD ")] - public void GetUsers_With_Empty_Condition() - { - var result = _ldapManager.GetUsers(); - - result.ShouldNotBeNull(); - result.ShouldContain(e => e.Name == _testData.AdministratorName); - } - - [Fact(Skip = "need environment AD ")] - public void GetUsers_With_Name() - { - var result = _ldapManager.GetUsers(name: _testData.AdministratorName); - - result.ShouldNotBeNull(); - result.ShouldContain(e => e.Name == _testData.AdministratorName); - } - - [Fact(Skip = "need environment AD ")] - public void GetUsers_With_Non_Existent_Name() - { - var result = _ldapManager.GetUsers(name: "NonExistentNameA"); - - result.ShouldNotBeNull(); - result.ShouldBeEmpty(); - } - - [Fact(Skip = "need environment AD ")] - public void GetUsers_With_CommonName() - { - var result = _ldapManager.GetUsers(commonName: _testData.AdministratorName); - - result.ShouldNotBeNull(); - result.ShouldContain(e => e.Name == _testData.AdministratorName); - } - - [Fact(Skip = "need environment AD ")] - public void GetUsers_With_Non_Existent_CommonName() - { - var result = _ldapManager.GetUsers(commonName: "NonExistentNameA"); - - result.ShouldNotBeNull(); - result.ShouldBeEmpty(); - } - - [Fact(Skip = "need environment AD ")] - public void GetUsers_With_DisplayName() - { - var result = _ldapManager.GetUsers(displayName: _testData.AdministratorName); - - result.ShouldNotBeNull(); - // the administrator in AD. not have display name by default. - result.ShouldBeEmpty(); - } - - [Fact(Skip = "need environment AD ")] - public void GetUser() - { - var result = _ldapManager.GetUser(_testData.AdministratorDistinguishedName); - - result.ShouldNotBeNull(); - result.Name.ShouldBe(_testData.AdministratorName); - } - - [Fact(Skip = "need environment AD ")] - public void GetUser_With_Non_Existent_DistinguishedName() - { - var result = _ldapManager.GetOrganization("NonExistentNameA"); - - result.ShouldBeNull(); - } - - [Fact(Skip = "need environment AD ")] - public void Authenticate() - { - var result = _ldapManager.Authenticate(_testData.AdministratorDomainName, _testData.AdministratorPassword); - - result.ShouldBeTrue(); - } - - [Fact(Skip = "need environment AD ")] - public void Authenticate_With_Wrong_Password() - { - var result = _ldapManager.Authenticate("NonExistentNameA", "PasswordA"); - - result.ShouldBeFalse(); - } - - [Fact(Skip = "need environment AD ")] - public void AddSubOrganization() - { - var parentOrganization = _ldapManager.GetOrganization(_testData.DomainControllersDistinguishedName); - var randomName = $"Test_{DateTime.Now.Ticks}"; - - _ldapManager.AddSubOrganization(randomName, parentOrganization); - - var result = _ldapManager.GetOrganizations(randomName); - result.ShouldNotBeNull(); - result.ShouldContain(e => e.Name == randomName); - } - - [Fact(Skip = "need environment AD ")] - public void AddSubOrganization_With_DistinguishedName() - { - var randomName = $"Test_{DateTime.Now.Ticks}"; - - _ldapManager.AddSubOrganization(randomName, _testData.DomainControllersDistinguishedName); - - var result = _ldapManager.GetOrganizations(randomName); - result.ShouldNotBeNull(); - result.ShouldContain(e => e.Name == randomName); - } - - [Fact(Skip = "need environment AD ")] - public void AddOrganizationUser() - { - var parentOrganization = _ldapManager.GetOrganization(_testData.DomainControllersDistinguishedName); - var randomName = $"Test_{DateTime.Now:yyMMddHHmmss}"; - _ldapManager.AddUserToOrganization(randomName, _testData.AdministratorPassword, parentOrganization); - - var result = _ldapManager.GetUsers(randomName); - result.ShouldNotBeNull(); - result.ShouldContain(e=>e.Name == randomName); + options.UseAutofac(); } } - } diff --git a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapOptions_Tests.cs b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapOptions_Tests.cs new file mode 100644 index 0000000000..8ec7ef94c6 --- /dev/null +++ b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapOptions_Tests.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.Options; +using Shouldly; +using Volo.Abp.Testing; +using Xunit; + +namespace Volo.Abp.Ldap +{ + public class LdapOptions_Tests : AbpIntegratedTest + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + + [Fact] + public void Should_Resolve_AbpAbpLdapOptionsFactory() + { + GetRequiredService>().ShouldBeOfType(typeof(AbpAbpLdapOptionsFactory)); + } + } +} diff --git a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapTestData.cs b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapTestData.cs deleted file mode 100644 index 8224686daa..0000000000 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/LdapTestData.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Ldap -{ - public class LdapTestData : ISingletonDependency - { - public string AdministratorName { get; } = "Administrator"; - public string AdministratorPassword { get; } = "yH.20190528"; - public string AdministratorDistinguishedName { get; } = "CN=Administrator,CN=Users,DC=yourdomain,DC=com,DC=cn"; - public string AdministratorDomainName { get; } = "Administrator@yourdomain.com.cn"; - - public string DomainControllersName = "Domain Controllers"; - public string DomainControllersDistinguishedName = "OU=Domain Controllers,DC=yourdomain,DC=com,DC=cn"; - - public string RootDistinguishedName { get; } = "DC=yourdomain,DC=com,DC=cn"; - - public string Organization001Name { get; } = "Test_A"; - - public string Test001Name { get; } = "test001"; - public string Test001Password { get; } = "yH.20190528"; - public string Test001Email { get; } = "test001@yourdomain.com.cn"; - - public string Test002Name { get; } = "test002"; - public string Test002Password { get; } = "yH.20190528"; - public string Test002WrongPassword { get; } = "yH.20190529"; - } -} \ No newline at end of file From c49b62ae03368750603a67c2cd2a6bf05660028d Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Fri, 7 Aug 2020 21:57:11 +0800 Subject: [PATCH 5/6] Update ldap localization. --- .../Volo/Abp/Ldap/Localization/en.json | 28 +++++------------ .../Volo/Abp/Ldap/Localization/tr.json | 31 +++++-------------- .../Volo/Abp/Ldap/Localization/zh-Hans.json | 31 +++++-------------- .../Volo/Abp/Ldap/Localization/zh-Hant.json | 31 +++++-------------- 4 files changed, 32 insertions(+), 89 deletions(-) diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json index 973e059684..e1894aa9d0 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/en.json @@ -1,28 +1,16 @@ { "culture": "en", "texts": { - "DisplayName:Abp.Ldap.ServerHost": "ServerHost", - "Description:Abp.Ldap.ServerHost": "ServerHost", + "DisplayName:Abp.Ldap.ServerHost": "Server host", + "Description:Abp.Ldap.ServerHost": "Server host", - "DisplayName:Abp.Ldap.ServerPort": "ServerPort", - "Description:Abp.Ldap.ServerPort": "ServerPort", + "DisplayName:Abp.Ldap.ServerPort": "Server port", + "Description:Abp.Ldap.ServerPort": "Server port", - "DisplayName:Abp.Ldap.UseSsl": "UseSsl", - "Description:Abp.Ldap.UseSsl": "UseSsl", + "DisplayName:Abp.Ldap.UserName": "Username", + "Description:Abp.Ldap.UserName": "Username", - "DisplayName:Abp.Ldap.SearchBase": "SearchBase", - "Description:Abp.Ldap.SearchBase": "SearchBase", - - "DisplayName:Abp.Ldap.DomainName": "DomainName", - "Description:Abp.Ldap.DomainName": "DomainName", - - "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - - "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - - "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", - "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + "DisplayName:Abp.Ldap.Password": "Password", + "Description:Abp.Ldap.Password": "Password" } } diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json index eac7a9dbec..078d403da8 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/tr.json @@ -1,28 +1,13 @@ { "culture": "tr", "texts": { - "DisplayName:Abp.Ldap.ServerHost": "ServerHost", - "Description:Abp.Ldap.ServerHost": "ServerHost", - - "DisplayName:Abp.Ldap.ServerPort": "ServerPort", - "Description:Abp.Ldap.ServerPort": "ServerPort", - - "DisplayName:Abp.Ldap.UseSsl": "UseSsl", - "Description:Abp.Ldap.UseSsl": "UseSsl", - - "DisplayName:Abp.Ldap.SearchBase": "SearchBase", - "Description:Abp.Ldap.SearchBase": "SearchBase", - - "DisplayName:Abp.Ldap.DomainName": "DomainName", - "Description:Abp.Ldap.DomainName": "DomainName", - - "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - - "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - - "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", - "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + "DisplayName:Abp.Ldap.ServerHost": "Sunucu Ana Bilgisayarı", + "Description:Abp.Ldap.ServerHost": "Sunucu Ana Bilgisayarı", + "DisplayName:Abp.Ldap.ServerPort": "Sunucu portu", + "Description:Abp.Ldap.ServerPort": "Sunucu portu", + "DisplayName:Abp.Ldap.UserName": "Kullanıcı adı", + "Description:Abp.Ldap.UserName": "Kullanıcı adı", + "DisplayName:Abp.Ldap.Password": "parola", + "Description:Abp.Ldap.Password": "parola" } } diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json index 94c0df37c1..6cacad8746 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hans.json @@ -1,28 +1,13 @@ { "culture": "zh-Hans", "texts": { - "DisplayName:Abp.Ldap.ServerHost": "ServerHost", - "Description:Abp.Ldap.ServerHost": "ServerHost", - - "DisplayName:Abp.Ldap.ServerPort": "ServerPort", - "Description:Abp.Ldap.ServerPort": "ServerPort", - - "DisplayName:Abp.Ldap.UseSsl": "UseSsl", - "Description:Abp.Ldap.UseSsl": "UseSsl", - - "DisplayName:Abp.Ldap.SearchBase": "SearchBase", - "Description:Abp.Ldap.SearchBase": "SearchBase", - - "DisplayName:Abp.Ldap.DomainName": "DomainName", - "Description:Abp.Ldap.DomainName": "DomainName", - - "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - - "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - - "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", - "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + "DisplayName:Abp.Ldap.ServerHost": "服务器主机", + "Description:Abp.Ldap.ServerHost": "服务器主机", + "DisplayName:Abp.Ldap.ServerPort": "服务器端口", + "Description:Abp.Ldap.ServerPort": "服务器端口", + "DisplayName:Abp.Ldap.UserName": "用户名", + "Description:Abp.Ldap.UserName": "用户名", + "DisplayName:Abp.Ldap.Password": "密码", + "Description:Abp.Ldap.Password": "密码" } } diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json index 9822a9b6dc..1fcf263ed1 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/Localization/zh-Hant.json @@ -1,28 +1,13 @@ { "culture": "zh-Hant", "texts": { - "DisplayName:Abp.Ldap.ServerHost": "ServerHost", - "Description:Abp.Ldap.ServerHost": "ServerHost", - - "DisplayName:Abp.Ldap.ServerPort": "ServerPort", - "Description:Abp.Ldap.ServerPort": "ServerPort", - - "DisplayName:Abp.Ldap.UseSsl": "UseSsl", - "Description:Abp.Ldap.UseSsl": "UseSsl", - - "DisplayName:Abp.Ldap.SearchBase": "SearchBase", - "Description:Abp.Ldap.SearchBase": "SearchBase", - - "DisplayName:Abp.Ldap.DomainName": "DomainName", - "Description:Abp.Ldap.DomainName": "DomainName", - - "DisplayName:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - "Description:Abp.Ldap.DomainDistinguishedName": "DomainDistinguishedName", - - "DisplayName:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - "Description:Abp.Ldap.Credentials.DomainUserName": "Credentials DomainUserName", - - "DisplayName:Abp.Ldap.Credentials.Password": "Credentials Password", - "Description:Abp.Ldap.Credentials.Password": "Credentials Password", + "DisplayName:Abp.Ldap.ServerHost": "服務器主機", + "Description:Abp.Ldap.ServerHost": "服務器主機", + "DisplayName:Abp.Ldap.ServerPort": "服務器端口", + "Description:Abp.Ldap.ServerPort": "服務器端口", + "DisplayName:Abp.Ldap.UserName": "用戶名", + "Description:Abp.Ldap.UserName": "用戶名", + "DisplayName:Abp.Ldap.Password": "密碼", + "Description:Abp.Ldap.Password": "密碼" } } From f6c80f765eed1f9854e5469c16f3476a50b0b5d0 Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Sat, 8 Aug 2020 14:15:51 +0800 Subject: [PATCH 6/6] Fix OverrideOptionsAsync method logic error. --- .../Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs | 10 +++++----- .../src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs | 2 +- .../Volo/Abp/Ldap/AbpLdapTestModule.cs | 7 +++++-- .../Volo/Abp/Ldap/LdapManager_Tests.cs | 14 +++++++++++++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs index c3d3549a12..0f7117a42a 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/AbpAbpLdapOptionsFactory.cs @@ -32,15 +32,15 @@ namespace Volo.Abp.Ldap protected virtual async Task OverrideOptionsAsync(AbpLdapOptions options) { - options.ServerHost = await GetStringValueOrDefault(LdapSettingNames.ServerHost) ?? options.ServerHost; + options.ServerHost = await GetSettingOrDefaultValue(LdapSettingNames.ServerHost, options.ServerHost); options.ServerPort = await SettingProvider.GetAsync(LdapSettingNames.ServerPort, options.ServerPort); - options.UserName = await GetStringValueOrDefault(LdapSettingNames.UserName) ?? options.UserName; - options.Password = await GetStringValueOrDefault(LdapSettingNames.Password) ?? options.Password; + options.UserName = await GetSettingOrDefaultValue(LdapSettingNames.UserName, options.UserName); + options.Password = await GetSettingOrDefaultValue(LdapSettingNames.Password, options.Password); } - protected virtual async Task GetStringValueOrDefault(string name, string defaultValue = default) + protected virtual async Task GetSettingOrDefaultValue(string name, string defaultValue) { - var value = await SettingProvider.GetOrNullAsync(LdapSettingNames.ServerHost); + var value = await SettingProvider.GetOrNullAsync(name); return value.IsNullOrWhiteSpace() ? defaultValue : value; } } 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 eeb7ce88b2..61b8f608d1 100644 --- a/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs +++ b/framework/src/Volo.Abp.Ldap/Volo/Abp/Ldap/LdapManager.cs @@ -29,7 +29,7 @@ namespace Volo.Abp.Ldap try { var conn = CreateLdapConnection(); - AuthenticateLdapConnection(conn, username,password); + AuthenticateLdapConnection(conn, username, password); return true; } catch (Exception ex) 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 index 7d6ba4d759..a9ab4a28ed 100644 --- a/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs +++ b/framework/test/Volo.Abp.Ldap.Tests/Volo/Abp/Ldap/AbpLdapTestModule.cs @@ -12,9 +12,12 @@ namespace Volo.Abp.Ldap { public override void ConfigureServices(ServiceConfigurationContext context) { - Configure(settings => + Configure(options => { - + options.ServerHost = "192.168.0.3"; + options.ServerPort = 389; + options.UserName = "cn=admin,dc=abp,dc=io"; + options.Password = "123qwe"; }); } } 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 c793347f95..4cfc8718c8 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,4 +1,7 @@ -using Volo.Abp.Testing; +using System; +using Shouldly; +using Volo.Abp.Testing; +using Xunit; namespace Volo.Abp.Ldap { @@ -15,5 +18,14 @@ namespace Volo.Abp.Ldap { options.UseAutofac(); } + + [Fact(Skip = "Required Ldap environment")] + public void Authenticate() + { + _ldapManager.Authenticate().ShouldBe(true); + _ldapManager.Authenticate("cn=abp,dc=abp,dc=io", "123qwe").ShouldBe(true); + _ldapManager.Authenticate("NoExists", "123qwe").ShouldBe(false); + } + } }