| | | 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) |
| | 0 | 10 | | { |
| | 0 | 11 | | var group = app.MapGroup(ApiPaths.Users.Group).WithTags("Users") |
| | 0 | 12 | | .RequireAuthorization(AuthPolicies.Admin); |
| | 0 | 13 | | group.MapPost(string.Empty, CreateUserAsync) |
| | 0 | 14 | | .RequireRateLimiting(AuthRateLimitPolicies.Sensitive); |
| | 0 | 15 | | group.MapPost(ApiPaths.Users.PasswordReset, ResetUserPasswordAsync) |
| | 0 | 16 | | .RequireRateLimiting(AuthRateLimitPolicies.Sensitive); |
| | 0 | 17 | | group.MapGet(string.Empty, GetUsersAsync); |
| | 0 | 18 | | group.MapGet(ApiPaths.Users.ById, GetUserAsync); |
| | 0 | 19 | | group.MapPatch(ApiPaths.Users.ById, PatchUserAsync) |
| | 0 | 20 | | .RequireRateLimiting(AuthRateLimitPolicies.Sensitive); |
| | 0 | 21 | | group.MapDelete(ApiPaths.Users.ById, DeleteUserAsync) |
| | 0 | 22 | | .RequireRateLimiting(AuthRateLimitPolicies.Sensitive); |
| | 0 | 23 | | group.MapGet(ApiPaths.Users.UserStorageConfigs, GetStorageConfigsAsync); |
| | 0 | 24 | | group.MapPost(ApiPaths.Users.UserStorageConfigs, CreateStorageConfigAsync) |
| | 0 | 25 | | .RequireRateLimiting(AuthRateLimitPolicies.Sensitive); |
| | 0 | 26 | | group.MapDelete(ApiPaths.Users.UserStorageConfigById, DeleteStorageConfigAsync) |
| | 0 | 27 | | .RequireRateLimiting(AuthRateLimitPolicies.Sensitive); |
| | 0 | 28 | | return app; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static async Task<IResult> CreateUserAsync( |
| | | 32 | | CreateAdminUserRequest request, |
| | | 33 | | IAuthService auth, |
| | | 34 | | IAuthResponseMapper mapper, |
| | | 35 | | CancellationToken ct) |
| | 2 | 36 | | { |
| | 2 | 37 | | var result = await auth.AdminCreateUserAsync(request.Username, request.Email, ct); |
| | 2 | 38 | | return mapper.GetAdminCreateUserResult(result); |
| | 2 | 39 | | } |
| | | 40 | | |
| | | 41 | | internal static async Task<IResult> ResetUserPasswordAsync( |
| | | 42 | | Guid userId, |
| | | 43 | | IAdminResetService adminReset, |
| | | 44 | | IAuthResponseMapper mapper, |
| | | 45 | | CancellationToken ct) |
| | 1 | 46 | | { |
| | 1 | 47 | | var result = await adminReset.ResetUserPasswordAsync(userId, ct); |
| | 1 | 48 | | return mapper.GetAdminResetPasswordResult(result); |
| | 1 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static async Task<IResult> GetUsersAsync( |
| | | 52 | | IAdminUserService adminUsers, |
| | | 53 | | IAuthResponseMapper mapper, |
| | | 54 | | CancellationToken ct) |
| | 1 | 55 | | { |
| | 1 | 56 | | var users = await adminUsers.GetAllAsync(ct); |
| | 1 | 57 | | return mapper.GetAdminGetUsersResult(users); |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | internal static async Task<IResult> GetUserAsync( |
| | | 61 | | Guid userId, |
| | | 62 | | IAdminUserService adminUsers, |
| | | 63 | | IAuthResponseMapper mapper, |
| | | 64 | | CancellationToken ct) |
| | 1 | 65 | | { |
| | 1 | 66 | | var user = await adminUsers.GetByIdAsync(userId, ct); |
| | 1 | 67 | | return mapper.GetAdminGetUserResult(user); |
| | 1 | 68 | | } |
| | | 69 | | |
| | | 70 | | internal static async Task<IResult> PatchUserAsync( |
| | | 71 | | Guid userId, |
| | | 72 | | PatchAdminUserRequest request, |
| | | 73 | | ClaimsPrincipal caller, |
| | | 74 | | IAdminUserService adminUsers, |
| | | 75 | | IAuthResponseMapper mapper, |
| | | 76 | | CancellationToken ct) |
| | 2 | 77 | | { |
| | 2 | 78 | | if (!Guid.TryParse(caller.FindFirstValue(ClaimTypes.NameIdentifier), out var callerId)) |
| | 1 | 79 | | return Results.Unauthorized(); |
| | | 80 | | |
| | 1 | 81 | | var result = await adminUsers.UpdateAsync(callerId, userId, request.IsAdmin, request.IsDisabled, ct); |
| | 1 | 82 | | return mapper.GetAdminPatchUserResult(result); |
| | 2 | 83 | | } |
| | | 84 | | |
| | | 85 | | internal static async Task<IResult> DeleteUserAsync( |
| | | 86 | | Guid userId, |
| | | 87 | | ClaimsPrincipal caller, |
| | | 88 | | IAdminUserService adminUsers, |
| | | 89 | | IAuthResponseMapper mapper, |
| | | 90 | | CancellationToken ct) |
| | 2 | 91 | | { |
| | 2 | 92 | | if (!Guid.TryParse(caller.FindFirstValue(ClaimTypes.NameIdentifier), out var callerId)) |
| | 1 | 93 | | return Results.Unauthorized(); |
| | | 94 | | |
| | 1 | 95 | | var result = await adminUsers.DeleteAsync(callerId, userId, ct); |
| | 1 | 96 | | return mapper.GetAdminDeleteUserResult(result); |
| | 2 | 97 | | } |
| | | 98 | | |
| | | 99 | | internal static async Task<IResult> GetStorageConfigsAsync( |
| | | 100 | | Guid userId, |
| | | 101 | | IAdminStorageConfigService adminStorageConfigs, |
| | | 102 | | IAuthResponseMapper mapper, |
| | | 103 | | CancellationToken ct) |
| | 1 | 104 | | { |
| | 1 | 105 | | var result = await adminStorageConfigs.GetByUserIdAsync(userId, ct); |
| | 1 | 106 | | return mapper.GetAdminGetStorageConfigsResult(result); |
| | 1 | 107 | | } |
| | | 108 | | |
| | | 109 | | internal static async Task<IResult> CreateStorageConfigAsync( |
| | | 110 | | Guid userId, |
| | | 111 | | CreateStorageConfigRequest request, |
| | | 112 | | IAdminStorageConfigService adminStorageConfigs, |
| | | 113 | | IAuthResponseMapper mapper, |
| | | 114 | | CancellationToken ct) |
| | 1 | 115 | | { |
| | 1 | 116 | | var result = await adminStorageConfigs.AddAsync(userId, request.RootPath, ct); |
| | 1 | 117 | | return mapper.GetAdminCreateStorageConfigResult(result); |
| | 1 | 118 | | } |
| | | 119 | | |
| | | 120 | | internal static async Task<IResult> DeleteStorageConfigAsync( |
| | | 121 | | Guid userId, |
| | | 122 | | Guid configId, |
| | | 123 | | IAdminStorageConfigService adminStorageConfigs, |
| | | 124 | | IAuthResponseMapper mapper, |
| | | 125 | | CancellationToken ct) |
| | 1 | 126 | | { |
| | 1 | 127 | | var result = await adminStorageConfigs.DeleteAsync(userId, configId, ct); |
| | 1 | 128 | | return mapper.GetAdminDeleteStorageConfigResult(result); |
| | 1 | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | public sealed record CreateAdminUserRequest(string Username, string Email); |
| | | 133 | | public sealed record AdminCreatedUserResponse(Guid Id, string Username, string Email, string TemporaryPassword); |
| | | 134 | | public sealed record AdminPasswordResetResponse(string TemporaryPassword); |
| | | 135 | | public sealed record AdminUserResponse(Guid Id, string Username, string Email, bool IsAdmin, bool IsDisabled, int Storag |
| | | 136 | | public sealed record PatchAdminUserRequest(bool? IsAdmin, bool? IsDisabled); |
| | | 137 | | public sealed record CreateStorageConfigRequest(string RootPath); |
| | | 138 | | public sealed record AdminStorageConfigResponse(Guid Id, Guid UserId, string RootPath, bool IsActive); |