mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
feat: reports; improved+simplifed modals; extractError funcction;
refactor: change jwt claims to lowercase; use response dtos for sensitive data fix: nullify user on token clears; change appsettings.dev View_Internal_API_Key
This commit is contained in:
@@ -60,14 +60,16 @@ namespace SharpbinV3.Server.Controllers
|
||||
return Ok(new
|
||||
{
|
||||
message = "User registered successfully.",
|
||||
user = new
|
||||
user = new UserResponseDto
|
||||
{
|
||||
user.UID,
|
||||
user.UUID,
|
||||
user.Username,
|
||||
user.Email,
|
||||
user.DisplayName,
|
||||
user.Roles
|
||||
UID = user.UID,
|
||||
UUID = user.UUID,
|
||||
Username = user.Username,
|
||||
DisplayName = user.DisplayName,
|
||||
Email = user.Email,
|
||||
Roles = user.Roles,
|
||||
Visibility = user.Visibility,
|
||||
LastLogin = user.LastLogin
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -93,7 +95,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
user = await _userService.GetByUsername(request.Username);
|
||||
if (user == null && !string.IsNullOrEmpty(request.Email))
|
||||
user = await _userService.GetByEmail(request.Email);
|
||||
|
||||
|
||||
if (user == null || !Bcrypt.Verify(request.Password, user.PasswordHash))
|
||||
return BadRequest(new { success = false, message = "Invalid username/email or password." });
|
||||
|
||||
@@ -112,7 +114,13 @@ namespace SharpbinV3.Server.Controllers
|
||||
}
|
||||
var token = _authService.GenerateJWTToken(user);
|
||||
await _authService.UpdateLoginTime(user);
|
||||
return Ok(new { success = true, token.Result });
|
||||
var result = await token;
|
||||
return Ok(new LoginResponseDto
|
||||
{
|
||||
Success = true,
|
||||
Token = result.Token ?? string.Empty,
|
||||
RefreshToken = result.RefreshToken ?? string.Empty
|
||||
});
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("refresh")]
|
||||
@@ -122,15 +130,15 @@ namespace SharpbinV3.Server.Controllers
|
||||
try
|
||||
{
|
||||
var jwtResult = await _authService.RefreshJWTToken(request.Token, request.RefreshToken);
|
||||
return Ok(new
|
||||
return Ok(new TokenRefreshResponseDto
|
||||
{
|
||||
success = jwtResult?.Success,
|
||||
message = jwtResult?.Errors != null && jwtResult.Errors.Count > 0 ? string.Join("; ", jwtResult.Errors) : "Token refreshed successfully.",
|
||||
token = new
|
||||
Success = jwtResult?.Success ?? false,
|
||||
Message = jwtResult?.Errors != null && jwtResult.Errors.Count > 0 ? string.Join("; ", jwtResult.Errors) : "Token refreshed successfully.",
|
||||
Token = jwtResult != null ? new TokenData
|
||||
{
|
||||
jwtResult?.Token,
|
||||
jwtResult?.RefreshToken
|
||||
}
|
||||
Token = jwtResult.Token,
|
||||
RefreshToken = jwtResult.RefreshToken
|
||||
} : null
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -173,7 +181,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> EnableTotp([FromBody] EnableTotpDto request)
|
||||
{
|
||||
var uuidClaim = User.Claims.FirstOrDefault(c => c.Type == "UUID")?.Value;
|
||||
if (string.IsNullOrEmpty(uuidClaim))
|
||||
if (string.IsNullOrEmpty(uuidClaim))
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var uuid = Guid.Parse(uuidClaim);
|
||||
|
||||
@@ -3,8 +3,9 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs;
|
||||
using SharpbinV3.Server.DTOs.Paste;
|
||||
using SharpbinV3.Server.DTOs.Report;
|
||||
using SharpbinV3.Server.DTOs.User;
|
||||
using SharpbinV3.Server.Extensions;
|
||||
using SharpbinV3.Server.Services;
|
||||
using SharpbinV3.Server.Services.Verification;
|
||||
@@ -54,14 +55,14 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
var user = await _authService.GetUserFromHttpContext(HttpContext);
|
||||
Paste paste = await _pasteService.Create(user, content, title, syntax, visibility, expiresAt, _pasteSettings.EnablePasteCompression);
|
||||
return Ok(new
|
||||
return Ok(new PasteCreatedResponseDto
|
||||
{
|
||||
paste.ID,
|
||||
paste.UUID,
|
||||
paste.IsCompressed,
|
||||
paste.Size,
|
||||
paste.TrueSize,
|
||||
paste.ExpiresAt
|
||||
ID = paste.ID,
|
||||
UUID = paste.UUID,
|
||||
IsCompressed = paste.IsCompressed,
|
||||
Size = paste.Size,
|
||||
TrueSize = paste.TrueSize,
|
||||
ExpiresAt = paste.ExpiresAt
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,34 +71,50 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> GetPasteByID(string id)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null)
|
||||
return NotFound(new { success = false, message = "Paste not found"});
|
||||
if (paste == null)
|
||||
return NotFound(new { success = false, message = "Paste not found" });
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
|
||||
int? reportCount = null;
|
||||
if (jwtUser != null && jwtUser.Roles.Any(r => r == 1 || r == 255))
|
||||
{
|
||||
var query = new ReportQuery
|
||||
{
|
||||
PastePID = paste.PID
|
||||
};
|
||||
reportCount = await _reportService.GetReportCount(query);
|
||||
}
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
success = true,
|
||||
paste = new {
|
||||
paste.ID,
|
||||
paste.UUID,
|
||||
paste.Title,
|
||||
paste.Size,
|
||||
paste.TrueSize,
|
||||
paste.IsCompressed,
|
||||
paste.Views,
|
||||
paste.Syntax,
|
||||
paste.Visibility,
|
||||
paste.ExpiresAt,
|
||||
paste.EditedAt,
|
||||
Author = paste.User != null && paste.User.Visibility == 0 ? new
|
||||
paste = new PasteResponseDto
|
||||
{
|
||||
ID = paste.ID,
|
||||
UUID = paste.UUID,
|
||||
Title = paste.Title,
|
||||
Size = paste.Size,
|
||||
TrueSize = paste.TrueSize,
|
||||
IsCompressed = paste.IsCompressed,
|
||||
Views = paste.Views,
|
||||
Syntax = paste.Syntax,
|
||||
Visibility = paste.Visibility,
|
||||
ExpiresAt = paste.ExpiresAt,
|
||||
EditedAt = paste.EditedAt,
|
||||
ReportCount = reportCount,
|
||||
Author = paste.User != null && paste.User.Visibility == 0 ? new UserSimpleDto
|
||||
{
|
||||
paste.User.UID,
|
||||
paste.User.UUID,
|
||||
paste.User.Username,
|
||||
paste.User.DisplayName,
|
||||
paste.User.Visibility
|
||||
UID = paste.User.UID,
|
||||
UUID = paste.User.UUID,
|
||||
Username = paste.User.Username,
|
||||
DisplayName = paste.User.DisplayName,
|
||||
Visibility = paste.User.Visibility,
|
||||
Roles = paste.User.Roles
|
||||
} : null
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("{id}/raw")]
|
||||
public async Task<IActionResult> GetRawPasteByID(string id)
|
||||
@@ -116,15 +133,15 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> EditPaste(string id)
|
||||
{
|
||||
string content = await new StreamReader(Request.Body).ReadToEndAsync();
|
||||
if (content == null || id == null)
|
||||
if (content == null || id == null)
|
||||
return StatusCode(400, new { success = false, message = "Invalid request." });
|
||||
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null)
|
||||
if (paste == null)
|
||||
return StatusCode(404, new { success = false, message = "Paste not found." });
|
||||
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255);
|
||||
@@ -144,11 +161,11 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> ModifyPasteMetadata(string id, [FromBody] UpdatePasteDto request)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null)
|
||||
if (paste == null)
|
||||
return NotFound(new { success = false, message = "Paste not found." });
|
||||
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255);
|
||||
@@ -180,18 +197,18 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
success = true,
|
||||
message = "Paste metadata updated successfully.",
|
||||
paste = new
|
||||
paste = new PasteResponseDto
|
||||
{
|
||||
newPaste.ID,
|
||||
newPaste.UUID,
|
||||
newPaste.Title,
|
||||
newPaste.Size,
|
||||
newPaste.TrueSize,
|
||||
newPaste.IsCompressed,
|
||||
newPaste.Views,
|
||||
newPaste.Syntax,
|
||||
newPaste.Visibility,
|
||||
newPaste.ExpiresAt
|
||||
ID = newPaste.ID,
|
||||
UUID = newPaste.UUID,
|
||||
Title = newPaste.Title,
|
||||
Size = newPaste.Size,
|
||||
TrueSize = newPaste.TrueSize,
|
||||
IsCompressed = newPaste.IsCompressed,
|
||||
Views = newPaste.Views,
|
||||
Syntax = newPaste.Syntax,
|
||||
Visibility = newPaste.Visibility,
|
||||
ExpiresAt = newPaste.ExpiresAt
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -205,7 +222,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
if (paste == null) return NotFound(new { success = false, message = "Paste not found." });
|
||||
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255);
|
||||
@@ -225,10 +242,10 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> RecordPasteView(string id)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null)
|
||||
if (paste == null)
|
||||
return NotFound(new { success = false, message = "Paste not found." });
|
||||
|
||||
if (_pasteSettings.View_HMAC_Secret == null)
|
||||
if (_pasteSettings.View_HMAC_Secret == null)
|
||||
return StatusCode(500, new { success = false, message = "View recording is not configured properly." });
|
||||
if (!string.IsNullOrWhiteSpace(_pasteSettings.View_Internal_API_Key)
|
||||
&& _pasteSettings.View_Internal_API_Key != Request.Headers["X-Internal-API-Key"])
|
||||
@@ -236,9 +253,9 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
var result = await _pasteService.RecordView(paste, HttpContext);
|
||||
|
||||
if (result.paste is null && !result.alreadyExists)
|
||||
if (result.paste is null && !result.alreadyExists)
|
||||
return StatusCode(500, new { success = false, message = "An error occurred while recording the paste view." });
|
||||
else if (result.alreadyExists)
|
||||
else if (result.alreadyExists)
|
||||
return Ok(new { success = true, message = "View already recorded." });
|
||||
|
||||
return Ok(new { success = true, message = "Paste view recorded." });
|
||||
@@ -251,14 +268,14 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> ReportPaste(string id, [FromBody] ReportDto request)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null)
|
||||
if (paste == null)
|
||||
return NotFound(new { success = false, message = "Paste not found." });
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
Enum.TryParse<ReportType>(request.ReportType, true, out var reportType);
|
||||
if (!Enum.IsDefined(reportType))
|
||||
if (!Enum.IsDefined(reportType))
|
||||
return BadRequest(new { success = false, message = "Invalid report type." });
|
||||
|
||||
if (!await _verificationService.VerifyAsync(new VerificationContext
|
||||
@@ -270,14 +287,24 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
|
||||
Report report = await _reportService.CreateReport(user.UUID, ReportTargetType.Paste, paste.PID, reportType, request.Description);
|
||||
if (report == null)
|
||||
if (report == null)
|
||||
return StatusCode(500, new { success = false, message = "An error occurred while reporting the paste." });
|
||||
|
||||
return Ok(new { success = true, message = "Paste reported successfully.", report = new
|
||||
return Ok(new
|
||||
{
|
||||
success = true,
|
||||
message = "Paste reported successfully.",
|
||||
report = new ReportResponseDto
|
||||
{
|
||||
id = report.ReportID,
|
||||
reportType,
|
||||
description = report.Description
|
||||
ReportID = report.ReportID,
|
||||
Type = report.Type,
|
||||
Status = report.Status,
|
||||
Description = report.Description,
|
||||
CreatedAt = report.CreatedAt,
|
||||
UpdatedAt = report.UpdatedAt,
|
||||
ReporterUUID = report.ReporterUUID,
|
||||
TargetType = report.TargetType,
|
||||
UserUUID = report.UserUUID
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -289,15 +316,15 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> ModifyPasteReport(string id, int reportId, [FromBody] ReportDto request)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null)
|
||||
if (paste == null)
|
||||
return NotFound(new { success = false, message = "Paste not found." });
|
||||
|
||||
var report = await _reportService.GetReportByID(reportId);
|
||||
if (report == null || report.TargetType != ReportTargetType.Paste || report.PastePID != paste.PID)
|
||||
if (report == null || report.TargetType != ReportTargetType.Paste || report.PastePID != paste.PID)
|
||||
return NotFound(new { success = false, message = "Report not found." });
|
||||
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255);
|
||||
@@ -305,14 +332,14 @@ namespace SharpbinV3.Server.Controllers
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to modify this report." });
|
||||
|
||||
Enum.TryParse<ReportType>(request.ReportType, true, out var reportType);
|
||||
if (!Enum.IsDefined(reportType))
|
||||
if (!Enum.IsDefined(reportType))
|
||||
return BadRequest(new { success = false, message = "Invalid report type." });
|
||||
|
||||
report.Type = reportType;
|
||||
report.Description = request.Description;
|
||||
|
||||
var updatedReport = await _reportService.UpdateReport(report);
|
||||
if (updatedReport == null)
|
||||
if (updatedReport == null)
|
||||
return StatusCode(500, new { success = false, message = "An error occurred while updating the report." });
|
||||
|
||||
return Ok(new { success = true, message = "Report updated successfully." });
|
||||
@@ -325,15 +352,15 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> DeletePasteReport(string id, int reportId)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null)
|
||||
if (paste == null)
|
||||
return NotFound(new { success = false, message = "Paste not found." });
|
||||
|
||||
var report = await _reportService.GetReportByID(reportId);
|
||||
if (report == null || report.TargetType != ReportTargetType.Paste || report.PastePID != paste.PID)
|
||||
if (report == null || report.TargetType != ReportTargetType.Paste || report.PastePID != paste.PID)
|
||||
return NotFound(new { success = false, message = "Report not found." });
|
||||
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255);
|
||||
@@ -344,7 +371,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
return StatusCode(403, new { success = false, message = "2FA is required to perform this action." });
|
||||
|
||||
bool result = await _reportService.DeleteReport(report);
|
||||
if (!result)
|
||||
if (!result)
|
||||
return StatusCode(500, new { success = false, message = "An error occurred while deleting the report." });
|
||||
|
||||
return Ok(new { success = true, message = "Report deleted successfully." });
|
||||
@@ -355,19 +382,19 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> GetRecentPastes()
|
||||
{
|
||||
var results = await _pasteService.GetList(0, 50, true);
|
||||
return Ok(results.Select(p => new
|
||||
return Ok(results.Select(p => new PasteResponseDto
|
||||
{
|
||||
p.ID,
|
||||
p.UUID,
|
||||
p.Title,
|
||||
p.Size,
|
||||
p.TrueSize,
|
||||
p.IsCompressed,
|
||||
p.Views,
|
||||
p.Syntax,
|
||||
p.Visibility,
|
||||
p.ExpiresAt,
|
||||
p.EditedAt
|
||||
ID = p.ID,
|
||||
UUID = p.UUID,
|
||||
Title = p.Title,
|
||||
Size = p.Size,
|
||||
TrueSize = p.TrueSize,
|
||||
IsCompressed = p.IsCompressed,
|
||||
Views = p.Views,
|
||||
Syntax = p.Syntax,
|
||||
Visibility = p.Visibility,
|
||||
ExpiresAt = p.ExpiresAt,
|
||||
EditedAt = p.EditedAt
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -375,20 +402,19 @@ namespace SharpbinV3.Server.Controllers
|
||||
[Route("info")]
|
||||
public IActionResult GetCreatePasteOptions()
|
||||
{
|
||||
var options = new
|
||||
return Ok(new PasteOptionsDto
|
||||
{
|
||||
syntaxes = _pasteSettings.ValidSyntaxLanguages,
|
||||
visibilities = new[]
|
||||
{
|
||||
new { value = 0, displayName = "Public" },
|
||||
new { value = 1, displayName = "Unlisted" },
|
||||
new { value = 2, displayName = "Private" }
|
||||
},
|
||||
maxTitleLength = _pasteSettings.MaxTitleLength,
|
||||
maxPasteSize = _pasteSettings.MaxPasteSizeInBytes,
|
||||
requiresVerification = _pasteSettings.RequiresVerfication
|
||||
};
|
||||
return Ok(options);
|
||||
Syntaxes = _pasteSettings.ValidSyntaxLanguages,
|
||||
Visibilities =
|
||||
[
|
||||
new() { Value = 0, DisplayName = "Public" },
|
||||
new() { Value = 1, DisplayName = "Unlisted" },
|
||||
new() { Value = 2, DisplayName = "Private" }
|
||||
],
|
||||
MaxTitleLength = _pasteSettings.MaxTitleLength,
|
||||
MaxPasteSize = _pasteSettings.MaxPasteSizeInBytes,
|
||||
RequiresVerification = _pasteSettings.RequiresVerfication
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs.Report;
|
||||
using SharpbinV3.Server.Extensions;
|
||||
using SharpbinV3.Server.Services;
|
||||
|
||||
namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ReportController(ReportService reportService) : Controller
|
||||
{
|
||||
[Route("{reportId:int}")]
|
||||
[HttpGet]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
[Authorize(Policy = "AuthAndNotBanned")]
|
||||
public async Task<IActionResult> GetReportById(int reportId)
|
||||
{
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user is null)
|
||||
return Unauthorized(new { success = false, message = "You must be logged in to view reports." });
|
||||
|
||||
var report = await reportService.GetReportByID(reportId);
|
||||
if (report is null)
|
||||
return NotFound(new { success = false, message = "Report not found." });
|
||||
|
||||
var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255);
|
||||
if (!hasPrivilegedRole && report.ReporterUUID != user.UUID)
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to view this report." });
|
||||
|
||||
var (Username, DisplayName) = await reportService.GetReporterInfo(report.ReporterUUID);
|
||||
|
||||
return Ok(new ReportResponseDto
|
||||
{
|
||||
ReportID = report.ReportID,
|
||||
Type = report.Type,
|
||||
Status = report.Status,
|
||||
Description = report.Description,
|
||||
CreatedAt = report.CreatedAt,
|
||||
UpdatedAt = report.UpdatedAt,
|
||||
ReporterUUID = report.ReporterUUID,
|
||||
ReporterUsername = Username,
|
||||
ReporterDisplayName = DisplayName,
|
||||
TargetType = report.TargetType,
|
||||
PasteId = report.Paste?.ID,
|
||||
PasteTitle = report.Paste?.Title,
|
||||
UserUUID = report.UserUUID,
|
||||
TargetUsername = report.User?.Username,
|
||||
TargetDisplayName = report.User?.DisplayName
|
||||
});
|
||||
}
|
||||
|
||||
[Route("{reportId:int}")]
|
||||
[HttpPatch]
|
||||
[EnableRateLimiting("Sensitive")]
|
||||
[Authorize(Policy = "AuthAndNotBanned")]
|
||||
public async Task<IActionResult> UpdateReport(int reportId, [FromBody] ReportUpdateDto request)
|
||||
{
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user is null)
|
||||
return Unauthorized(new { success = false, message = "You must be logged in to update reports." });
|
||||
if (!user.Roles.Contains(1) && !user.Roles.Contains(255))
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to update reports." });
|
||||
|
||||
var report = await reportService.GetReportByID(reportId);
|
||||
if (report is null)
|
||||
return NotFound(new { success = false, message = "Report not found." });
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Type))
|
||||
{
|
||||
Enum.TryParse<ReportType>(request.Type, true, out var reportType);
|
||||
if (!Enum.IsDefined(reportType))
|
||||
return BadRequest(new { success = false, message = "Invalid report type." });
|
||||
report.Type = reportType;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Status))
|
||||
{
|
||||
Enum.TryParse<ReportStatus>(request.Status, true, out var reportStatus);
|
||||
if (!Enum.IsDefined(reportStatus))
|
||||
return BadRequest(new { success = false, message = "Invalid report status." });
|
||||
report.Status = reportStatus;
|
||||
}
|
||||
|
||||
if (request.Description != null)
|
||||
report.Description = request.Description;
|
||||
|
||||
report.UpdatedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
var updated = await reportService.UpdateReport(report);
|
||||
var (Username, DisplayName) = await reportService.GetReporterInfo(updated.ReporterUUID);
|
||||
|
||||
return Ok(new ReportResponseDto
|
||||
{
|
||||
ReportID = updated.ReportID,
|
||||
Type = updated.Type,
|
||||
Status = updated.Status,
|
||||
Description = updated.Description,
|
||||
CreatedAt = updated.CreatedAt,
|
||||
UpdatedAt = updated.UpdatedAt,
|
||||
ReporterUUID = updated.ReporterUUID,
|
||||
ReporterUsername = Username,
|
||||
ReporterDisplayName = DisplayName,
|
||||
TargetType = updated.TargetType,
|
||||
PasteId = updated.Paste?.ID,
|
||||
PasteTitle = updated.Paste?.Title,
|
||||
UserUUID = updated.UserUUID,
|
||||
TargetUsername = updated.User?.Username,
|
||||
TargetDisplayName = updated.User?.DisplayName
|
||||
});
|
||||
}
|
||||
|
||||
[Route("options")]
|
||||
[HttpGet]
|
||||
public IActionResult GetReportOptions()
|
||||
{
|
||||
var validTypes = Enum.GetNames<ReportType>();
|
||||
var statuses = Enum.GetNames<ReportStatus>();
|
||||
return Ok(new ReportOptionsDto { Types = validTypes, Statuses = statuses });
|
||||
}
|
||||
|
||||
[Route("pastes")]
|
||||
[HttpGet]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
[Authorize(Policy = "AuthAndNotBanned")]
|
||||
public async Task<IActionResult> GetReportsForPastes([FromQuery] ReportQuery request)
|
||||
{
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user is null)
|
||||
return Unauthorized(new { success = false, message = "You must be logged in to view reports." });
|
||||
if (!user.Roles.Contains(1) && !user.Roles.Contains(255))
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to view pastes." });
|
||||
|
||||
var query = request with
|
||||
{
|
||||
Page = Math.Max(request.Page, 1),
|
||||
PageSize = Math.Clamp(request.PageSize, 1, 100),
|
||||
UserUUID = null
|
||||
};
|
||||
|
||||
var reports = await reportService.GetPasteReports(query);
|
||||
var totalCount = await reportService.GetReportCount(query);
|
||||
var totalPages = (int)Math.Ceiling(totalCount / (double)query.PageSize);
|
||||
return Ok(new
|
||||
{
|
||||
Reports = reports,
|
||||
Pagination = new
|
||||
{
|
||||
query.Page,
|
||||
query.PageSize,
|
||||
TotalCount = totalCount,
|
||||
TotalPages = totalPages
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Route("users")]
|
||||
[HttpGet]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
[Authorize(Policy = "AuthAndNotBanned")]
|
||||
public async Task<IActionResult> GetReportsForUsers([FromQuery] ReportQuery request)
|
||||
{
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user is null)
|
||||
return Unauthorized(new { success = false, message = "You must be logged in to view reports." });
|
||||
if (!user.Roles.Contains(1) && !user.Roles.Contains(255))
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to view users." });
|
||||
|
||||
var query = request with
|
||||
{
|
||||
Page = Math.Max(request.Page, 1),
|
||||
PageSize = Math.Clamp(request.PageSize, 1, 100),
|
||||
PastePID = null,
|
||||
PasteId = null
|
||||
};
|
||||
|
||||
var reports = await reportService.GetUserReports(query);
|
||||
var totalCount = await reportService.GetReportCount(query);
|
||||
var totalPages = (int)Math.Ceiling(totalCount / (double)query.PageSize);
|
||||
return Ok(new
|
||||
{
|
||||
Reports = reports,
|
||||
Pagination = new
|
||||
{
|
||||
query.Page,
|
||||
query.PageSize,
|
||||
TotalCount = totalCount,
|
||||
TotalPages = totalPages
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Route("all")]
|
||||
[HttpGet]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
[Authorize(Policy = "AuthAndNotBanned")]
|
||||
public async Task<IActionResult> GetAllReports(
|
||||
[FromQuery] Guid? reporterUuid,
|
||||
[FromQuery] ReportTargetType? targetType,
|
||||
[FromQuery] ReportType? type,
|
||||
[FromQuery] ReportStatus? status,
|
||||
[FromQuery] int page = 1,
|
||||
[FromQuery] int pageSize = 20)
|
||||
{
|
||||
var user = HttpContext.GetJwtUser();
|
||||
if (user is null)
|
||||
return Unauthorized(new { success = false, message = "You must be logged in to view reports." });
|
||||
if (!user.Roles.Contains(1) && !user.Roles.Contains(255))
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to view reports." });
|
||||
|
||||
var query = new ReportQuery
|
||||
{
|
||||
ReporterUUID = reporterUuid,
|
||||
TargetType = targetType,
|
||||
Type = type,
|
||||
Status = status,
|
||||
Page = Math.Max(page, 1),
|
||||
PageSize = Math.Clamp(pageSize, 1, 100)
|
||||
};
|
||||
|
||||
var reports = await reportService.GetReports(query);
|
||||
var totalCount = await reportService.GetReportCount(query);
|
||||
var totalPages = (int)Math.Ceiling(totalCount / (double)query.PageSize);
|
||||
return Ok(new
|
||||
{
|
||||
Reports = reports,
|
||||
Pagination = new
|
||||
{
|
||||
query.Page,
|
||||
query.PageSize,
|
||||
TotalCount = totalCount,
|
||||
TotalPages = totalPages
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs;
|
||||
using SharpbinV3.Server.DTOs.Paste;
|
||||
using SharpbinV3.Server.DTOs.Report;
|
||||
using SharpbinV3.Server.DTOs.User;
|
||||
using SharpbinV3.Server.Extensions;
|
||||
using SharpbinV3.Server.Services;
|
||||
@@ -31,26 +33,26 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> GetByUsername(string username, [FromQuery] int page = 1)
|
||||
{
|
||||
var user = await _userService.GetByUsername(username, withPastes: true);
|
||||
return user == null ? NotFound(new { success = false, message = "User not found." }) : Ok(await BuildUserResponse(user, page));
|
||||
return user == null ? NotFound(new { success = false, message = "User not found." }) : await BuildUserResponse(user, page);
|
||||
}
|
||||
|
||||
[HttpGet("uuid/{uuid}")]
|
||||
public async Task<IActionResult> GetByUUID(Guid uuid, [FromQuery] int page = 1)
|
||||
{
|
||||
var user = await _userService.GetByUUID(uuid, withPastes: true);
|
||||
return user == null ? NotFound(new { success = false, message = "User not found."}) : Ok(await BuildUserResponse(user, page));
|
||||
return user == null ? NotFound(new { success = false, message = "User not found." }) : await BuildUserResponse(user, page);
|
||||
}
|
||||
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetMe([FromQuery] int page = 1)
|
||||
{
|
||||
var userUUID = HttpContext.User?.FindFirst("UUID")?.Value;
|
||||
var userUUID = HttpContext.User?.FindFirst("uuid")?.Value;
|
||||
if (userUUID is null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var user = await _userService.GetByUUID(Guid.Parse(userUUID), withPastes: true);
|
||||
return user == null ? NotFound(new { success = false, message = "User not found."}) : Ok(await BuildUserResponse(user, page));
|
||||
return user == null ? NotFound(new { success = false, message = "User not found." }) : await BuildUserResponse(user, page);
|
||||
}
|
||||
|
||||
[HttpPatch("uuid/{uuid}")]
|
||||
@@ -62,7 +64,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
if (jwtUser == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
@@ -107,11 +109,11 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> DeleteByUUID(Guid uuid, [FromBody] DeleteUserDto dto)
|
||||
{
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
if (jwtUser == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
|
||||
if (!jwtUser.Roles.Any(r => r == 255) && user.UUID != jwtUser.UUID)
|
||||
@@ -154,11 +156,11 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> ReportUser(Guid uuid, [FromBody] ReportDto request)
|
||||
{
|
||||
var reporter = HttpContext.GetJwtUser();
|
||||
if (reporter == null)
|
||||
if (reporter == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var reportedUser = await _userService.GetByUUID(uuid);
|
||||
if (reportedUser == null)
|
||||
if (reportedUser == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
if (reportedUser.UUID == reporter.UUID)
|
||||
return BadRequest(new { success = false, message = "You cannot report yourself." });
|
||||
@@ -180,12 +182,18 @@ namespace SharpbinV3.Server.Controllers
|
||||
return Ok(new
|
||||
{
|
||||
success = true,
|
||||
message = "Paste reported successfully.",
|
||||
report = new
|
||||
message = "User reported successfully.",
|
||||
report = new ReportResponseDto
|
||||
{
|
||||
id = report.ReportID,
|
||||
reportType,
|
||||
description = report.Description
|
||||
ReportID = report.ReportID,
|
||||
Type = report.Type,
|
||||
Status = report.Status,
|
||||
Description = report.Description,
|
||||
CreatedAt = report.CreatedAt,
|
||||
UpdatedAt = report.UpdatedAt,
|
||||
ReporterUUID = report.ReporterUUID,
|
||||
TargetType = report.TargetType,
|
||||
UserUUID = report.UserUUID
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -197,11 +205,11 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> UpdateUserReport(Guid uuid, int reportId, [FromBody] ReportDto request)
|
||||
{
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
if (jwtUser == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
|
||||
var report = await _reportService.GetReportByID(reportId);
|
||||
@@ -226,11 +234,11 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> DeleteUserReport(Guid uuid, int reportId)
|
||||
{
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
if (jwtUser == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
if (user == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
|
||||
var report = await _reportService.GetReportByID(reportId);
|
||||
@@ -244,9 +252,89 @@ namespace SharpbinV3.Server.Controllers
|
||||
return Ok(new { success = true, message = "Report deleted successfully." });
|
||||
}
|
||||
|
||||
private async Task<object> BuildUserResponse(User user, int page = 1)
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
[Route("{uuid}/reports")]
|
||||
public async Task<IActionResult> GetUserReports(Guid uuid, [FromQuery] ReportQuery request)
|
||||
{
|
||||
var isAuthenticatedUser = HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString();
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
|
||||
if (!jwtUser.Roles.Any(r => r == 1 || r == 255))
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to view this user's reports." });
|
||||
|
||||
var query = request with
|
||||
{
|
||||
UserUUID = user.UUID,
|
||||
Page = Math.Max(request.Page, 1),
|
||||
PageSize = Math.Clamp(request.PageSize, 1, 100)
|
||||
};
|
||||
var reports = await _reportService.GetUserReports(query);
|
||||
var totalCount = await _reportService.GetReportCount(query);
|
||||
var totalPages = (int)Math.Ceiling(totalCount / (double)query.PageSize);
|
||||
return Ok(new
|
||||
{
|
||||
Reports = reports,
|
||||
Pagination = new
|
||||
{
|
||||
query.Page,
|
||||
query.PageSize,
|
||||
TotalCount = totalCount,
|
||||
TotalPages = totalPages
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
[Route("{uuid}/reports/submitted")]
|
||||
public async Task<IActionResult> GetUserSubmittedReports(Guid uuid, [FromQuery] ReportQuery request)
|
||||
{
|
||||
var jwtUser = HttpContext.GetJwtUser();
|
||||
if (jwtUser == null)
|
||||
return Unauthorized(new { success = false, message = "Invalid token." });
|
||||
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null)
|
||||
return NotFound(new { success = false, message = "User not found." });
|
||||
|
||||
var isOwner = user.UUID == jwtUser.UUID;
|
||||
var hasPrivilegedRole = jwtUser.Roles.Any(r => r == 1 || r == 255);
|
||||
if (!isOwner && !hasPrivilegedRole)
|
||||
return StatusCode(403, new { success = false, message = "You do not have permission to view this user's submitted reports." });
|
||||
|
||||
var query = request with
|
||||
{
|
||||
ReporterUUID = user.UUID,
|
||||
Page = Math.Max(request.Page, 1),
|
||||
PageSize = Math.Clamp(request.PageSize, 1, 100)
|
||||
};
|
||||
|
||||
var reports = await _reportService.GetReports(query);
|
||||
var totalCount = await _reportService.GetReportCount(query);
|
||||
var totalPages = (int)Math.Ceiling(totalCount / (double)query.PageSize);
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
Reports = reports,
|
||||
Pagination = new
|
||||
{
|
||||
query.Page,
|
||||
query.PageSize,
|
||||
TotalCount = totalCount,
|
||||
TotalPages = totalPages
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<IActionResult> BuildUserResponse(User user, int page = 1)
|
||||
{
|
||||
var isAuthenticatedUser = HttpContext.User?.FindFirst("uuid")?.Value == user.UUID.ToString();
|
||||
var pasteQuery = _db.Pastes.AsNoTracking().Where(p => p.AuthorUUID == user.UUID);
|
||||
if (!isAuthenticatedUser)
|
||||
{
|
||||
@@ -268,18 +356,18 @@ namespace SharpbinV3.Server.Controllers
|
||||
.ThenByDescending(p => p.ID)
|
||||
.Skip(skip)
|
||||
.Take(pageSize)
|
||||
.Select(p => new
|
||||
.Select(p => new PasteResponseDto
|
||||
{
|
||||
p.ID,
|
||||
p.UUID,
|
||||
p.Title,
|
||||
p.Syntax,
|
||||
p.Size,
|
||||
p.TrueSize,
|
||||
p.Views,
|
||||
p.Visibility,
|
||||
p.EditedAt,
|
||||
p.ExpiresAt
|
||||
ID = p.ID,
|
||||
UUID = p.UUID,
|
||||
Title = p.Title,
|
||||
Syntax = p.Syntax,
|
||||
Size = p.Size,
|
||||
TrueSize = p.TrueSize,
|
||||
Views = p.Views,
|
||||
Visibility = p.Visibility,
|
||||
EditedAt = p.EditedAt,
|
||||
ExpiresAt = p.ExpiresAt
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
@@ -293,31 +381,54 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
if (isAuthenticatedUser)
|
||||
{
|
||||
return new
|
||||
var reports = await _reportService.GetReports(new ReportQuery
|
||||
{
|
||||
user.UID,
|
||||
user.Username,
|
||||
user.UUID,
|
||||
user.DisplayName,
|
||||
user.Email,
|
||||
user.LastLogin,
|
||||
user.Roles,
|
||||
user.Visibility,
|
||||
ReporterUUID = user.UUID,
|
||||
Page = page,
|
||||
PageSize = pageSize
|
||||
});
|
||||
var reportsCount = await _reportService.GetReportCountByReporter(user.UUID);
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
User = new UserResponseDto
|
||||
{
|
||||
UID = user.UID,
|
||||
Username = user.Username,
|
||||
UUID = user.UUID,
|
||||
DisplayName = user.DisplayName,
|
||||
Email = user.Email,
|
||||
LastLogin = user.LastLogin,
|
||||
Roles = user.Roles,
|
||||
Visibility = user.Visibility
|
||||
},
|
||||
Pastes = pastes,
|
||||
Pagination = pagination
|
||||
};
|
||||
Reports = reports,
|
||||
Pagination = pagination,
|
||||
ReportsPagination = new
|
||||
{
|
||||
Page = page,
|
||||
PageSize = pageSize,
|
||||
TotalCount = reportsCount,
|
||||
TotalPages = (int)Math.Ceiling(reportsCount / (double)pageSize)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new
|
||||
return Ok(new
|
||||
{
|
||||
user.UID,
|
||||
user.Username,
|
||||
user.UUID,
|
||||
user.DisplayName,
|
||||
user.Roles,
|
||||
User = new UserSimpleDto
|
||||
{
|
||||
UID = user.UID,
|
||||
Username = user.Username,
|
||||
UUID = user.UUID,
|
||||
DisplayName = user.DisplayName,
|
||||
Roles = user.Roles,
|
||||
Visibility = user.Visibility
|
||||
},
|
||||
Pastes = pastes,
|
||||
Pagination = pagination
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user