mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
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:
@@ -221,7 +221,8 @@ namespace SharpbinV2.Server.Controllers
|
|||||||
user.Email,
|
user.Email,
|
||||||
user.DisplayName,
|
user.DisplayName,
|
||||||
user.Created,
|
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." });
|
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." });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
namespace SharpbinV2.Server
|
|
||||||
{
|
|
||||||
public class Logging
|
|
||||||
{
|
|
||||||
public Logging()
|
|
||||||
{
|
|
||||||
if (!Directory.Exists("logs"))
|
|
||||||
Directory.CreateDirectory("logs");
|
|
||||||
}
|
|
||||||
public void LogInfo(string message)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"[{DateTime.Now} INFO] {message}");
|
|
||||||
File.AppendAllText($"logs/{DateTime.Now.ToString("yyyy-MM-dd")}.log", $"[{DateTime.Now} INFO] {message}\n");
|
|
||||||
}
|
|
||||||
public void LogWarning(string message)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
||||||
Console.WriteLine($"[{DateTime.Now} WARNING] {message}");
|
|
||||||
File.AppendAllText($"logs/{DateTime.Now.ToString("yyyy-MM-dd")}.log", $"[{DateTime.Now} WARNING] {message}\n");
|
|
||||||
Console.ResetColor();
|
|
||||||
}
|
|
||||||
public void LogError(string message)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
Console.WriteLine($"[{DateTime.Now} ERROR] {message}");
|
|
||||||
File.AppendAllText($"logs/{DateTime.Now.ToString("yyyy-MM-dd")}.log", $"[{DateTime.Now} ERROR] {message}\n");
|
|
||||||
Console.ResetColor();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -699,5 +699,69 @@ namespace SharpbinV2.Server.Services
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public async Task<bool> DeletePaste(Paste paste, ILogger logger)
|
||||||
|
{
|
||||||
|
if (paste == null || string.IsNullOrEmpty(paste.UUID))
|
||||||
|
return false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection(_connectionString))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "DELETE FROM pastes WHERE UUID = @UUID;";
|
||||||
|
command.Parameters.AddWithValue("@UUID", paste.UUID);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "DELETE FROM views WHERE PasteUUID = @PasteUUID;";
|
||||||
|
command.Parameters.AddWithValue("@PasteUUID", paste.UUID);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(paste.FilePath) && File.Exists(paste.FilePath))
|
||||||
|
{
|
||||||
|
File.Delete(paste.FilePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "Failed to delete paste from database.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public async Task<Paste?> UpdatePasteID(Paste paste, string newId, ILogger logger)
|
||||||
|
{
|
||||||
|
if (paste == null || string.IsNullOrEmpty(paste.UUID) || string.IsNullOrEmpty(newId))
|
||||||
|
return null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection(_connectionString))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "UPDATE pastes SET ID = @NewID, Fileath = @FilePath WHERE UUID = @UUID;";
|
||||||
|
command.Parameters.AddWithValue("@NewID", newId);
|
||||||
|
command.Parameters.AddWithValue("@FilePath", $"pastes/{newId}" ?? paste.FilePath);
|
||||||
|
command.Parameters.AddWithValue("@UUID", paste.UUID);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
paste.ID = newId;
|
||||||
|
paste.FilePath = $"pastes/{newId}";
|
||||||
|
return paste;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "Failed to update paste ID in database.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
@@ -9,6 +9,17 @@
|
|||||||
<SignAssembly>False</SignAssembly>
|
<SignAssembly>False</SignAssembly>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="logs\**" />
|
||||||
|
<Compile Remove="pastes\**" />
|
||||||
|
<Content Remove="logs\**" />
|
||||||
|
<Content Remove="pastes\**" />
|
||||||
|
<EmbeddedResource Remove="logs\**" />
|
||||||
|
<EmbeddedResource Remove="pastes\**" />
|
||||||
|
<None Remove="logs\**" />
|
||||||
|
<None Remove="pastes\**" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="Classes.cs" />
|
<Compile Remove="Classes.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -330,6 +330,10 @@ code.hljs {
|
|||||||
background: url('data:image/svg+xml;utf8,<svg fill="none" stroke="%23aaa" stroke-width="2" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><circle cx="10" cy="7.5" r="3.5"/><path d="M3 17c0-3.3 3.1-6 7-6s7 2.7 7 6"/></svg>') no-repeat center/1em 1em;
|
background: url('data:image/svg+xml;utf8,<svg fill="none" stroke="%23aaa" stroke-width="2" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><circle cx="10" cy="7.5" r="3.5"/><path d="M3 17c0-3.3 3.1-6 7-6s7 2.7 7 6"/></svg>') no-repeat center/1em 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.edit-icon {
|
||||||
|
background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-edit"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1" /><path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z" /><path d="M16 5l3 3" /></svg>') no-repeat center/1em 1em;
|
||||||
|
}
|
||||||
|
|
||||||
#password-modal-bg {
|
#password-modal-bg {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 0;
|
top: 0;
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ let totalPages = 1;
|
|||||||
let pastesPerPage = 10;
|
let pastesPerPage = 10;
|
||||||
let userPastes = [];
|
let userPastes = [];
|
||||||
|
|
||||||
|
const userTypes = {
|
||||||
|
"Admin": 255,
|
||||||
|
"User": 0
|
||||||
|
};
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
waitForAuthData().then(data => {
|
waitForAuthData().then(data => {
|
||||||
|
|||||||
@@ -163,6 +163,37 @@ function addInfo(paste) {
|
|||||||
document.getElementById("raw-button").addEventListener("click", function () {
|
document.getElementById("raw-button").addEventListener("click", function () {
|
||||||
window.location.href = `/raw/${paste.id}`;
|
window.location.href = `/raw/${paste.id}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
waitForAuthData().then(() => {
|
||||||
|
if (authData.user.uuid === paste.authorUUID || authData.user.type === 255) {
|
||||||
|
document.getElementById("delete-button").style.display = "inline-block";
|
||||||
|
document.getElementById("delete-button").addEventListener("click", function () {
|
||||||
|
if (confirm("Are you sure you want to delete this paste?")) {
|
||||||
|
fetch(`/api/pastes/${paste.id}`, {
|
||||||
|
method: "DELETE"
|
||||||
|
}).then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
showNotification("Paste deleted!", "info", 2000);
|
||||||
|
window.location.href = "/";
|
||||||
|
} else {
|
||||||
|
showNotification("Failed to delete paste.", "error", 2000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function waitForAuthData() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const checkAuth = setInterval(() => {
|
||||||
|
if (authData) {
|
||||||
|
clearInterval(checkAuth);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function addContent(content, syntax) {
|
function addContent(content, syntax) {
|
||||||
|
|||||||
@@ -21,7 +21,10 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="main-container">
|
<div class="main-container">
|
||||||
<div class="paste-header">
|
<div class="paste-header">
|
||||||
|
<div style="display: flex;">
|
||||||
<h1 id="paste-title">Title</h1>
|
<h1 id="paste-title">Title</h1>
|
||||||
|
<span class=" edit-icon" style="width: 32px; height: 32px;" id="edit-button" title="Edit Paste"></span>
|
||||||
|
</div>
|
||||||
<div class="paste-information-card">
|
<div class="paste-information-card">
|
||||||
<div id="info-throbber" class="throbber"></div>
|
<div id="info-throbber" class="throbber"></div>
|
||||||
<div class="paste-info-item"><span class="info-icon syntax-icon"></span><span id="paste-syntax">Syntax</span></div>
|
<div class="paste-info-item"><span class="info-icon syntax-icon"></span><span id="paste-syntax">Syntax</span></div>
|
||||||
@@ -36,6 +39,7 @@
|
|||||||
<button id="copy-button">Copy</button>
|
<button id="copy-button">Copy</button>
|
||||||
<button id="download-button">Download</button>
|
<button id="download-button">Download</button>
|
||||||
<button id="raw-button">Raw</button>
|
<button id="raw-button">Raw</button>
|
||||||
|
<button id="delete-button" style="display: none;">Delete</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="content-throbber" class="throbber"></div>
|
<div id="content-throbber" class="throbber"></div>
|
||||||
<pre id="paste-content">Loading...</pre>
|
<pre id="paste-content">Loading...</pre>
|
||||||
|
|||||||
Reference in New Issue
Block a user