diff --git a/API/Controllers/AccountController.cs b/API/Controllers/AccountController.cs index 78b7f23b7..71a8fcb69 100644 --- a/API/Controllers/AccountController.cs +++ b/API/Controllers/AccountController.cs @@ -4,8 +4,10 @@ using System.Threading.Tasks; using API.Constants; using API.DTOs; using API.Entities; +using API.Extensions; using API.Interfaces; using AutoMapper; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -36,6 +38,21 @@ namespace API.Controllers _mapper = mapper; } + [Authorize(Policy = "RequireAdminRole")] + [HttpPost("reset-password")] + public async Task UpdatePassword(ResetPasswordDto resetPasswordDto) + { + _logger.LogInformation($"{User.GetUsername()} is changing {resetPasswordDto.UserName}'s password."); + var user = await _userManager.Users.SingleAsync(x => x.UserName == resetPasswordDto.UserName); + var result = await _userManager.RemovePasswordAsync(user); + if (!result.Succeeded) return BadRequest("Unable to update password"); + + result = await _userManager.AddPasswordAsync(user, resetPasswordDto.Password); + if (!result.Succeeded) return BadRequest("Unable to update password"); + + return Ok($"{resetPasswordDto.UserName}'s Password has been reset."); + } + [HttpPost("register")] public async Task> Register(RegisterDto registerDto) { diff --git a/API/DTOs/ResetPasswordDto.cs b/API/DTOs/ResetPasswordDto.cs new file mode 100644 index 000000000..f486f4349 --- /dev/null +++ b/API/DTOs/ResetPasswordDto.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace API.DTOs +{ + public class ResetPasswordDto + { + [Required] + public string UserName { get; init; } + [Required] + [StringLength(8, MinimumLength = 4)] + public string Password { get; init; } + } +} \ No newline at end of file diff --git a/API/Middleware/ExceptionMiddleware.cs b/API/Middleware/ExceptionMiddleware.cs index 413fbdb16..5c168cf3d 100644 --- a/API/Middleware/ExceptionMiddleware.cs +++ b/API/Middleware/ExceptionMiddleware.cs @@ -25,7 +25,6 @@ namespace API.Middleware public async Task InvokeAsync(HttpContext context) { - _logger.LogError("The middleware called"); try { await _next(context); // downstream middlewares or http call diff --git a/API/Startup.cs b/API/Startup.cs index aaa2dae49..1aee5cecc 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -42,10 +42,8 @@ namespace API { app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1")); + app.UseHangfireDashboard(); } - - - app.UseHangfireDashboard(); app.UseHttpsRedirection();