Skip to content

Commit cabd673

Browse files
committed
Added update employee password module
- Updated Employee details - Added summary comments on Accounts Feature
1 parent ed9e244 commit cabd673

File tree

8 files changed

+106
-19
lines changed

8 files changed

+106
-19
lines changed

WebApi/Features/Accounts/AccountsController.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace WebApi.Features.Accounts
99
{
10-
[Authorize(Roles = "Admin")]
10+
[Authorize]
1111
[Route("api/[controller]"), ApiController]
1212
public class AccountsController : ControllerBase
1313
{
@@ -19,6 +19,7 @@ public AccountsController(IMediator mediator)
1919
}
2020

2121
// POST: api/accounts/register
22+
[Authorize(Roles = "Admin")]
2223
[HttpPost("register")]
2324
public async Task<IActionResult> Register(RegisterViewModel viewModel)
2425
{
@@ -36,16 +37,28 @@ public async Task<IActionResult> Register(RegisterViewModel viewModel)
3637
return new CreatedResult("", employeeInfo);
3738
}
3839

39-
[Authorize]
40+
// PUT: api/accounts/update-password
41+
[Authorize(Roles = "Admin")]
42+
[HttpPut("update-password")]
43+
public async Task<IActionResult> UpdatePassword(ChangePasswordViewModel viewModel)
44+
{
45+
// Change a specific Employee account's password
46+
var result = await _mediator.Send(new UpdatePassword.Command(viewModel));
47+
if(!result) return StatusCode(500);
48+
49+
return Ok();
50+
}
51+
52+
// PUT: api/accounts/change-password
4053
[HttpPut("change-password")]
41-
public async Task<IActionResult> ChangePassword(UpdatePasswordViewModel viewModel)
54+
public async Task<IActionResult> ChangePassword(ChangePasswordViewModel viewModel)
4255
{
4356
// Check if Old password is correct
4457
var validatePassword = await _mediator.Send(new Auth.ValidatePassword.Query(viewModel.UserName, viewModel.OldPassword));
4558
if (!validatePassword) return BadRequest("Incorrect password");
4659

4760
// Change account password
48-
var result = await _mediator.Send(new UpdatePassword.Command(viewModel));
61+
var result = await _mediator.Send(new ChangePassword.Command(viewModel));
4962
if(!result.Succeeded) return BadRequest("Unable to change password");
5063

5164
return Ok();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using MediatR;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Identity;
8+
using WebApi.Entities;
9+
10+
namespace WebApi.Features.Accounts
11+
{
12+
/// <summary>
13+
/// Change account password
14+
/// </summary>
15+
public class ChangePassword
16+
{
17+
public class Command : IRequest<IdentityResult>
18+
{
19+
public Command(ChangePasswordViewModel viewModel)
20+
{
21+
ViewModel = viewModel;
22+
}
23+
24+
public ChangePasswordViewModel ViewModel { get; }
25+
}
26+
27+
public class CommandHandler : IRequestHandler<Command, IdentityResult>
28+
{
29+
private readonly UserManager<User> _manager;
30+
private readonly IHttpContextAccessor _httpContext;
31+
32+
public CommandHandler(UserManager<User> manager, IHttpContextAccessor httpContext)
33+
{
34+
_manager = manager;
35+
_httpContext = httpContext;
36+
}
37+
38+
public async Task<IdentityResult> Handle(Command request, CancellationToken cancellationToken)
39+
{
40+
try
41+
{
42+
// Get the username in sub type claim
43+
var username = _httpContext.HttpContext.User.Claims
44+
.First(m => m.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")
45+
.Value;
46+
47+
// Get account details
48+
var user = await _manager.FindByNameAsync(username);
49+
50+
// Change account password then return result
51+
return await _manager.ChangePasswordAsync(
52+
user,
53+
request.ViewModel.OldPassword,
54+
request.ViewModel.NewPassword);
55+
}
56+
catch (Exception e)
57+
{
58+
Console.WriteLine(e);
59+
throw;
60+
}
61+
}
62+
}
63+
}
64+
}

WebApi/Features/Accounts/UpdatePasswordViewModel.cs renamed to WebApi/Features/Accounts/ChangePasswordViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace WebApi.Features.Accounts
22
{
3-
public class UpdatePasswordViewModel
3+
public class ChangePasswordViewModel
44
{
55
public string UserName { get; set; }
66
public string OldPassword { get; set; }

WebApi/Features/Accounts/Register.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace WebApi.Features.Accounts
1111
{
12+
/// <summary>
13+
/// Create employee account
14+
/// </summary>
1215
public class Register
1316
{
1417
public class Command : IRequest<EmployeeViewModel>

WebApi/Features/Accounts/UpdatePassword.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
using System;
23
using System.Threading;
34
using System.Threading.Tasks;
@@ -7,19 +8,22 @@
78

89
namespace WebApi.Features.Accounts
910
{
11+
/// <summary>
12+
/// Change a specific Employee account's password
13+
/// </summary>
1014
public class UpdatePassword
1115
{
12-
public class Command : IRequest<IdentityResult>
16+
public class Command : IRequest<bool>
1317
{
14-
public Command(UpdatePasswordViewModel viewModel)
18+
public Command(ChangePasswordViewModel viewModel)
1519
{
1620
ViewModel = viewModel;
1721
}
1822

19-
public UpdatePasswordViewModel ViewModel { get; }
23+
public ChangePasswordViewModel ViewModel { get; }
2024
}
2125

22-
public class CommandHandler : IRequestHandler<Command, IdentityResult>
26+
public class CommandHandler : IRequestHandler<Command, bool>
2327
{
2428
private readonly UserManager<User> _manager;
2529

@@ -28,18 +32,20 @@ public CommandHandler(UserManager<User> manager)
2832
_manager = manager;
2933
}
3034

31-
public async Task<IdentityResult> Handle(Command request, CancellationToken cancellationToken)
35+
public async Task<bool> Handle(Command request, CancellationToken cancellationToken)
3236
{
3337
try
3438
{
3539
// Get account details
3640
var user = await _manager.FindByNameAsync(request.ViewModel.UserName);
3741

38-
// Change account password then return result
39-
return await _manager.ChangePasswordAsync(
40-
user,
41-
request.ViewModel.OldPassword,
42-
request.ViewModel.NewPassword);
42+
// Remove the existing password
43+
await _manager.RemovePasswordAsync(user);
44+
45+
// Add the new password
46+
var result = await _manager.AddPasswordAsync(user, request.ViewModel.NewPassword);
47+
48+
return (result.Succeeded) ? true : false;
4349
}
4450
catch (Exception e)
4551
{

WebApi/Features/Auth/AuthController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public IActionResult ChallengeAuth(string role)
4949
var validateRole = _httpContext.HttpContext.User.IsInRole(role);
5050
if(!validateRole)
5151
{
52-
return Unauthorized();
52+
return Forbid();
5353
}
5454
}
5555

WebApi/Features/Employees/Details.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using AutoMapper;
66
using MediatR;
7+
using Microsoft.EntityFrameworkCore;
78
using WebApi.Infrastructure;
89

910
namespace WebApi.Features.Employees
@@ -45,7 +46,9 @@ public async Task<EmployeeViewModel> Handle(Query request, CancellationToken can
4546
try
4647
{
4748
// Find employee, get the Id param from Query
48-
var model = await _context.Employees.FindAsync(request.Id);
49+
var model = await _context.Employees
50+
.Include(m => m.Identity)
51+
.FirstAsync(m => m.Id == request.Id);
4952

5053
// Map model to view model
5154
return _mapper.Map<EmployeeViewModel>(model);

WebApi/Features/Logs/List.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public async Task<Object> Handle(Query request, CancellationToken cancellationTo
6161
var isEmployee = _httpContext.HttpContext.User.IsInRole("Employee");
6262
if (isEmployee)
6363
{
64-
// var username = _httpContext.HttpContext.User.FindFirst("sub");
65-
6664
// Get the username in sub type claim
6765
var username = _httpContext.HttpContext.User.Claims
6866
.First(m => m.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")

0 commit comments

Comments
 (0)