Skip to content

Commit c1543c8

Browse files
committed
Change directory of migrations folder
- Refactor AccountsController - [General] Fixed indention in `if` statements - Updated the property of GracePeriod to `int` in Config - Updated view model validations - Updated InitializeDatabase in SeedData.cs
1 parent 5442371 commit c1543c8

20 files changed

+113
-48
lines changed

src/Api/Constants/AttendanceConfig.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public class AttendanceConfig
44
{
55
public const string TimeIn = "09:00";
66
public const string TimeOut = "18:00";
7-
public const string GracePeriod = "15";
8-
7+
public const int GracePeriod = 15;
98
}
109
}

src/Api/Entities/Config.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class Config
77
public Guid Id { get; set; }
88
public string TimeIn { get; set; }
99
public string TimeOut { get; set; }
10-
public string GracePeriod { get; set; }
10+
public int GracePeriod { get; set; }
1111
}
1212
}

src/Api/Extensions/ListExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace WebApi.Extensions
77
public static class ListExtensions
88
{
99
/// <summary>
10-
///
10+
/// Apply sort filters based on parameters
1111
/// </summary>
1212
/// <param name="queryable"></param>
1313
/// <param name="parameters"></param>

src/Api/Features/Accounts/AccountsController.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Microsoft.AspNetCore.Authorization;
55
using WebApi.Entities;
66
using MediatR;
7+
using System.Security.Claims;
8+
using Microsoft.AspNetCore.Http;
79

810
namespace WebApi.Features.Accounts
911
{
@@ -12,10 +14,12 @@ namespace WebApi.Features.Accounts
1214
public class AccountsController : ControllerBase
1315
{
1416
private readonly IMediator _mediator;
17+
private readonly IHttpContextAccessor _httpContext;
1518

16-
public AccountsController(IMediator mediator)
19+
public AccountsController(IMediator mediator, IHttpContextAccessor httpContext)
1720
{
1821
_mediator = mediator;
22+
_httpContext = httpContext;
1923
}
2024

2125
// POST: api/accounts/register
@@ -25,11 +29,13 @@ public async Task<IActionResult> Register(RegisterViewModel viewModel)
2529
{
2630
// mediator from Features/Employees
2731
var isCardExist = await _mediator.Send(new Employees.IsCardExists.Query(Guid.Empty, viewModel.CardNo));
28-
if (isCardExist) return BadRequest("Card No. is already in use");
32+
if (isCardExist)
33+
return BadRequest("Card No. is already in use");
2934

3035
// mediator from Features/Employees
3136
var isUsernameExist = await _mediator.Send(new Auth.IsUserExists.Query(viewModel.UserName));
32-
if(isUsernameExist) return BadRequest($"Username {viewModel.UserName} is already taken");
37+
if (isUsernameExist)
38+
return BadRequest($"Username {viewModel.UserName} is already taken");
3339

3440
// Create user account
3541
var employeeInfo = await _mediator.Send(new Register.Command(viewModel));
@@ -40,11 +46,12 @@ public async Task<IActionResult> Register(RegisterViewModel viewModel)
4046
// PUT: api/accounts/update-password
4147
[Authorize(Roles = "Admin")]
4248
[HttpPut("update-password")]
43-
public async Task<IActionResult> UpdatePassword(ChangePasswordViewModel viewModel)
49+
public async Task<IActionResult> UpdatePassword(UpdatePasswordViewModel viewModel)
4450
{
4551
// Change a specific Employee account's password
4652
var result = await _mediator.Send(new UpdatePassword.Command(viewModel));
47-
if(!result) return StatusCode(500);
53+
if (!result)
54+
return BadRequest();
4855

4956
return Ok();
5057
}
@@ -54,12 +61,15 @@ public async Task<IActionResult> UpdatePassword(ChangePasswordViewModel viewMode
5461
public async Task<IActionResult> ChangePassword(ChangePasswordViewModel viewModel)
5562
{
5663
// Check if Old password is correct
57-
var validatePassword = await _mediator.Send(new Auth.ValidatePassword.Query(viewModel.UserName, viewModel.OldPassword));
58-
if (!validatePassword) return BadRequest("Incorrect password");
64+
var currentUser = _httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
65+
var validatePassword = await _mediator.Send(new Auth.ValidatePassword.Query(currentUser, viewModel.OldPassword));
66+
if (!validatePassword)
67+
return BadRequest("Incorrect password");
5968

6069
// Change account password
6170
var result = await _mediator.Send(new ChangePassword.Command(viewModel));
62-
if(!result.Succeeded) return BadRequest("Unable to change password");
71+
if (!result.Succeeded)
72+
return BadRequest("Unable to change password");
6373

6474
return Ok();
6575
}

src/Api/Features/Accounts/ChangePassword.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Security.Claims;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using MediatR;
@@ -40,9 +41,7 @@ public async Task<IdentityResult> Handle(Command request, CancellationToken canc
4041
try
4142
{
4243
// 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;
44+
var username = _httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
4645

4746
// Get account details
4847
var user = await _manager.FindByNameAsync(username);
@@ -51,7 +50,8 @@ public async Task<IdentityResult> Handle(Command request, CancellationToken canc
5150
return await _manager.ChangePasswordAsync(
5251
user,
5352
request.ViewModel.OldPassword,
54-
request.ViewModel.NewPassword);
53+
request.ViewModel.NewPassword
54+
);
5555
}
5656
catch (Exception e)
5757
{
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
using System.ComponentModel.DataAnnotations;
2+
13
namespace WebApi.Features.Accounts
24
{
35
public class ChangePasswordViewModel
46
{
5-
public string UserName { get; set; }
7+
[Required]
68
public string OldPassword { get; set; }
9+
[Required]
10+
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
711
public string NewPassword { get; set; }
812
}
913
}

src/Api/Features/Accounts/UpdatePassword.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using System;
3+
using System.Linq;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using MediatR;
@@ -15,12 +16,12 @@ public class UpdatePassword
1516
{
1617
public class Command : IRequest<bool>
1718
{
18-
public Command(ChangePasswordViewModel viewModel)
19+
public Command(UpdatePasswordViewModel viewModel)
1920
{
2021
ViewModel = viewModel;
2122
}
2223

23-
public ChangePasswordViewModel ViewModel { get; }
24+
public UpdatePasswordViewModel ViewModel { get; }
2425
}
2526

2627
public class CommandHandler : IRequestHandler<Command, bool>
@@ -36,14 +37,19 @@ public async Task<bool> Handle(Command request, CancellationToken cancellationTo
3637
{
3738
try
3839
{
40+
// Validate User
41+
var validateUser = _manager.Users.FirstOrDefault(m => m.UserName == request.ViewModel.UserName);
42+
if(validateUser == null)
43+
return false;
44+
3945
// Get account details
4046
var user = await _manager.FindByNameAsync(request.ViewModel.UserName);
4147

4248
// Remove the existing password
4349
await _manager.RemovePasswordAsync(user);
4450

4551
// Add the new password
46-
var result = await _manager.AddPasswordAsync(user, request.ViewModel.NewPassword);
52+
var result = await _manager.AddPasswordAsync(user, request.ViewModel.Password);
4753

4854
return (result.Succeeded) ? true : false;
4955
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace WebApi.Features.Accounts
4+
{
5+
public class UpdatePasswordViewModel
6+
{
7+
[Required]
8+
public string UserName { get; set; }
9+
[Required]
10+
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
11+
public string Password { get; set; }
12+
}
13+
}

src/Api/Features/Auth/AuthController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public async Task<IActionResult> Login(LoginViewModel viewModel)
2525
{
2626
// Check if credentials are correct
2727
var validate = await _mediator.Send(new ValidatePassword.Query(viewModel.UserName, viewModel.Password));
28-
if (!validate) return BadRequest("Invalid username or password");
28+
if (!validate)
29+
return BadRequest("Invalid username or password");
2930

3031
// Get User Claims
3132
var claimsIdentity = await _mediator.Send(new GetRoleClaimsIdentity.Command(viewModel));
@@ -48,9 +49,7 @@ public IActionResult ChallengeAuth(string role)
4849
{
4950
var validateRole = _httpContext.HttpContext.User.IsInRole(role);
5051
if(!validateRole)
51-
{
5252
return Forbid();
53-
}
5453
}
5554

5655
return Ok();

src/Api/Features/Auth/Extensions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@ public static long ToUnixEpochDate(DateTime date) =>
1818
/// </summary>
1919
public static void ThrowIfInvalidOptions(JwtIssuerOptions options)
2020
{
21-
if (options == null) throw new ArgumentNullException(nameof(options));
21+
if (options == null)
22+
throw new ArgumentNullException(nameof(options));
2223

2324
if (options.ValidFor <= TimeSpan.Zero)
24-
{
2525
throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(JwtIssuerOptions.ValidFor));
26-
}
2726

2827
if (options.SigningCredentials == null)
29-
{
3028
throw new ArgumentNullException(nameof(JwtIssuerOptions.SigningCredentials));
31-
}
3229

3330
if (options.JtiGenerator == null)
34-
{
3531
throw new ArgumentNullException(nameof(JwtIssuerOptions.JtiGenerator));
36-
}
3732
}
3833
}
3934
}

0 commit comments

Comments
 (0)