| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.API.Services; |
| | | 3 | | using System.Security.Claims; |
| | | 4 | | |
| | | 5 | | namespace Anichron.API.Endpoints; |
| | | 6 | | |
| | | 7 | | public static class AdminEndpoints |
| | | 8 | | { |
| | | 9 | | public static IEndpointRouteBuilder MapAdminEndpoints(this IEndpointRouteBuilder app) |
| | 50 | 10 | | { |
| | 50 | 11 | | var group = app.MapGroup(ApiPaths.Users.Group).WithTags("Users") |
| | 50 | 12 | | .RequireAdmin(); |
| | 50 | 13 | | group.MapPost(string.Empty, CreateUserAsync) |
| | 50 | 14 | | .RequireRateLimit(AuthRateLimitPolicies.Sensitive) |
| | 50 | 15 | | .Produces<AdminCreatedUserResponse>(StatusCodes.Status201Created) |
| | 50 | 16 | | .ProducesValidationProblem(StatusCodes.Status409Conflict) |
| | 50 | 17 | | .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity); |
| | 50 | 18 | | group.MapPost(ApiPaths.Users.PasswordReset, ResetUserPasswordAsync) |
| | 50 | 19 | | .RequireRateLimit(AuthRateLimitPolicies.Sensitive) |
| | 50 | 20 | | .Produces<AdminPasswordResetResponse>() |
| | 50 | 21 | | .Produces(StatusCodes.Status404NotFound); |
| | 50 | 22 | | group.MapGet(string.Empty, GetUsersAsync) |
| | 50 | 23 | | .Produces<List<AdminUserResponse>>(); |
| | 50 | 24 | | group.MapGet(ApiPaths.Users.ById, GetUserAsync) |
| | 50 | 25 | | .Produces<AdminUserResponse>() |
| | 50 | 26 | | .Produces(StatusCodes.Status404NotFound); |
| | 50 | 27 | | group.MapPatch(ApiPaths.Users.ById, PatchUserAsync) |
| | 50 | 28 | | .RequireRateLimit(AuthRateLimitPolicies.Sensitive) |
| | 50 | 29 | | .Produces<AdminUserResponse>() |
| | 50 | 30 | | .Produces(StatusCodes.Status404NotFound); |
| | 50 | 31 | | group.MapDelete(ApiPaths.Users.ById, DeleteUserAsync) |
| | 50 | 32 | | .RequireRateLimit(AuthRateLimitPolicies.Sensitive) |
| | 50 | 33 | | .Produces(StatusCodes.Status204NoContent) |
| | 50 | 34 | | .Produces(StatusCodes.Status404NotFound); |
| | 50 | 35 | | group.MapGet(ApiPaths.Users.UserStorageConfigs, GetStorageConfigsAsync) |
| | 50 | 36 | | .Produces<List<AdminStorageConfigResponse>>() |
| | 50 | 37 | | .Produces(StatusCodes.Status404NotFound); |
| | 50 | 38 | | group.MapPost(ApiPaths.Users.UserStorageConfigs, CreateStorageConfigAsync) |
| | 50 | 39 | | .RequireRateLimit(AuthRateLimitPolicies.Sensitive) |
| | 50 | 40 | | .Produces<AdminStorageConfigResponse>(StatusCodes.Status201Created) |
| | 50 | 41 | | .Produces(StatusCodes.Status404NotFound) |
| | 50 | 42 | | .ProducesValidationProblem(StatusCodes.Status409Conflict) |
| | 50 | 43 | | .ProducesValidationProblem(StatusCodes.Status422UnprocessableEntity); |
| | 50 | 44 | | group.MapDelete(ApiPaths.Users.UserStorageConfigById, DeleteStorageConfigAsync) |
| | 50 | 45 | | .RequireRateLimit(AuthRateLimitPolicies.Sensitive) |
| | 50 | 46 | | .Produces(StatusCodes.Status204NoContent) |
| | 50 | 47 | | .Produces(StatusCodes.Status404NotFound); |
| | 50 | 48 | | return app; |
| | 50 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static async Task<IResult> CreateUserAsync( |
| | | 52 | | CreateAdminUserRequest request, |
| | | 53 | | IAuthService auth, |
| | | 54 | | IAdminResponseMapper mapper, |
| | | 55 | | CancellationToken ct) |
| | 6 | 56 | | { |
| | 6 | 57 | | var result = await auth.AdminCreateUserAsync(request.Username, request.Email, ct); |
| | 6 | 58 | | return mapper.GetCreateUserResult(result); |
| | 6 | 59 | | } |
| | | 60 | | |
| | | 61 | | internal static async Task<IResult> ResetUserPasswordAsync( |
| | | 62 | | Guid userId, |
| | | 63 | | IAdminResetService adminReset, |
| | | 64 | | IAdminResponseMapper mapper, |
| | | 65 | | CancellationToken ct) |
| | 1 | 66 | | { |
| | 1 | 67 | | var result = await adminReset.ResetUserPasswordAsync(userId, ct); |
| | 1 | 68 | | return mapper.GetResetPasswordResult(result); |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | internal static async Task<IResult> GetUsersAsync( |
| | | 72 | | IAdminUserService adminUsers, |
| | | 73 | | IAdminResponseMapper mapper, |
| | | 74 | | CancellationToken ct) |
| | 1 | 75 | | { |
| | 1 | 76 | | var users = await adminUsers.GetAllAsync(ct); |
| | 1 | 77 | | return mapper.GetUsersResult(users); |
| | 1 | 78 | | } |
| | | 79 | | |
| | | 80 | | internal static async Task<IResult> GetUserAsync( |
| | | 81 | | Guid userId, |
| | | 82 | | IAdminUserService adminUsers, |
| | | 83 | | IAdminResponseMapper mapper, |
| | | 84 | | CancellationToken ct) |
| | 1 | 85 | | { |
| | 1 | 86 | | var user = await adminUsers.GetByIdAsync(userId, ct); |
| | 1 | 87 | | return mapper.GetUserResult(user); |
| | 1 | 88 | | } |
| | | 89 | | |
| | | 90 | | internal static async Task<IResult> PatchUserAsync( |
| | | 91 | | Guid userId, |
| | | 92 | | PatchAdminUserRequest request, |
| | | 93 | | ClaimsPrincipal caller, |
| | | 94 | | IAdminUserService adminUsers, |
| | | 95 | | IAdminResponseMapper mapper, |
| | | 96 | | CancellationToken ct) |
| | 4 | 97 | | { |
| | 4 | 98 | | if (!Guid.TryParse(caller.FindFirstValue(ClaimTypes.NameIdentifier), out var callerId)) |
| | 1 | 99 | | return Results.Unauthorized(); |
| | | 100 | | |
| | 3 | 101 | | var result = await adminUsers.UpdateAsync(callerId, userId, request.IsAdmin, request.IsDisabled, ct); |
| | 3 | 102 | | return mapper.GetPatchUserResult(result); |
| | 4 | 103 | | } |
| | | 104 | | |
| | | 105 | | internal static async Task<IResult> DeleteUserAsync( |
| | | 106 | | Guid userId, |
| | | 107 | | ClaimsPrincipal caller, |
| | | 108 | | IAdminUserService adminUsers, |
| | | 109 | | IAdminResponseMapper mapper, |
| | | 110 | | CancellationToken ct) |
| | 4 | 111 | | { |
| | 4 | 112 | | if (!Guid.TryParse(caller.FindFirstValue(ClaimTypes.NameIdentifier), out var callerId)) |
| | 1 | 113 | | return Results.Unauthorized(); |
| | | 114 | | |
| | 3 | 115 | | var result = await adminUsers.DeleteAsync(callerId, userId, ct); |
| | 3 | 116 | | return mapper.GetDeleteUserResult(result); |
| | 4 | 117 | | } |
| | | 118 | | |
| | | 119 | | internal static async Task<IResult> GetStorageConfigsAsync( |
| | | 120 | | Guid userId, |
| | | 121 | | IAdminStorageConfigService adminStorageConfigs, |
| | | 122 | | IAdminResponseMapper mapper, |
| | | 123 | | CancellationToken ct) |
| | 1 | 124 | | { |
| | 1 | 125 | | var result = await adminStorageConfigs.GetByUserIdAsync(userId, ct); |
| | 1 | 126 | | return mapper.GetStorageConfigsResult(result); |
| | 1 | 127 | | } |
| | | 128 | | |
| | | 129 | | internal static async Task<IResult> CreateStorageConfigAsync( |
| | | 130 | | Guid userId, |
| | | 131 | | CreateStorageConfigRequest request, |
| | | 132 | | IAdminStorageConfigService adminStorageConfigs, |
| | | 133 | | IAdminResponseMapper mapper, |
| | | 134 | | CancellationToken ct) |
| | 5 | 135 | | { |
| | 5 | 136 | | var result = await adminStorageConfigs.AddAsync(userId, request.RootPath, ct); |
| | 5 | 137 | | return mapper.GetCreateStorageConfigResult(result); |
| | 5 | 138 | | } |
| | | 139 | | |
| | | 140 | | internal static async Task<IResult> DeleteStorageConfigAsync( |
| | | 141 | | Guid userId, |
| | | 142 | | Guid configId, |
| | | 143 | | IAdminStorageConfigService adminStorageConfigs, |
| | | 144 | | IAdminResponseMapper mapper, |
| | | 145 | | CancellationToken ct) |
| | 1 | 146 | | { |
| | 1 | 147 | | var result = await adminStorageConfigs.DeleteAsync(userId, configId, ct); |
| | 1 | 148 | | return mapper.GetDeleteStorageConfigResult(result); |
| | 1 | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | public sealed record CreateAdminUserRequest(string Username, string Email); |
| | | 153 | | public sealed record AdminCreatedUserResponse(Guid Id, string Username, string Email, string TemporaryPassword); |
| | | 154 | | public sealed record AdminPasswordResetResponse(string TemporaryPassword); |
| | | 155 | | public sealed record AdminUserResponse(Guid Id, string Username, string Email, bool IsAdmin, bool IsDisabled, int Storag |
| | | 156 | | public sealed record PatchAdminUserRequest(bool? IsAdmin, bool? IsDisabled); |
| | | 157 | | public sealed record CreateStorageConfigRequest(string RootPath); |
| | | 158 | | public sealed record AdminStorageConfigResponse(Guid Id, Guid UserId, string RootPath, bool IsActive); |