Browse Source

Merge pull request #7362 from abpframework/maliming/AbpIdentityResultExtensions

Allows to customize the English localized text of Identity errors.
pull/7470/head
Yunus Emre Kalkan 6 years ago
committed by GitHub
parent
commit
b6909b62aa
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 73
      modules/identity/src/Volo.Abp.Identity.Domain/Microsoft/AspNetCore/Identity/AbpIdentityResultExtensions.cs
  2. 6
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo.Abp.Identity.Domain.Tests.csproj
  3. 15
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs
  4. 10
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityResultException_Tests.cs
  5. 7
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/LocalizationExtensions/en.json

73
modules/identity/src/Volo.Abp.Identity.Domain/Microsoft/AspNetCore/Identity/AbpIdentityResultExtensions.cs

@ -1,17 +1,50 @@
using System; using System;
using System.Collections;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Resources;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Volo.Abp;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Volo.Abp.Localization;
using Volo.Abp.Text.Formatting; using Volo.Abp.Text.Formatting;
namespace Microsoft.AspNetCore.Identity namespace Microsoft.AspNetCore.Identity
{ {
public static class AbpIdentityResultExtensions public static class AbpIdentityResultExtensions
{ {
private static readonly Dictionary<string, string> IdentityStrings = new Dictionary<string, string>();
static AbpIdentityResultExtensions()
{
var identityResourceManager = new ResourceManager("Microsoft.Extensions.Identity.Core.Resources", typeof(UserManager<>).Assembly);
var resourceSet = identityResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, false);
if (resourceSet == null)
{
throw new AbpException("Can't get the ResourceSet of Identity.");
}
var iterator = resourceSet.GetEnumerator();
while (true)
{
if (!iterator.MoveNext())
{
break;
}
var key = iterator.Key?.ToString();
var value = iterator.Value?.ToString();
if (key != null && value != null)
{
IdentityStrings.Add(key, value);
}
}
if (!IdentityStrings.Any())
{
throw new AbpException("ResourceSet values of Identity is empty.");
}
}
public static void CheckErrors(this IdentityResult identityResult) public static void CheckErrors(this IdentityResult identityResult)
{ {
if (identityResult.Succeeded) if (identityResult.Succeeded)
@ -41,25 +74,19 @@ namespace Microsoft.AspNetCore.Identity
} }
var error = identityResult.Errors.First(); var error = identityResult.Errors.First();
var key = $"Volo.Abp.Identity:{error.Code}"; var englishString = IdentityStrings.GetOrDefault(error.Code);
using (CultureHelper.Use(CultureInfo.GetCultureInfo("en"))) if (englishString == null)
{ {
var englishLocalizedString = localizer[key];
if (englishLocalizedString.ResourceNotFound)
{
return Array.Empty<string>();
}
if (FormattedStringValueExtracter.IsMatch(error.Description, englishLocalizedString.Value,
out var values))
{
return values;
}
return Array.Empty<string>(); return Array.Empty<string>();
} }
if (FormattedStringValueExtracter.IsMatch(error.Description, englishString, out var values))
{
return values;
}
return Array.Empty<string>();
} }
public static string LocalizeErrors(this IdentityResult identityResult, IStringLocalizer localizer) public static string LocalizeErrors(this IdentityResult identityResult, IStringLocalizer localizer)
@ -85,16 +112,12 @@ namespace Microsoft.AspNetCore.Identity
if (!localizedString.ResourceNotFound) if (!localizedString.ResourceNotFound)
{ {
using (CultureHelper.Use(CultureInfo.GetCultureInfo("en"))) var englishString = IdentityStrings.GetOrDefault(error.Code);
if (englishString != null)
{ {
var englishLocalizedString = localizer[key]; if (FormattedStringValueExtracter.IsMatch(error.Description, englishString, out var values))
if (!englishLocalizedString.ResourceNotFound)
{ {
if (FormattedStringValueExtracter.IsMatch(error.Description, englishLocalizedString.Value, return string.Format(localizedString.Value, values.Cast<object>().ToArray());
out var values))
{
return string.Format(localizedString.Value, values.Cast<object>().ToArray());
}
} }
} }
} }

6
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo.Abp.Identity.Domain.Tests.csproj

@ -19,6 +19,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Volo\Abp\Identity\LocalizationExtensions\*.json" />
<Content Remove="Volo\Abp\Identity\LocalizationExtensions\*.json" />
</ItemGroup> </ItemGroup>
</Project> </Project>

15
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs

@ -2,9 +2,12 @@
using Volo.Abp.Authorization.Permissions; using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Identity.Localization;
using Volo.Abp.Localization;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement.Identity; using Volo.Abp.PermissionManagement.Identity;
using Volo.Abp.Threading; using Volo.Abp.Threading;
using Volo.Abp.VirtualFileSystem;
namespace Volo.Abp.Identity namespace Volo.Abp.Identity
{ {
@ -21,6 +24,18 @@ namespace Volo.Abp.Identity
{ {
options.AutoEventSelectors.Add<IdentityUser>(); options.AutoEventSelectors.Add<IdentityUser>();
}); });
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<AbpIdentityDomainTestModule>();
});
Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Get<IdentityResource>()
.AddVirtualJson("/Volo/Abp/Identity/LocalizationExtensions");
});
} }
public override void OnApplicationInitialization(ApplicationInitializationContext context) public override void OnApplicationInitialization(ApplicationInitializationContext context)

10
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityResultException_Tests.cs

@ -28,10 +28,18 @@ namespace Volo.Abp.Identity
using (CultureHelper.Use("tr")) using (CultureHelper.Use("tr"))
{ {
var localizeMessage = exception.LocalizeMessage(new LocalizationContext(ServiceProvider)); var localizeMessage = exception.LocalizeMessage(new LocalizationContext(ServiceProvider));
localizeMessage.ShouldContain("Şifre en az 6 karakter uzunluğunda olmalı."); localizeMessage.ShouldContain("Şifre en az 6 karakter uzunluğunda olmalı.");
localizeMessage.ShouldContain("Şifre en az bir sayı ya da harf olmayan karakter içermeli."); localizeMessage.ShouldContain("Şifre en az bir sayı ya da harf olmayan karakter içermeli.");
} }
using (CultureHelper.Use("en"))
{
var localizeMessage = exception.LocalizeMessage(new LocalizationContext(ServiceProvider));
localizeMessage.ShouldContain("Password length must be greater than 6 characters.");
localizeMessage.ShouldContain("Password must contain at least one non-alphanumeric character.");
}
} }
} }
} }

7
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/LocalizationExtensions/en.json

@ -0,0 +1,7 @@
{
"culture": "en",
"texts": {
"Volo.Abp.Identity:PasswordTooShort": "Password length must be greater than {0} characters.",
"Volo.Abp.Identity:PasswordRequiresNonAlphanumeric": "Password must contain at least one non-alphanumeric character."
}
}
Loading…
Cancel
Save