Enhance paste management and user interface

- Updated `AuthController.cs` to include user `Type` in responses.
- Added `DeletePaste` and `ChangePasteID` endpoints in `PasteController.cs` with authentication and error handling.
- Removed `Logging.cs`, indicating a refactor of the logging mechanism.
- Implemented methods for deleting and updating paste IDs in `DatabaseService.cs`.
- Modified `SharpbinV2.Server.csproj` to exclude `logs` and `pastes` directories from compilation.
- Enhanced `paste.css` with styles for an edit icon.
- Introduced `userTypes` object in `dash.js` for better role management.
- Added delete button functionality in `paste.js` for authorized users.
- Updated `paste.html` to include edit and delete buttons for improved user experience.
This commit is contained in:
whoswhip
2025-07-10 16:07:35 -04:00
parent b95f540ef4
commit 57f1709032
9 changed files with 225 additions and 33 deletions
@@ -221,7 +221,8 @@ namespace SharpbinV2.Server.Controllers
user.Email,
user.DisplayName,
user.Created,
user.LastLogin
user.LastLogin,
user.Type
}
});
}
@@ -342,5 +342,108 @@ namespace SharpbinV2.Server.Controllers
return StatusCode(500, new { error = "An error occurred while retrieving the user's pastes." });
}
}
[HttpDelete("{id}")]
[EnableRateLimiting("general")]
public async Task<IActionResult> DeletePaste(string id)
{
var requestDetails = HelperService.GetRequestDetails(HttpContext);
if (string.IsNullOrWhiteSpace(requestDetails.Token) || await _databaseService.GetSession(requestDetails.Token) == null)
return Unauthorized(new { success = false, message = "Invalid or missing authentication token." });
User? user = await _databaseService.UserFromToken(requestDetails.Token ?? "");
if (user == null || user.UUID == "0")
return Unauthorized(new { success = false, message = "User not found." });
if (string.IsNullOrWhiteSpace(id))
return BadRequest(new { success = false, message = "Invalid paste ID." });
try
{
Paste? paste = await _databaseService.GetPasteFromID(id);
if (paste == null)
return NotFound(new { success = false, message = "Paste not found." });
if (paste.AuthorUUID != user.UUID && user.Type != 255)
return Unauthorized(new { success = false, message = "You do not have permission to delete this paste." });
bool success = await _databaseService.DeletePaste(paste, _logger);
if (!success)
return StatusCode(500, new { success, message = "An error occurred while deleting the paste." });
if (IOFile.Exists(paste.FilePath))
IOFile.Delete(paste.FilePath);
_logger.LogInformation($"Paste deleted successfully: {id}");
return Ok(new { success = true, message = "Paste deleted successfully." });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting paste");
return StatusCode(500, new { error = "An error occurred while deleting the paste." });
}
}
[HttpPost("{id}/change-id")]
[EnableRateLimiting("general")]
public async Task<IActionResult> ChangePasteID(string id, string newId)
{
var requestDetails = HelperService.GetRequestDetails(HttpContext);
if (string.IsNullOrWhiteSpace(requestDetails.Token) || await _databaseService.GetSession(requestDetails.Token) == null)
return Unauthorized(new { success = false, message = "Invalid or missing authentication token." });
User? user = await _databaseService.UserFromToken(requestDetails.Token ?? "");
if (user == null || user.UUID == "0")
return Unauthorized(new { success = false, message = "User not found." });
if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(newId))
return BadRequest(new { success = false, message = "Invalid paste ID." });
if (user.Type != 255)
return Unauthorized(new { success = false, message = "You do not have permission to change paste ID." });
try
{
var paste = await _databaseService.GetPasteFromID(id);
if (paste == null)
return NotFound(new { success = false, message = "Paste not found." });
var newPaste = await _databaseService.UpdatePasteID(paste, newId, _logger);
if (newPaste == null)
return StatusCode(500, new { success = false, message = "An error occurred while changing the paste ID." });
if (IOFile.Exists(paste.FilePath) && paste.ID != null)
{
string newFilePath = paste.FilePath.Replace(paste.ID, newId);
IOFile.Move(paste.FilePath, newFilePath);
paste.FilePath = newFilePath;
}
else
{
return StatusCode(500, new { success = false, message = "Paste file not found or invalid." });
}
return Ok(new
{
success = true,
message = "Paste ID changed successfully.",
paste = new
{
newPaste.ID,
newPaste.UUID,
newPaste.Title,
newPaste.Syntax,
newPaste.Visibility,
newPaste.Created,
newPaste.Size,
newPaste.TrueSize,
newPaste.AuthorUUID
}
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Error changing paste ID");
return StatusCode(500, new { error = "An error occurred while changing the paste ID." });
}
}
}
}