mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
@@ -16,13 +16,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController(
|
||||
UserService userService,
|
||||
AppDbContext db,
|
||||
TotpVerificationProvider totp,
|
||||
VerificationService verificationService,
|
||||
IOptions<AuthSettings> authSettings
|
||||
) : ControllerBase
|
||||
public class UserController(UserService userService, AppDbContext db, TotpVerificationProvider totp, VerificationService verificationService, IOptions<AuthSettings> authSettings) : ControllerBase
|
||||
{
|
||||
private readonly UserService _userService = userService;
|
||||
private readonly AppDbContext _db = db;
|
||||
@@ -58,16 +52,12 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
[HttpPatch("uuid/{uuid}")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> UpdateByUUID(
|
||||
Guid uuid,
|
||||
[FromBody] UpdateUserDto updatedUser
|
||||
)
|
||||
public async Task<IActionResult> UpdateByUUID(Guid uuid, [FromBody] UpdateUserDto updatedUser)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
return Forbid();
|
||||
if (jwtUser == null) return Forbid();
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
return NotFound();
|
||||
@@ -75,11 +65,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
return Forbid();
|
||||
if (user.Roles.Contains(255) && !jwtUser.Roles.Contains(255))
|
||||
return Forbid();
|
||||
if (
|
||||
jwtUser.Roles.Contains(255)
|
||||
&& !jwtUser.TotpEnabled
|
||||
&& _authSettings.Admins_Require_2FA
|
||||
)
|
||||
if (jwtUser.Roles.Contains(255) && !jwtUser.TotpEnabled && _authSettings.Admins_Require_2FA)
|
||||
return Forbid();
|
||||
|
||||
user.DisplayName = updatedUser.DisplayName ?? user.DisplayName;
|
||||
@@ -92,16 +78,11 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
if (updatedUser.Roles.Contains(255) && jwtUser.TotpEnabled)
|
||||
{
|
||||
if (
|
||||
string.IsNullOrEmpty(updatedUser.TotpCode)
|
||||
|| !await _totp.VerifyAsync(
|
||||
new VerificationContext
|
||||
{
|
||||
UserUUID = jwtUser.UUID,
|
||||
Code = updatedUser.TotpCode,
|
||||
}
|
||||
)
|
||||
)
|
||||
if (string.IsNullOrEmpty(updatedUser.TotpCode) || !await _totp.VerifyAsync(new VerificationContext
|
||||
{
|
||||
UserUUID = jwtUser.UUID,
|
||||
Code = updatedUser.TotpCode
|
||||
}))
|
||||
{
|
||||
return Unauthorized(new { message = "Invalid TOTP code." });
|
||||
}
|
||||
@@ -118,44 +99,32 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> DeleteByUUID(Guid uuid, [FromBody] DeleteUserDto dto)
|
||||
{
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
return Forbid();
|
||||
if (jwtUser == null) return Forbid();
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
return Forbid();
|
||||
if (user == null) return Forbid();
|
||||
if (!jwtUser.Roles.Any(r => r == 255) && user.UUID != jwtUser.UUID)
|
||||
return Forbid();
|
||||
if (
|
||||
jwtUser.Roles.Contains(255)
|
||||
&& !jwtUser.TotpEnabled
|
||||
&& _authSettings.Admins_Require_2FA
|
||||
)
|
||||
if (jwtUser.Roles.Contains(255) && !jwtUser.TotpEnabled && _authSettings.Admins_Require_2FA)
|
||||
return Forbid();
|
||||
|
||||
if (jwtUser.TotpEnabled)
|
||||
{
|
||||
if (
|
||||
string.IsNullOrEmpty(dto.TotpCode)
|
||||
|| !await _totp.VerifyAsync(
|
||||
new VerificationContext { UserUUID = jwtUser.UUID, Code = dto.TotpCode }
|
||||
)
|
||||
)
|
||||
if (string.IsNullOrEmpty(dto.TotpCode) || !await _totp.VerifyAsync(new VerificationContext
|
||||
{
|
||||
UserUUID = jwtUser.UUID,
|
||||
Code = dto.TotpCode
|
||||
}))
|
||||
{
|
||||
return Unauthorized(new { message = "Invalid TOTP code." });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (
|
||||
string.IsNullOrEmpty(dto.Token)
|
||||
|| !await _verificationService.VerifyAsync(
|
||||
new VerificationContext
|
||||
{
|
||||
Token = dto.Token,
|
||||
Ip = HttpContext.GetRequestIP(),
|
||||
}
|
||||
)
|
||||
)
|
||||
if (string.IsNullOrEmpty(dto.Token) || !await _verificationService.VerifyAsync(new VerificationContext
|
||||
{
|
||||
Token = dto.Token,
|
||||
Ip = HttpContext.GetRequestIP()
|
||||
}))
|
||||
{
|
||||
return Unauthorized(new { message = "Invalid verification token." });
|
||||
}
|
||||
@@ -168,8 +137,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
private async Task<object> BuildUserResponse(User user, int page = 1)
|
||||
{
|
||||
var isAuthenticatedUser =
|
||||
HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString();
|
||||
var isAuthenticatedUser = HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString();
|
||||
var pasteQuery = _db.Pastes.AsNoTracking().Where(p => p.AuthorUUID == user.UUID);
|
||||
if (!isAuthenticatedUser)
|
||||
{
|
||||
@@ -202,7 +170,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
p.Views,
|
||||
p.Visibility,
|
||||
p.EditedAt,
|
||||
p.ExpiresAt,
|
||||
p.ExpiresAt
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
@@ -211,7 +179,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
Page = page,
|
||||
PageSize = pageSize,
|
||||
TotalCount = totalCount,
|
||||
TotalPages = totalPages,
|
||||
TotalPages = totalPages
|
||||
};
|
||||
|
||||
if (isAuthenticatedUser)
|
||||
@@ -227,7 +195,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
user.Roles,
|
||||
user.Visibility,
|
||||
Pastes = pastes,
|
||||
Pagination = pagination,
|
||||
Pagination = pagination
|
||||
};
|
||||
}
|
||||
|
||||
@@ -239,7 +207,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
user.DisplayName,
|
||||
user.Roles,
|
||||
Pastes = pastes,
|
||||
Pagination = pagination,
|
||||
Pagination = pagination
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user