From f40d4ab68c3aabf95b8775601b1534bda9665b4b Mon Sep 17 00:00:00 2001 From: whoswhip Date: Tue, 30 Dec 2025 13:35:10 -0500 Subject: [PATCH] feat: implement user update functionality by UUID and add validation to UpdateUserRequest --- .../Controllers/UserController.cs | 31 +++++++++++++++ SharpbinV3.Server/DTOs/UpdateUserRequest.cs | 38 +++++++++++++++++++ SharpbinV3.Server/Services/IUserService.cs | 1 + SharpbinV3.Server/Services/UserService.cs | 7 ++++ 4 files changed, 77 insertions(+) create mode 100644 SharpbinV3.Server/DTOs/UpdateUserRequest.cs diff --git a/SharpbinV3.Server/Controllers/UserController.cs b/SharpbinV3.Server/Controllers/UserController.cs index 4eaf111..b094c70 100644 --- a/SharpbinV3.Server/Controllers/UserController.cs +++ b/SharpbinV3.Server/Controllers/UserController.cs @@ -3,7 +3,9 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using SharpbinV3.Server.Data; using SharpbinV3.Server.Data.Entities; +using SharpbinV3.Server.DTOs; using SharpbinV3.Server.Services; +using System.Security.Claims; namespace SharpbinV3.Server.Controllers { @@ -40,6 +42,35 @@ namespace SharpbinV3.Server.Controllers return user == null ? NotFound() : Ok(await BuildUserResponse(user, page)); } + [HttpPatch("uuid/{uuid}")] + [Authorize] + public async Task UpdateByUUID(Guid uuid, [FromBody] UpdateUserRequest updatedUser) + { + if (!ModelState.IsValid) + return BadRequest(ModelState); + var jwtUser = HttpContext.User; + var uuidClaim = jwtUser?.FindFirst("UUID")?.Value; + if (uuidClaim == null || jwtUser == null) return Forbid(); + var userRoles = jwtUser.Claims + .Where(c => c.Type == ClaimTypes.Role) + .Select(c => int.Parse(c.Value)); + + var user = await _userService.GetByUUID(uuid); + if (user == null) + return NotFound(); + if (!userRoles.Any(r => r == 1 || r == 255) && user.UUID != Guid.Parse(uuidClaim)) + return Forbid(); + + user.DisplayName = updatedUser.DisplayName ?? user.DisplayName; + if (userRoles.Any(r => r == 255) || user.UUID == Guid.Parse(uuidClaim)) // only admins or self + { + user.Email = updatedUser.Email ?? user.Email; + user.Visibility = updatedUser.Visibility ?? user.Visibility; + } + await _db.SaveChangesAsync(); + return Ok(new { message = "User updated successfully." }); + } + private async Task BuildUserResponse(User user, int page = 1) { var isAuthenticatedUser = HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString(); diff --git a/SharpbinV3.Server/DTOs/UpdateUserRequest.cs b/SharpbinV3.Server/DTOs/UpdateUserRequest.cs new file mode 100644 index 0000000..334041b --- /dev/null +++ b/SharpbinV3.Server/DTOs/UpdateUserRequest.cs @@ -0,0 +1,38 @@ +using System.ComponentModel.DataAnnotations; + +namespace SharpbinV3.Server.DTOs +{ + public class UpdateUserRequest + { + public string? DisplayName { get; set; } + public string? Email { get; set; } + public int? Visibility { get; set; } = 0; + + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + if (DisplayName != null) { + if (DisplayName.Length < 3 || DisplayName.Length > 26) + { + results.Add(new ValidationResult("DisplayName must be between 3 and 50 characters.", [nameof(DisplayName)])); + } + } + if (Email != null) + { + var emailAttribute = new EmailAddressAttribute(); + if (!emailAttribute.IsValid(Email)) + { + results.Add(new ValidationResult("Invalid email format.", [nameof(Email)])); + } + } + if (Visibility != null) + { + if (Visibility < 0 || Visibility > 2) + { + results.Add(new ValidationResult("Visibility must be between 0 and 2.", [nameof(Visibility)])); + } + } + return results; + } + } +} diff --git a/SharpbinV3.Server/Services/IUserService.cs b/SharpbinV3.Server/Services/IUserService.cs index dc224d9..f67e2c4 100644 --- a/SharpbinV3.Server/Services/IUserService.cs +++ b/SharpbinV3.Server/Services/IUserService.cs @@ -8,5 +8,6 @@ namespace SharpbinV3.Server.Services Task GetByUsername(string username, bool withPastes = false); Task GetByUUID(Guid uuid, bool withPastes = false); Task GetByEmail(string email, bool withPastes = false); + Task Update(User user); } } diff --git a/SharpbinV3.Server/Services/UserService.cs b/SharpbinV3.Server/Services/UserService.cs index f6ec7cb..73f173a 100644 --- a/SharpbinV3.Server/Services/UserService.cs +++ b/SharpbinV3.Server/Services/UserService.cs @@ -39,5 +39,12 @@ namespace SharpbinV3.Server.Services query = query.Include(u => u.Pastes); return query.FirstOrDefaultAsync(u => u.Email == email); } + + public async Task Update(User user) + { + _db.Users.Update(user); + await _db.SaveChangesAsync(); + return user; + } } }