mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +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
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace SharpbinV3.Server.DTOs.Auth
|
||||
{
|
||||
public class LoginResponseDto
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string Token { get; set; } = string.Empty;
|
||||
public string RefreshToken { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class TokenRefreshResponseDto
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public TokenData? Token { get; set; }
|
||||
}
|
||||
|
||||
public class TokenData
|
||||
{
|
||||
public string? Token { get; set; }
|
||||
public string? RefreshToken { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using SharpbinV3.Server.DTOs.User;
|
||||
|
||||
namespace SharpbinV3.Server.DTOs.Paste
|
||||
{
|
||||
public class PasteResponseDto
|
||||
{
|
||||
public string ID { get; set; } = string.Empty;
|
||||
public Guid UUID { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? Syntax { get; set; }
|
||||
public long Size { get; set; }
|
||||
public long TrueSize { get; set; }
|
||||
public bool IsCompressed { get; set; }
|
||||
public int Views { get; set; }
|
||||
public int Visibility { get; set; }
|
||||
public long? EditedAt { get; set; }
|
||||
public long ExpiresAt { get; set; }
|
||||
public int? ReportCount { get; set; }
|
||||
public UserSimpleDto? Author { get; set; }
|
||||
}
|
||||
|
||||
public class PasteCreatedResponseDto
|
||||
{
|
||||
public string ID { get; set; } = string.Empty;
|
||||
public Guid UUID { get; set; }
|
||||
public bool IsCompressed { get; set; }
|
||||
public long Size { get; set; }
|
||||
public long TrueSize { get; set; }
|
||||
public long ExpiresAt { get; set; }
|
||||
}
|
||||
|
||||
public class PasteOptionsDto
|
||||
{
|
||||
public string[] Syntaxes { get; set; } = [];
|
||||
public List<PasteVisibilityOptionDto> Visibilities { get; set; } = [];
|
||||
public int MaxTitleLength { get; set; }
|
||||
public long MaxPasteSize { get; set; }
|
||||
public bool RequiresVerification { get; set; }
|
||||
}
|
||||
|
||||
public class PasteVisibilityOptionDto
|
||||
{
|
||||
public int Value { get; set; }
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SharpbinV3.Server.DTOs
|
||||
namespace SharpbinV3.Server.DTOs.Report
|
||||
{
|
||||
public class ReportDto
|
||||
{
|
||||
[Required]
|
||||
[MaxLength(1000)]
|
||||
[MinLength(12), MaxLength(1000)]
|
||||
public string Description { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string ReportType { get; set; } = string.Empty; // e.g., "Spam", "Abuse", etc.
|
||||
@@ -0,0 +1,17 @@
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
|
||||
namespace SharpbinV3.Server.DTOs.Report
|
||||
{
|
||||
public sealed record ReportQuery
|
||||
{
|
||||
public Guid? ReporterUUID { get; init; }
|
||||
public Guid? UserUUID { get; init; }
|
||||
public int? PastePID { get; init; }
|
||||
public string? PasteId { get; init; }
|
||||
public ReportTargetType? TargetType { get; init; }
|
||||
public ReportType? Type { get; init; }
|
||||
public ReportStatus? Status { get; init; }
|
||||
public int Page { get; init; } = 1;
|
||||
public int PageSize { get; init; } = 20;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
|
||||
namespace SharpbinV3.Server.DTOs.Report
|
||||
{
|
||||
public class ReportResponseDto
|
||||
{
|
||||
public int? ReportID { get; set; }
|
||||
public ReportType Type { get; set; }
|
||||
public ReportStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public long CreatedAt { get; set; }
|
||||
public long? UpdatedAt { get; set; }
|
||||
public Guid ReporterUUID { get; set; }
|
||||
public string? ReporterUsername { get; set; }
|
||||
public string? ReporterDisplayName { get; set; }
|
||||
public ReportTargetType TargetType { get; set; }
|
||||
public string? PasteId { get; set; }
|
||||
public string? PasteTitle { get; set; }
|
||||
public Guid? UserUUID { get; set; }
|
||||
public string? TargetUsername { get; set; }
|
||||
public string? TargetDisplayName { get; set; }
|
||||
}
|
||||
|
||||
public class ReportOptionsDto
|
||||
{
|
||||
public string[] Types { get; set; } = [];
|
||||
public string[] Statuses { get; set; } = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SharpbinV3.Server.DTOs.Report
|
||||
{
|
||||
public class ReportUpdateDto
|
||||
{
|
||||
public string? Status { get; set; }
|
||||
public string? Type { get; set; }
|
||||
[MaxLength(1000)]
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace SharpbinV3.Server.DTOs.User
|
||||
{
|
||||
public class UserSimpleDto
|
||||
{
|
||||
public int? UID { get; set; }
|
||||
public Guid UUID { get; set; }
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string? DisplayName { get; set; }
|
||||
public int[] Roles { get; set; } = [];
|
||||
public int Visibility { get; set; }
|
||||
}
|
||||
|
||||
public class UserResponseDto : UserSimpleDto
|
||||
{
|
||||
public string? Email { get; set; }
|
||||
public long? LastLogin { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ namespace SharpbinV3.Server.Data.Entities
|
||||
[Index(nameof(UserUUID))]
|
||||
public class Report
|
||||
{
|
||||
[Key]
|
||||
public int? ReportID { get; set; }
|
||||
|
||||
public ReportType Type { get; set; }
|
||||
|
||||
@@ -22,13 +22,13 @@ namespace SharpbinV3.Server.Extensions
|
||||
|
||||
var claims = context.User.Claims;
|
||||
|
||||
var uuidClaim = claims.FirstOrDefault(c => c.Type == "UUID")?.Value;
|
||||
var uuidClaim = claims.FirstOrDefault(c => c.Type == "uuid")?.Value;
|
||||
if (string.IsNullOrEmpty(uuidClaim) || !Guid.TryParse(uuidClaim, out var uuid))
|
||||
return null;
|
||||
|
||||
var username = claims.FirstOrDefault(c => c.Type == "Username")?.Value ?? "";
|
||||
var displayName = claims.FirstOrDefault(c => c.Type == "DisplayName")?.Value ?? "";
|
||||
var totpEnabled = claims.FirstOrDefault(c => c.Type == "TOTP_Enabled")?.Value == "True";
|
||||
var username = claims.FirstOrDefault(c => c.Type == "username")?.Value ?? "";
|
||||
var displayName = claims.FirstOrDefault(c => c.Type == "displayname")?.Value ?? "";
|
||||
var totpEnabled = claims.FirstOrDefault(c => c.Type == "totp_enabled")?.Value == "True";
|
||||
|
||||
var roles = claims
|
||||
.Where(c => c.Type == ClaimTypes.Role)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
@@ -13,6 +12,7 @@ using SharpbinV3.Server.Services.Verification;
|
||||
using SharpbinV3.Server.Services.Verification.Providers;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.RateLimiting;
|
||||
|
||||
namespace SharpbinV3.Server
|
||||
@@ -25,7 +25,12 @@ namespace SharpbinV3.Server
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
||||
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
||||
});
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddDbContext<AppDbContext>(opt =>
|
||||
|
||||
@@ -53,10 +53,10 @@ namespace SharpbinV3.Server.Services
|
||||
var totpEnabled = await _db.UserTotps.AnyAsync(t => t.UserUUID == user.UUID);
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new("UUID", user.UUID.ToString()),
|
||||
new("Username", user.Username),
|
||||
new("DisplayName", user.DisplayName ?? ""),
|
||||
new("TOTP_Enabled", totpEnabled.ToString()),
|
||||
new("uuid", user.UUID.ToString()),
|
||||
new("username", user.Username),
|
||||
new("displayname", user.DisplayName ?? ""),
|
||||
new("totp_enabled", totpEnabled.ToString()),
|
||||
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
||||
};
|
||||
|
||||
@@ -188,7 +188,7 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
public async Task<User?> GetUserFromHttpContext(HttpContext context)
|
||||
{
|
||||
var uuidClaim = context.User.Claims.FirstOrDefault(c => c.Type == "UUID")?.Value;
|
||||
var uuidClaim = context.User.Claims.FirstOrDefault(c => c.Type == "uuid")?.Value;
|
||||
if (uuidClaim == null)
|
||||
return null;
|
||||
var userUUID = Guid.Parse(uuidClaim);
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs.Report;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public class ReportService(AppDbContext db)
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private sealed record UserInfo(string Username, string? DisplayName);
|
||||
public async Task<Report> CreateReport(Guid reporterUUID, ReportTargetType targetType, Guid userUUID, ReportType type, string description)
|
||||
{
|
||||
var report = new Report
|
||||
@@ -36,22 +38,136 @@ namespace SharpbinV3.Server.Services
|
||||
return report;
|
||||
}
|
||||
|
||||
public async Task<List<Report>> GetReports(Guid reporterUUID)
|
||||
private IQueryable<Report> BuildReportQuery(ReportQuery query)
|
||||
{
|
||||
IQueryable<Report> q = _db.Reports;
|
||||
|
||||
if (query.ReporterUUID != null)
|
||||
q = q.Where(r => r.ReporterUUID == query.ReporterUUID);
|
||||
|
||||
if (query.UserUUID != null)
|
||||
q = q.Where(r => r.UserUUID == query.UserUUID);
|
||||
|
||||
if (query.PastePID != null)
|
||||
q = q.Where(r => r.PastePID == query.PastePID);
|
||||
|
||||
if (query.PasteId != null)
|
||||
{
|
||||
q = q.Where(r => _db.Pastes
|
||||
.Where(p => p.ID == query.PasteId)
|
||||
.Select(p => (int?)p.PID)
|
||||
.Contains(r.PastePID));
|
||||
}
|
||||
|
||||
if (query.TargetType != null)
|
||||
q = q.Where(r => r.TargetType == query.TargetType);
|
||||
|
||||
if (query.Type != null)
|
||||
q = q.Where(r => r.Type == query.Type);
|
||||
|
||||
if (query.Status != null)
|
||||
q = q.Where(r => r.Status == query.Status);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<ReportResponseDto>> GetPasteReports(ReportQuery query)
|
||||
{
|
||||
query = query with
|
||||
{
|
||||
TargetType = ReportTargetType.Paste,
|
||||
UserUUID = null
|
||||
};
|
||||
|
||||
return await GetReports(query);
|
||||
}
|
||||
|
||||
public async Task<List<ReportResponseDto>> GetUserReports(ReportQuery query)
|
||||
{
|
||||
query = query with
|
||||
{
|
||||
TargetType = ReportTargetType.User,
|
||||
PastePID = null
|
||||
};
|
||||
|
||||
return await GetReports(query);
|
||||
}
|
||||
|
||||
public async Task<List<ReportResponseDto>> GetReports(ReportQuery query)
|
||||
{
|
||||
return await BuildReportQuery(query)
|
||||
.OrderByDescending(r => r.CreatedAt)
|
||||
.Skip((query.Page - 1) * query.PageSize)
|
||||
.Take(query.PageSize)
|
||||
.Select(r => new ReportResponseDto
|
||||
{
|
||||
ReportID = r.ReportID,
|
||||
Type = r.Type,
|
||||
Status = r.Status,
|
||||
Description = r.Description,
|
||||
CreatedAt = r.CreatedAt,
|
||||
UpdatedAt = r.UpdatedAt,
|
||||
ReporterUUID = r.ReporterUUID,
|
||||
ReporterUsername = _db.Users
|
||||
.Where(u => u.UUID == r.ReporterUUID)
|
||||
.Select(u => u.Username)
|
||||
.FirstOrDefault(),
|
||||
ReporterDisplayName = _db.Users
|
||||
.Where(u => u.UUID == r.ReporterUUID)
|
||||
.Select(u => u.DisplayName)
|
||||
.FirstOrDefault(),
|
||||
TargetType = r.TargetType,
|
||||
PasteId = r.Paste != null ? r.Paste.ID : null,
|
||||
PasteTitle = r.Paste != null ? r.Paste.Title : null,
|
||||
UserUUID = r.UserUUID,
|
||||
TargetUsername = r.User != null ? r.User.Username : null,
|
||||
TargetDisplayName = r.User != null ? r.User.DisplayName : null
|
||||
})
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<(string? Username, string? DisplayName)> GetReporterInfo(Guid reporterUUID)
|
||||
{
|
||||
var info = await _db.Users.AsNoTracking()
|
||||
.Where(u => u.UUID == reporterUUID)
|
||||
.Select(u => new UserInfo(u.Username, u.DisplayName))
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return info is null ? (null, null) : (info.Username, info.DisplayName);
|
||||
}
|
||||
|
||||
|
||||
public async Task<int> GetReportCount(ReportTargetType targetType)
|
||||
{
|
||||
return await _db.Reports
|
||||
.Where(r => r.TargetType == targetType)
|
||||
.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<int> GetReportCountByReporter(Guid reporterUUID)
|
||||
{
|
||||
return await _db.Reports
|
||||
.Where(r => r.ReporterUUID == reporterUUID)
|
||||
.ToListAsync();
|
||||
.CountAsync();
|
||||
}
|
||||
public async Task<List<Report>> GetReports(ReportTargetType targetType, int? pastePID)
|
||||
|
||||
public async Task<int> GetReportCount()
|
||||
{
|
||||
return await _db.Reports
|
||||
.Where(r => r.TargetType == targetType && r.PastePID == pastePID)
|
||||
.ToListAsync();
|
||||
.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<int> GetReportCount(ReportQuery query)
|
||||
{
|
||||
return await BuildReportQuery(query).CountAsync();
|
||||
}
|
||||
|
||||
public async Task<Report?> GetReportByID(int reportID)
|
||||
{
|
||||
return await _db.Reports
|
||||
.Include(r => r.User)
|
||||
.Include(r => r.Paste)
|
||||
.FirstOrDefaultAsync(r => r.ReportID == reportID);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
"Audience": "SharpbinClient"
|
||||
},
|
||||
"PasteSettings": {
|
||||
"View_HMAC_Secret": "1341551a3f1ae93b98dc690b50dd514ed99da49459fda56d7b8c739d754b6e05cbd05ad820cbd2b77c246289d022e311a2e07df9bacb5a17caea38e125b97667",
|
||||
"View_HMAC_Internal_API_Key": "1fabd25116c87818550b11448ef609c572dd60b2aac2af9d893eb16b754f6dac"
|
||||
"View_HMAC_Secret": "e73433ba62483b5a97e2017f3ee784ed672c9888ec385dda24c6d77285764038640b1bcb8cf6a662568f31199c9096e10ed4eb5013b6658763a2ee0594a6278",
|
||||
"View_Internal_API_Key": "cccdd42d9f2f648493e402f1da7c855ea798ad510e6bb9ea712dd7bed838e58"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user