ref: project structure
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Auth;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Permissions;
|
||||
using mws.backend.dotnet.domain.Roles;
|
||||
using mws.backend.dotnet.domain.Users;
|
||||
|
||||
namespace mws.backend.dotnet.application.Users;
|
||||
|
||||
public record CreateUserCommand(Guid ActorUserId, CreateUserRequest Request) : IRequest<UserDto>;
|
||||
|
||||
public class CreateUserHandler(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions, IMapper mapper)
|
||||
: IRequestHandler<CreateUserCommand, UserDto>
|
||||
{
|
||||
private const string Screen = "users";
|
||||
|
||||
public async Task<UserDto> Handle(CreateUserCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Create, ct);
|
||||
|
||||
var request = command.Request;
|
||||
var username = request.Username.Trim();
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(request.Password))
|
||||
{
|
||||
throw new BadRequestException("Username and password are required");
|
||||
}
|
||||
|
||||
if (await uow.Users.ExistsByUsernameAsync(username, ct))
|
||||
{
|
||||
throw new BadRequestException("Username already exists");
|
||||
}
|
||||
|
||||
Role? role = null;
|
||||
if (request.RoleId.HasValue)
|
||||
{
|
||||
role = await uow.Roles.GetByIdAsync(request.RoleId.Value, ct)
|
||||
?? throw new BadRequestException("Role not found");
|
||||
}
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Username = username,
|
||||
PasswordHash = passwordHasher.Hash(request.Password),
|
||||
DisplayName = request.DisplayName.Trim(),
|
||||
IsActive = true,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
UserRoles = role != null ? new List<UserRole> { new UserRole { RoleId = role.Id } } : new List<UserRole>()
|
||||
};
|
||||
|
||||
uow.Users.Add(user);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return mapper.Map<UserDto>(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Permissions;
|
||||
|
||||
namespace mws.backend.dotnet.application.Users;
|
||||
|
||||
public record DeleteUserCommand(Guid ActorUserId, Guid Id) : IRequest;
|
||||
|
||||
public class DeleteUserHandler(IUnitOfWork uow, IPermissionService permissions) : IRequestHandler<DeleteUserCommand>
|
||||
{
|
||||
private const string Screen = "users";
|
||||
|
||||
public async Task Handle(DeleteUserCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Delete, ct);
|
||||
|
||||
var user = await uow.Users.GetByIdAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("User not found");
|
||||
|
||||
var soleOwnerProjectIds = await uow.Projects.GetOwnedProjectIdsAsync(command.Id, ct);
|
||||
|
||||
foreach (var projectId in soleOwnerProjectIds)
|
||||
{
|
||||
var ownerCount = await uow.Projects.CountOwnersAsync(projectId, ct);
|
||||
if (ownerCount <= 1)
|
||||
{
|
||||
throw new BadRequestException("Cannot delete a user that is the sole owner of a project");
|
||||
}
|
||||
}
|
||||
|
||||
uow.Users.Remove(user);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Auth;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Permissions;
|
||||
|
||||
namespace mws.backend.dotnet.application.Users;
|
||||
|
||||
public record ResetPasswordCommand(Guid ActorUserId, Guid Id, ResetPasswordRequest Request) : IRequest;
|
||||
|
||||
public class ResetPasswordHandler(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions)
|
||||
: IRequestHandler<ResetPasswordCommand>
|
||||
{
|
||||
private const string Screen = "users";
|
||||
|
||||
public async Task Handle(ResetPasswordCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(command.Request.NewPassword))
|
||||
{
|
||||
throw new BadRequestException("New password is required");
|
||||
}
|
||||
|
||||
var user = await uow.Users.GetByIdAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("User not found");
|
||||
|
||||
user.PasswordHash = passwordHasher.Hash(command.Request.NewPassword);
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Permissions;
|
||||
|
||||
namespace mws.backend.dotnet.application.Users;
|
||||
|
||||
public record UpdateUserCommand(Guid ActorUserId, Guid Id, UpdateUserRequest Request) : IRequest<UserDto>;
|
||||
|
||||
public class UpdateUserHandler(IUnitOfWork uow, IPermissionService permissions, IMapper mapper)
|
||||
: IRequestHandler<UpdateUserCommand, UserDto>
|
||||
{
|
||||
private const string Screen = "users";
|
||||
|
||||
public async Task<UserDto> Handle(UpdateUserCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
var user = await uow.Users.GetByIdWithRoleAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("User not found");
|
||||
|
||||
var request = command.Request;
|
||||
if (request.RoleId.HasValue)
|
||||
{
|
||||
var role = await uow.Roles.GetByIdAsync(request.RoleId.Value, ct)
|
||||
?? throw new BadRequestException("Role not found");
|
||||
}
|
||||
|
||||
user.DisplayName = request.DisplayName.Trim();
|
||||
user.IsActive = request.IsActive;
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return mapper.Map<UserDto>(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user