Add Role unsetting functionality
This commit is contained in:
29
src/Imprink.Application/Users/DeleteUserRoleHandler.cs
Normal file
29
src/Imprink.Application/Users/DeleteUserRoleHandler.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using Imprink.Application.Users.Dtos;
|
||||||
|
using Imprink.Domain.Entities.Users;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace Imprink.Application.Users;
|
||||||
|
|
||||||
|
public record DeleteUserRoleCommand(string Sub, Guid RoleId) : IRequest<UserRoleDto?>;
|
||||||
|
|
||||||
|
public class DeleteUserRoleHandler(IUnitOfWork uw) : IRequestHandler<DeleteUserRoleCommand, UserRoleDto?>
|
||||||
|
{
|
||||||
|
public async Task<UserRoleDto?> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken)) return null;
|
||||||
|
|
||||||
|
var userRole = new UserRole
|
||||||
|
{
|
||||||
|
UserId = request.Sub,
|
||||||
|
RoleId = request.RoleId
|
||||||
|
};
|
||||||
|
|
||||||
|
var removedRole = await uw.UserRoleRepository.RemoveUserRoleAsync(userRole, cancellationToken);
|
||||||
|
|
||||||
|
return new UserRoleDto
|
||||||
|
{
|
||||||
|
UserId = removedRole.UserId,
|
||||||
|
RoleId = removedRole.RoleId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@ namespace Imprink.WebApi.Controllers.Users;
|
|||||||
public class UserRoleController(IMediator mediator) : ControllerBase
|
public class UserRoleController(IMediator mediator) : ControllerBase
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpGet("me")]
|
[HttpGet("/me")]
|
||||||
public async Task<IActionResult> GetMyRoles()
|
public async Task<IActionResult> GetMyRoles()
|
||||||
{
|
{
|
||||||
var claims = User.Claims as Claim[] ?? User.Claims.ToArray();
|
var claims = User.Claims as Claim[] ?? User.Claims.ToArray();
|
||||||
@@ -23,7 +23,7 @@ public class UserRoleController(IMediator mediator) : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Authorize(Roles = "Admin")]
|
[Authorize(Roles = "Admin")]
|
||||||
[HttpPost("set")]
|
[HttpPost("/set")]
|
||||||
public async Task<IActionResult> SetUserRole(SetUserRoleCommand command)
|
public async Task<IActionResult> SetUserRole(SetUserRoleCommand command)
|
||||||
{
|
{
|
||||||
var userRole = await mediator.Send(command);
|
var userRole = await mediator.Send(command);
|
||||||
@@ -33,4 +33,16 @@ public class UserRoleController(IMediator mediator) : ControllerBase
|
|||||||
|
|
||||||
return Ok(userRole);
|
return Ok(userRole);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Authorize(Roles = "Admin")]
|
||||||
|
[HttpPost("/unset")]
|
||||||
|
public async Task<IActionResult> UnsetUserRole(DeleteUserRoleCommand command)
|
||||||
|
{
|
||||||
|
var userRole = await mediator.Send(command);
|
||||||
|
|
||||||
|
if (userRole == null)
|
||||||
|
return BadRequest();
|
||||||
|
|
||||||
|
return Ok(userRole);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user