Skip to content

Commit

Permalink
Add GetUserNameFromEmailAsync to IdentityUserManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Dec 28, 2023
1 parent 4d798c2 commit 75c5d72
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Volo.Abp.Account.Localization;
Expand Down Expand Up @@ -55,22 +54,4 @@ protected virtual string GetLocalizeExceptionMessage(Exception exception)

return exception.Message;
}

protected virtual async Task<string> GetUserNameFromEmail(string email)
{
var userName = email.Split('@')[0];
var existUser = await UserManager.FindByNameAsync(userName);
while (existUser != null)
{
var randomUserName = userName + RandomHelper.GetRandom(1000, 9999);
existUser = await UserManager.FindByNameAsync(randomUserName);
if (existUser == null)
{
userName = randomUserName;
break;
}
}

return userName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected virtual async Task TrySetEmailAsync()
return;
}

var userName = await GetUserNameFromEmail(emailClaim.Value);
var userName = await UserManager.GetUserNameFromEmailAsync(emailClaim.Value);
Input = new PostInput { UserName = userName, EmailAddress = emailClaim.Value };
}
}
Expand All @@ -128,7 +128,7 @@ public virtual async Task<IActionResult> OnPostAsync()
}
if (Input.UserName.IsNullOrWhiteSpace())
{
Input.UserName = await GetUserNameFromEmail(Input.EmailAddress);
Input.UserName = await UserManager.GetUserNameFromEmailAsync(Input.EmailAddress);
}
await RegisterExternalUserAsync(externalLoginInfo, Input.UserName, Input.EmailAddress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,57 @@ public virtual Task<string> GetRandomUserNameAsync(int length)

return Task.FromResult(randomUserName);
}

public virtual async Task<string> GetUserNameFromEmailAsync(string email)
{
var userName = email.Split('@')[0];
if (!Options.User.AllowedUserNameCharacters.IsNullOrWhiteSpace() && !userName.All(Options.User.AllowedUserNameCharacters.Contains))
{

Check warning on line 444 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L444

Added line #L444 was not covered by tests
// The user name contains not allowed characters. We will use the email address as user name.
return email;

Check warning on line 446 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L446

Added line #L446 was not covered by tests
}

if (await ValidateUserNameAsync(userName))
{
return userName;

Check warning on line 451 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L450-L451

Added lines #L450 - L451 were not covered by tests
}

const int maxTryCount = 10;
var tryCount = 0;

if (Options.User.AllowedUserNameCharacters.IsNullOrWhiteSpace() || "0123456789".All(Options.User.AllowedUserNameCharacters.Contains))
{
var randomUserName = userName;
var isUserNameValid = await ValidateUserNameAsync(randomUserName);
while (tryCount < maxTryCount)
{
randomUserName = userName + RandomHelper.GetRandom(1000, 9999);
isUserNameValid = await ValidateUserNameAsync(randomUserName);
if (isUserNameValid)
{
return randomUserName;
}
tryCount++;
}

Check warning on line 470 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L469-L470

Added lines #L469 - L470 were not covered by tests
if (isUserNameValid)
{
return randomUserName;

Check warning on line 473 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L472-L473

Added lines #L472 - L473 were not covered by tests
}
}

Check warning on line 475 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L475

Added line #L475 was not covered by tests

tryCount = 0;
while (tryCount < maxTryCount)
{
var randomUserName = userName + await GetRandomUserNameAsync(4);
var isUserNameValid = await ValidateUserNameAsync(randomUserName);
if (isUserNameValid)
{
return randomUserName;
}
tryCount++;
}

Check warning on line 487 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L486-L487

Added lines #L486 - L487 were not covered by tests

// We could not find a valid user name so we are returning the email address.
return email;

Check warning on line 490 in modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs#L490

Added line #L490 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -422,6 +423,34 @@ public async Task GetRandomUserNameAsync()
username.All(c => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+".Contains(c)).ShouldBeTrue();
}

Check warning on line 424 in modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs#L420-L424

Added lines #L420 - L424 were not covered by tests

[Fact]
public async Task GetUserNameFromEmailAsync()
{
_identityUserManager.Options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyz0123456789";
var username = await _identityUserManager.GetUserNameFromEmailAsync("[email protected]");
username.Length.ShouldBe(9); //admin and random 4 numbers
username.ShouldContain("admin");
Regex.IsMatch(username, @"\d{4}$").ShouldBeTrue();

Check warning on line 433 in modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs#L428-L433

Added lines #L428 - L433 were not covered by tests

_identityUserManager.Options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyz";
username = await _identityUserManager.GetUserNameFromEmailAsync("[email protected]");
username.Length.ShouldBe(9); //admin and random 4 characters
username.ShouldContain("admin");
Regex.IsMatch(username, @"[a-z]{4}$").ShouldBeTrue();

Check warning on line 439 in modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs#L435-L439

Added lines #L435 - L439 were not covered by tests

_identityUserManager.Options.User.AllowedUserNameCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
username = await _identityUserManager.GetUserNameFromEmailAsync("[email protected]");
username.Length.ShouldBe(9); //admin and random 4 characters
username.ShouldContain("ADMIN");
Regex.IsMatch(username, @"[A-Z]{4}$").ShouldBeTrue();

Check warning on line 445 in modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs#L441-L445

Added lines #L441 - L445 were not covered by tests

_identityUserManager.Options.User.AllowedUserNameCharacters = null!;
username = await _identityUserManager.GetUserNameFromEmailAsync("[email protected]");
username.Length.ShouldBe(9); //admin and random 4 numbers
username.ShouldContain("admin");
Regex.IsMatch(username, @"[0-9]{4}$").ShouldBeTrue();
}

Check warning on line 452 in modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

View check run for this annotation

Codecov / codecov/patch

modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs#L447-L452

Added lines #L447 - L452 were not covered by tests

private async Task CreateRandomDefaultRoleAsync()
{
await _identityRoleRepository.InsertAsync(
Expand Down

0 comments on commit 75c5d72

Please sign in to comment.