Browse Source

Merge pull request #17755 from abpframework/username-17750

Allow to set username on social registration.
pull/17797/head
Halil İbrahim Kalkan 3 years ago
committed by GitHub
parent
commit
3bdd2280f8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs
  2. 3
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs
  3. 12
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml
  4. 41
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs

19
modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.Account.Localization; using Volo.Abp.Account.Localization;
@ -54,4 +55,22 @@ public abstract class AccountPageModel : AbpPageModel
return exception.Message; 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;
}
} }

3
modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs

@ -269,8 +269,9 @@ public class LoginModel : AccountPageModel
await IdentityOptions.SetAsync(); await IdentityOptions.SetAsync();
var emailAddress = info.Principal.FindFirstValue(AbpClaimTypes.Email); var emailAddress = info.Principal.FindFirstValue(AbpClaimTypes.Email);
var userName = await GetUserNameFromEmail(emailAddress);
var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id); var user = new IdentityUser(GuidGenerator.Create(), userName, emailAddress, CurrentTenant.Id);
CheckIdentityErrors(await UserManager.CreateAsync(user)); CheckIdentityErrors(await UserManager.CreateAsync(user));
CheckIdentityErrors(await UserManager.SetEmailAsync(user, emailAddress)); CheckIdentityErrors(await UserManager.SetEmailAsync(user, emailAddress));

12
modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml

@ -12,11 +12,11 @@
<a href="@Url.Page("./Login", new {returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash})" class="text-decoration-none">@L["Login"]</a> <a href="@Url.Page("./Login", new {returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash})" class="text-decoration-none">@L["Login"]</a>
</strong> </strong>
<form method="post" class="mt-4"> <form method="post" class="mt-4">
@if (!Model.IsExternalLogin && Model.EnableLocalRegister) @if ((!Model.IsExternalLogin || Model.UserNameExtracted) && Model.EnableLocalRegister)
{ {
<abp-input asp-for="Input.UserName" auto-focus="true"/> <abp-input asp-for="Input.UserName" auto-focus="true"/>
} }
@if(Model.EnableLocalRegister || Model.IsExternalLogin) @if(Model.EnableLocalRegister || Model.IsExternalLogin)
{ {
<abp-input asp-for="Input.EmailAddress"/> <abp-input asp-for="Input.EmailAddress"/>
@ -26,16 +26,16 @@
{ {
<abp-input asp-for="Input.Password"/> <abp-input asp-for="Input.Password"/>
} }
@if(Model.EnableLocalRegister || Model.IsExternalLogin) @if(Model.EnableLocalRegister || Model.IsExternalLogin)
{ {
<div class="d-grid gap-2"> <div class="d-grid gap-2">
<abp-button button-type="Primary" type="submit" class="btn-lg mt-4">@L["Register"]</abp-button> <abp-button button-type="Primary" type="submit" class="btn-lg mt-4">@L["Register"]</abp-button>
</div> </div>
} }
</form> </form>
@if (!Model.IsExternalLogin && Model.VisibleExternalProviders.Any()) @if (!Model.IsExternalLogin && Model.VisibleExternalProviders.Any())
{ {
<div class="mt-2"> <div class="mt-2">
@ -48,6 +48,6 @@
</form> </form>
</div> </div>
} }
</div> </div>
</div> </div>

41
modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs

@ -35,19 +35,20 @@ public class RegisterModel : AccountPageModel
[BindProperty(SupportsGet = true)] [BindProperty(SupportsGet = true)]
public string ExternalLoginAuthSchema { get; set; } public string ExternalLoginAuthSchema { get; set; }
public bool UserNameExtracted { get; set; }
public IEnumerable<ExternalProviderModel> ExternalProviders { get; set; } public IEnumerable<ExternalProviderModel> ExternalProviders { get; set; }
public IEnumerable<ExternalProviderModel> VisibleExternalProviders => ExternalProviders.Where(x => !string.IsNullOrWhiteSpace(x.DisplayName)); public IEnumerable<ExternalProviderModel> VisibleExternalProviders => ExternalProviders.Where(x => !string.IsNullOrWhiteSpace(x.DisplayName));
public bool EnableLocalRegister { get; set; } public bool EnableLocalRegister { get; set; }
public bool IsExternalLoginOnly => EnableLocalRegister == false && ExternalProviders?.Count() == 1; public bool IsExternalLoginOnly => EnableLocalRegister == false && ExternalProviders?.Count() == 1;
public string ExternalLoginScheme => IsExternalLoginOnly ? ExternalProviders?.SingleOrDefault()?.AuthenticationScheme : null; public string ExternalLoginScheme => IsExternalLoginOnly ? ExternalProviders?.SingleOrDefault()?.AuthenticationScheme : null;
protected IAuthenticationSchemeProvider SchemeProvider { get; } protected IAuthenticationSchemeProvider SchemeProvider { get; }
protected AbpAccountOptions AccountOptions { get; } protected AbpAccountOptions AccountOptions { get; }
public RegisterModel( public RegisterModel(
IAccountAppService accountAppService, IAccountAppService accountAppService,
IAuthenticationSchemeProvider schemeProvider, IAuthenticationSchemeProvider schemeProvider,
IOptions<AbpAccountOptions> accountOptions) IOptions<AbpAccountOptions> accountOptions)
{ {
@ -66,16 +67,16 @@ public class RegisterModel : AccountPageModel
{ {
return await OnPostExternalLogin(ExternalLoginScheme); return await OnPostExternalLogin(ExternalLoginScheme);
} }
Alerts.Warning(L["SelfRegistrationDisabledMessage"]); Alerts.Warning(L["SelfRegistrationDisabledMessage"]);
} }
await TrySetEmailAsync(); await TrySetEmailAsync();
return Page(); return Page();
} }
private async Task TrySetEmailAsync() protected virtual async Task TrySetEmailAsync()
{ {
if (IsExternalLogin) if (IsExternalLogin)
{ {
@ -98,7 +99,8 @@ public class RegisterModel : AccountPageModel
return; return;
} }
Input = new PostInput { EmailAddress = emailClaim.Value }; var userName = await GetUserNameFromEmail(emailClaim.Value);
Input = new PostInput { UserName = userName, EmailAddress = emailClaim.Value };
} }
} }
@ -121,8 +123,12 @@ public class RegisterModel : AccountPageModel
Logger.LogWarning("External login info is not available"); Logger.LogWarning("External login info is not available");
return RedirectToPage("./Login"); return RedirectToPage("./Login");
} }
if (Input.UserName.IsNullOrWhiteSpace())
await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress); {
Input.UserName = await GetUserNameFromEmail(Input.EmailAddress);
}
UserNameExtracted = true;
await RegisterExternalUserAsync(externalLoginInfo, Input.UserName, Input.EmailAddress);
} }
else else
{ {
@ -156,11 +162,11 @@ public class RegisterModel : AccountPageModel
await SignInManager.SignInAsync(user, isPersistent: true); await SignInManager.SignInAsync(user, isPersistent: true);
} }
protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string emailAddress) protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string userName, string emailAddress)
{ {
await IdentityOptions.SetAsync(); await IdentityOptions.SetAsync();
var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id); var user = new IdentityUser(GuidGenerator.Create(), userName, emailAddress, CurrentTenant.Id);
(await UserManager.CreateAsync(user)).CheckErrors(); (await UserManager.CreateAsync(user)).CheckErrors();
(await UserManager.AddDefaultRolesAsync(user)).CheckErrors(); (await UserManager.AddDefaultRolesAsync(user)).CheckErrors();
@ -191,7 +197,7 @@ public class RegisterModel : AccountPageModel
{ {
return true; return true;
} }
if (!EnableLocalRegister) if (!EnableLocalRegister)
{ {
return false; return false;
@ -199,10 +205,9 @@ public class RegisterModel : AccountPageModel
return true; return true;
} }
protected virtual async Task<List<ExternalProviderModel>> GetExternalProviders() protected virtual async Task<List<ExternalProviderModel>> GetExternalProviders()
{ {
var schemes = await SchemeProvider.GetAllSchemesAsync(); var schemes = await SchemeProvider.GetAllSchemesAsync();
return schemes return schemes
@ -214,7 +219,7 @@ public class RegisterModel : AccountPageModel
}) })
.ToList(); .ToList();
} }
protected virtual async Task<IActionResult> OnPostExternalLogin(string provider) protected virtual async Task<IActionResult> OnPostExternalLogin(string provider)
{ {
var redirectUrl = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { ReturnUrl, ReturnUrlHash }); var redirectUrl = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { ReturnUrl, ReturnUrlHash });
@ -241,7 +246,7 @@ public class RegisterModel : AccountPageModel
[DisableAuditing] [DisableAuditing]
public string Password { get; set; } public string Password { get; set; }
} }
public class ExternalProviderModel public class ExternalProviderModel
{ {
public string DisplayName { get; set; } public string DisplayName { get; set; }

Loading…
Cancel
Save