Skip to content

Commit

Permalink
#218 - Command and server logic for changing password.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Feb 21, 2019
1 parent 420b83e commit 99767f6
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/Money.UI.Backend/Bootstrap/BootstrapTask.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Money.Commands;
using Money.Commands.Handlers;
using Money.Data;
using Money.Hubs;
using Money.Models;
using Money.Services;
using Neptuo;
using Neptuo.Activators;
Expand Down Expand Up @@ -174,6 +178,8 @@ private void Domain()
);

bootstrapTask.Initialize();

commandDispatcher.Handlers.AddAll(new UserHandler(services.BuildServiceProvider().GetRequiredService<UserManager<ApplicationUser>>()));
}

private void ReadModels()
Expand Down
37 changes: 37 additions & 0 deletions src/Money.UI.Backend/Commands/Handlers/UserHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Identity;
using Money.Models;
using Neptuo;
using Neptuo.Collections.Specialized;
using Neptuo.Commands.Handlers;
using Neptuo.Models.Keys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Money.Commands.Handlers
{
public class UserHandler : ICommandHandler<Envelope<ChangePassword>>
{
private readonly UserManager<ApplicationUser> userManager;

public UserHandler(UserManager<ApplicationUser> userManager)
{
Ensure.NotNull(userManager, "userManager");
this.userManager = userManager;
}

public async Task HandleAsync(Envelope<ChangePassword> command)
{
StringKey userKey = command.Metadata.Get<StringKey>("UserKey");
ApplicationUser user = await userManager.FindByIdAsync(userKey.Identifier);
if (user == null)
throw new InvalidOperationException($"Unable to load user with ID '{userKey.Identifier}'.");

IdentityResult result = await userManager.ChangePasswordAsync(user, command.Body.Current, command.Body.New);
if (!result.Succeeded)
throw new InvalidOperationException($"Password change failed for ID '{userKey.Identifier}'.");
}
}
}
39 changes: 39 additions & 0 deletions src/Money.UI.Blazor/Commands/ChangePassword.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Neptuo;
using Neptuo.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Money.Commands
{
/// <summary>
/// A command for changing user password.
/// </summary>
public class ChangePassword : Command
{
/// <summary>
/// Gets a current password.
/// </summary>
public string Current { get; }

/// <summary>
/// Gets a new password.
/// </summary>
public string New { get; }

/// <summary>
/// Creates a new instance for changing password.
/// </summary>
/// <param name="current">A current password.</param>
/// <param name="new">A new password.</param>
public ChangePassword(string current, string @new)
{
Ensure.NotNull(current, "current");
Ensure.NotNull(@new, "new");
Current = current;
New = @new;
}
}
}
2 changes: 2 additions & 0 deletions src/Money.UI.Blazor/Models/Api/CommandMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public CommandMapper()
Add<SetExchangeRate>("currency-exchangerate-set");
Add<RemoveExchangeRate>("currency-exchangerate-remove");
Add<DeleteCurrency>("currency-delete");

Add<ChangePassword>("user-change-password");
}
}
}
27 changes: 19 additions & 8 deletions src/Money.UI.Blazor/Pages/User/ChangePassword.cshtml
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
@page "/user/changepassword"
@inject ICommandDispatcher Commands

<UserHead />

<div class="user">
<div class="row">
<div class="col-md-6">
<form method="post">
<form method="post" onsubmit="@OnFormSubmit">
<div class="form-group">
<label for="OldPassword">Current password</label>
<input class="form-control" autofocus="" type="password" data-val="true" data-val-required="The Current password field is required." id="OldPassword" name="OldPassword">
<span class="text-danger field-validation-valid" data-valmsg-for="OldPassword" data-valmsg-replace="true"></span>
<input class="form-control" autofocus="" type="password" id="OldPassword" bind="@Current" />
</div>
<div class="form-group">
<label for="NewPassword">New password</label>
<input class="form-control" type="password" data-val="true" data-val-length="The New password must be at least 4 and at max 100 characters long." data-val-length-max="100" data-val-length-min="4" data-val-required="The New password field is required." id="NewPassword" name="NewPassword">
<span class="text-danger field-validation-valid" data-valmsg-for="NewPassword" data-valmsg-replace="true"></span>
<input class="form-control" type="password" id="NewPassword" bind="@New" />
</div>
<div class="form-group">
<label for="ConfirmPassword">Confirm new password</label>
<input class="form-control" type="password" data-val="true" data-val-equalto="The new password and confirmation password do not match." data-val-equalto-other="*.NewPassword" id="ConfirmPassword" name="ConfirmPassword">
<span class="text-danger field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span>
<input class="form-control" type="password" id="ConfirmPassword" bind="@Confirm" />
</div>
<button type="submit" class="btn btn-default">Update password</button>
</form>
</div>
</div>
</div>
</div>

@functions
{
public string Current { get; set; }
public string New { get; set; }
public string Confirm { get; set; }

private async Task OnFormSubmit()
{
if (!String.IsNullOrEmpty(Current) && !String.IsNullOrEmpty(New) && New == Confirm)
await Commands.HandleAsync(new Commands.ChangePassword(Current, New));
}
}
14 changes: 11 additions & 3 deletions src/Money.UI.Blazor/Pages/User/Profile.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<div class="user">
<div class="row">
<div class="col-md-6">
<form method="post">
<form method="post" onsubmit="OnFormSubmit">
<div class="form-group">
<label for="UserName">UserName</label>
<input class="form-control" disabled="" type="text" id="UserName" name="UserName" value="maraf">
</div>
<div class="form-group">
<label for="Email">Email</label>
<div class="input-group">
<input class="form-control" autofocus="" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" value="">
<input class="form-control" autofocus type="email" id="Email" name="Email" value="">
<span class="input-group-addon" aria-hidden="true"><span class="glyphicon glyphicon-ok text-success"></span></span>
</div>
<span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
Expand All @@ -22,4 +22,12 @@
</form>
</div>
</div>
</div>
</div>

@functions
{
private async Task OnFormSubmit()
{

}
}
6 changes: 6 additions & 0 deletions src/Money.UI.Blazor/Services/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public ApiClient(HttpClient http, CommandMapper commandMapper, QueryMapper query
public Task<string> GetUserNameAsync()
=> http.GetStringAsync("/api/username");

public Task ChangeEmail(string email)
=> http.PostJsonAsync("/api/user/changeemail", email);

public Task ChangePassword(string currentPassword, string newPassword)
=> http.PostJsonAsync("/api/user/changeemail", new { Current = currentPassword, New = newPassword });

private Request CreateRequest(Type type, string payload)
=> new Request() { Type = type.AssemblyQualifiedName, Payload = payload };

Expand Down
3 changes: 3 additions & 0 deletions src/Money.UI.Blazor/Services/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@ public void OpenAbout()

public void OpenUserManage()
=> uri.NavigateTo(UrlUserManage());

public void OpenUserPassword()
=> uri.NavigateTo(UrlUserPassword());
}
}

0 comments on commit 99767f6

Please sign in to comment.