-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#218 - Command and server logic for changing password.
- Loading branch information
Showing
8 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters