mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Implemented ability to reset a user's password.
This commit is contained in:
parent
6309ae9dd3
commit
8220709b4c
@ -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<ActionResult> 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<ActionResult<UserDto>> Register(RegisterDto registerDto)
|
||||
{
|
||||
|
13
API/DTOs/ResetPasswordDto.cs
Normal file
13
API/DTOs/ResetPasswordDto.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -42,11 +42,9 @@ namespace API
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
|
||||
app.UseHangfireDashboard();
|
||||
}
|
||||
|
||||
|
||||
app.UseHangfireDashboard();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
Loading…
x
Reference in New Issue
Block a user