Skip to content

Commit

Permalink
Add DisplayName to user entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed May 1, 2020
1 parent 6ef5eaa commit c484ddb
Show file tree
Hide file tree
Showing 7 changed files with 797 additions and 15 deletions.
10 changes: 6 additions & 4 deletions Server/Areas/Identity/Pages/Account/Manage/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
<div class="col-md-6">
<form id="profile-form" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Username"></label>
<input asp-for="Username" class="form-control" disabled />
</div>

<div class="form-group">
<label asp-for="Input.Email"></label>
@if (Model.IsEmailConfirmed)
Expand All @@ -31,6 +28,11 @@
}
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.DisplayName"></label>
<input asp-for="Input.DisplayName" class="form-control" />
<span asp-validation-for="Input.DisplayName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.PhoneNumber"></label>
<input asp-for="Input.PhoneNumber" class="form-control" />
Expand Down
41 changes: 31 additions & 10 deletions Server/Areas/Identity/Pages/Account/Manage/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ public partial class IndexModel : PageModel
private readonly UserManager<RemotelyUser> _userManager;
private readonly SignInManager<RemotelyUser> _signInManager;
private readonly IEmailSenderEx _emailSender;
private readonly DataService _dataService;

public IndexModel(
UserManager<RemotelyUser> userManager,
SignInManager<RemotelyUser> signInManager,
DataService dataService,
IEmailSenderEx emailSender)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_dataService = dataService;
}

public string Username { get; set; }

public bool IsEmailConfirmed { get; set; }

[TempData]
Expand All @@ -40,8 +41,13 @@ public IndexModel(

public class InputModel
{
[Display(Name = "Display Name")]
[StringLength(100)]
public string DisplayName { get; set; }

[Required]
[EmailAddress]
[Display(Name = "Email/Username")]
public string Email { get; set; }

[Phone]
Expand All @@ -57,16 +63,11 @@ public async Task<IActionResult> OnGetAsync()
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}

var userName = await _userManager.GetUserNameAsync(user);
var email = await _userManager.GetEmailAsync(user);
var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

Username = userName;

Input = new InputModel
{
Email = email,
PhoneNumber = phoneNumber
Email = user.Email,
PhoneNumber = user.PhoneNumber,
DisplayName = user.DisplayName
};

IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
Expand All @@ -90,6 +91,19 @@ public async Task<IActionResult> OnPostAsync()
var email = await _userManager.GetEmailAsync(user);
if (Input.Email != email)
{
if (_dataService.DoesUserExist(Input.Email))
{
ModelState.AddModelError("Input.UserName", "Email/Username already exists.");
return Page();
}

var setUsernameResult = await _userManager.SetUserNameAsync(user, Input.Email);
if (!setUsernameResult.Succeeded)
{
var userId = await _userManager.GetUserIdAsync(user);
throw new InvalidOperationException($"Unexpected error occurred setting user name for user with ID '{userId}'.");
}

var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email);
if (!setEmailResult.Succeeded)
{
Expand All @@ -109,6 +123,13 @@ public async Task<IActionResult> OnPostAsync()
}
}



if (Input.DisplayName != user.DisplayName)
{
await _dataService.SetDisplayName(user, Input.DisplayName);
}

await _signInManager.RefreshSignInAsync(user);
StatusMessage = "Your profile has been updated";
return RedirectToPage();
Expand Down
Loading

0 comments on commit c484ddb

Please sign in to comment.