mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: admins can change user's roles
refactor: banned role has priority over all other roles when displaying chore: npm run format
This commit is contained in:
@@ -1,19 +1,26 @@
|
||||
<script lang="ts">
|
||||
export let show = false;
|
||||
export let mode: 'decrypt' | 'encrypt' | 'confirm' | 'prompt' = 'decrypt';
|
||||
export let mode: 'decrypt' | 'encrypt' | 'confirm' | 'prompt' | 'multiselect' = 'decrypt';
|
||||
export let title = '';
|
||||
export let message = '';
|
||||
export let error = '';
|
||||
export let placeholder = '';
|
||||
export let inputType = 'text';
|
||||
export let onConfirm: (value: string | boolean) => void;
|
||||
export let items: { label: string; value: any }[] = [];
|
||||
export let initialValue: any = null;
|
||||
export let onConfirm: (value: any) => void;
|
||||
export let onCancel: () => void;
|
||||
export let confirmButtonText = '';
|
||||
|
||||
let inputValue = '';
|
||||
let inputValue: any = '';
|
||||
|
||||
$: if (!show) {
|
||||
inputValue = '';
|
||||
inputValue =
|
||||
initialValue !== null
|
||||
? JSON.parse(JSON.stringify(initialValue))
|
||||
: mode === 'multiselect'
|
||||
? []
|
||||
: '';
|
||||
}
|
||||
|
||||
function handleSubmit() {
|
||||
@@ -32,15 +39,20 @@
|
||||
? 'Enter password to encrypt'
|
||||
: mode === 'confirm'
|
||||
? 'Confirm action'
|
||||
: 'Enter value');
|
||||
: mode === 'multiselect'
|
||||
? 'Select items'
|
||||
: 'Enter value');
|
||||
|
||||
$: confirmLabel =
|
||||
confirmButtonText ||
|
||||
(mode === 'decrypt' ? 'Decrypt' :
|
||||
mode === 'encrypt' ? 'Encrypt' :
|
||||
mode === 'confirm' ? 'Delete' :
|
||||
'Confirm');
|
||||
$: shouldShowInput = mode !== 'confirm';
|
||||
(mode === 'decrypt'
|
||||
? 'Decrypt'
|
||||
: mode === 'encrypt'
|
||||
? 'Encrypt'
|
||||
: mode === 'confirm'
|
||||
? 'Delete'
|
||||
: 'Confirm');
|
||||
$: shouldShowInput = mode !== 'confirm' && mode !== 'multiselect';
|
||||
</script>
|
||||
|
||||
{#if show}
|
||||
@@ -68,6 +80,26 @@
|
||||
/>
|
||||
{/if}
|
||||
|
||||
{#if mode === 'multiselect'}
|
||||
<div
|
||||
class="mb-4 max-h-60 overflow-y-auto rounded border border-neutral-700 bg-neutral-800 p-2"
|
||||
>
|
||||
{#each items as item}
|
||||
<label
|
||||
class="flex cursor-pointer items-center gap-2 rounded p-2 text-white hover:bg-neutral-700"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:group={inputValue}
|
||||
value={item.value}
|
||||
class="h-4 w-4 rounded border-neutral-600 bg-neutral-700 text-neutral-500"
|
||||
/>
|
||||
<span>{item.label}</span>
|
||||
</label>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if error}
|
||||
<div class="mb-3 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200">
|
||||
{error}
|
||||
@@ -78,7 +110,9 @@
|
||||
<button
|
||||
type="submit"
|
||||
class={`flex-1 cursor-pointer rounded px-4 py-2 font-semibold text-white transition-colors ${
|
||||
mode === 'confirm' ? 'bg-red-900 hover:bg-red-800' : 'bg-neutral-700 hover:bg-neutral-800'
|
||||
mode === 'confirm'
|
||||
? 'bg-red-900 hover:bg-red-800'
|
||||
: 'bg-neutral-700 hover:bg-neutral-800'
|
||||
}`}
|
||||
>
|
||||
{confirmLabel}
|
||||
|
||||
@@ -98,15 +98,3 @@ export function clearServerTokens(cookies: Cookies) {
|
||||
cookies.delete('token', { path: '/' });
|
||||
cookies.delete('refreshToken', { path: '/' });
|
||||
}
|
||||
|
||||
export function getUserUUIDFromToken() {
|
||||
if (!browser) return null;
|
||||
const token = getToken();
|
||||
if (!token) return null;
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split('.')[1]));
|
||||
return payload.UUID || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,11 @@
|
||||
let selectedVisibility = data.options?.visibilities?.[0]?.value;
|
||||
let password = '';
|
||||
|
||||
const syntaxOptions = data.options?.syntaxes?.map((lang: string) => ({
|
||||
value: lang,
|
||||
label: displayNames[lang] ?? lang.charAt(0).toUpperCase() + lang.slice(1)
|
||||
})) ?? [];
|
||||
const syntaxOptions =
|
||||
data.options?.syntaxes?.map((lang: string) => ({
|
||||
value: lang,
|
||||
label: displayNames[lang] ?? lang.charAt(0).toUpperCase() + lang.slice(1)
|
||||
})) ?? [];
|
||||
|
||||
const expiresOptions = [
|
||||
{ value: 0, label: 'Never Expire' },
|
||||
@@ -38,12 +39,11 @@
|
||||
{ value: 315360000000, label: 'Expire in 10 Years' }
|
||||
];
|
||||
|
||||
const visibilityOptions = data.options?.visibilities?.map(
|
||||
(visibility: { value: number; displayName: string }) => ({
|
||||
const visibilityOptions =
|
||||
data.options?.visibilities?.map((visibility: { value: number; displayName: string }) => ({
|
||||
value: visibility.value,
|
||||
label: visibility.displayName
|
||||
})
|
||||
) ?? [];
|
||||
})) ?? [];
|
||||
|
||||
async function handleSubmit(event: Event) {
|
||||
event.preventDefault();
|
||||
@@ -143,7 +143,7 @@
|
||||
{/if}
|
||||
<button
|
||||
type="submit"
|
||||
class="w-full rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 cursor-pointer"
|
||||
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900"
|
||||
>Create Paste</button
|
||||
>
|
||||
{#if error}
|
||||
|
||||
@@ -60,10 +60,11 @@
|
||||
: false;
|
||||
|
||||
let interval: ReturnType<typeof setInterval> | null = null;
|
||||
const syntaxOptions = data.options?.syntaxes?.map((lang: string) => ({
|
||||
value: lang,
|
||||
label: displayNames[lang] ?? lang.charAt(0).toUpperCase() + lang.slice(1)
|
||||
})) ?? [];
|
||||
const syntaxOptions =
|
||||
data.options?.syntaxes?.map((lang: string) => ({
|
||||
value: lang,
|
||||
label: displayNames[lang] ?? lang.charAt(0).toUpperCase() + lang.slice(1)
|
||||
})) ?? [];
|
||||
const expiresOptions = [
|
||||
{ value: 0, label: 'Never Expire' },
|
||||
{ value: 600000, label: 'Expire in 10 Minutes' },
|
||||
@@ -79,12 +80,11 @@
|
||||
{ value: 157680000000, label: 'Expire in 5 Years' },
|
||||
{ value: 315360000000, label: 'Expire in 10 Years' }
|
||||
];
|
||||
const visibilityOptions = data.options?.visibilities?.map(
|
||||
(visibility: { value: number; displayName: string }) => ({
|
||||
const visibilityOptions =
|
||||
data.options?.visibilities?.map((visibility: { value: number; displayName: string }) => ({
|
||||
value: visibility.value,
|
||||
label: visibility.displayName
|
||||
})
|
||||
) ?? [];
|
||||
})) ?? [];
|
||||
|
||||
onMount(() => {
|
||||
interval = setInterval(() => {
|
||||
@@ -559,7 +559,7 @@
|
||||
alert('Failed to delete paste.');
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
}}
|
||||
>
|
||||
<Trash2 class="mr-1 h-5 w-5 text-red-400" />
|
||||
@@ -647,13 +647,11 @@
|
||||
modalResolve = null;
|
||||
}
|
||||
}}
|
||||
title={
|
||||
modalMode === 'decrypt'
|
||||
? 'Decrypt Paste'
|
||||
: modalMode === 'encrypt'
|
||||
title={modalMode === 'decrypt'
|
||||
? 'Decrypt Paste'
|
||||
: modalMode === 'encrypt'
|
||||
? 'Encrypt Paste'
|
||||
: 'Are you sure you want to delete this paste?'
|
||||
}
|
||||
: 'Are you sure you want to delete this paste?'}
|
||||
/>
|
||||
|
||||
<style>
|
||||
|
||||
@@ -1,45 +1,59 @@
|
||||
<script lang="ts">
|
||||
import { resolve } from '$app/paths';
|
||||
import type { PageData } from './$types';
|
||||
import type { Pagination } from '$lib/types/pagination';
|
||||
import { User, CalendarDays, ShieldUser, Ban, File, AtSign, Pencil, Trash2, Clock, Hash } from '@lucide/svelte';
|
||||
import type { Pagination } from '$lib/types/pagination';
|
||||
import {
|
||||
User,
|
||||
CalendarDays,
|
||||
ShieldUser,
|
||||
Ban,
|
||||
File,
|
||||
AtSign,
|
||||
Pencil,
|
||||
Trash2,
|
||||
Clock,
|
||||
Hash
|
||||
} from '@lucide/svelte';
|
||||
import { extractDateFromUUIDv7, tooltip, dateToRelativeString } from '$lib/utils/misc';
|
||||
import { roles } from '$lib/consts';
|
||||
import { getToken, getUserUUIDFromToken } from '$lib/utils/auth';
|
||||
import { getToken } from '$lib/utils/auth';
|
||||
import Modal from '$lib/components/Modal.svelte';
|
||||
import { user } from '$lib/stores/user';
|
||||
|
||||
export let data: PageData;
|
||||
|
||||
let currentPage = data.user.pagination?.page ?? 1;
|
||||
let pagination: Pagination = data.user.pagination ?? {
|
||||
page: 1,
|
||||
pageSize: 50,
|
||||
totalCount: 0,
|
||||
totalPages: 1
|
||||
};
|
||||
page: 1,
|
||||
pageSize: 50,
|
||||
totalCount: 0,
|
||||
totalPages: 1
|
||||
};
|
||||
let pastes = data.user.pastes ?? [];
|
||||
let loading = false;
|
||||
let isOwner = false;
|
||||
let showEditModal = false;
|
||||
let showDeleteModal = false;
|
||||
let showRoleModal = false;
|
||||
let modalError = '';
|
||||
let newDisplayName = '';
|
||||
|
||||
$: {
|
||||
const userUUID = getUserUUIDFromToken();
|
||||
isOwner = !!userUUID && userUUID === data.user.uuid;
|
||||
}
|
||||
$: isOwner = data.user && $user ? data.user.uuid === $user.uuid : false;
|
||||
|
||||
const roleOptions = [
|
||||
{ label: 'Member', value: 0 },
|
||||
{ label: 'Moderator', value: 1 },
|
||||
{ label: 'Administrator', value: 255 },
|
||||
{ label: 'Banned', value: 403 }
|
||||
];
|
||||
|
||||
async function fetchPage(pageNum: number) {
|
||||
if (pageNum < 1 || pageNum > (pagination.totalPages || 1) || loading) return;
|
||||
loading = true;
|
||||
const token = getToken();
|
||||
const res = await fetch(
|
||||
`/api/user/${data.user.username}?page=${pageNum}`,
|
||||
{
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : {}
|
||||
}
|
||||
);
|
||||
const token = getToken();
|
||||
const res = await fetch(`/api/user/${data.user.username}?page=${pageNum}`, {
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : {}
|
||||
});
|
||||
if (!res.ok) {
|
||||
loading = false;
|
||||
return;
|
||||
@@ -51,11 +65,7 @@
|
||||
loading = false;
|
||||
}
|
||||
|
||||
async function handleEditDisplayName(displayName: string) {
|
||||
if (!displayName.trim()) {
|
||||
modalError = 'Display name cannot be empty';
|
||||
return;
|
||||
}
|
||||
async function handleUserUpdate(displayName?: string, selectedRoles?: number[]) {
|
||||
loading = true;
|
||||
const token = getToken();
|
||||
const res = await fetch(`/api/user/uuid/${data.user.uuid}`, {
|
||||
@@ -64,7 +74,10 @@
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({ displayName: displayName.trim() })
|
||||
body: JSON.stringify({
|
||||
...(displayName !== undefined ? { displayName: displayName.trim() } : {}),
|
||||
...(selectedRoles !== undefined ? { roles: selectedRoles } : {})
|
||||
})
|
||||
});
|
||||
loading = false;
|
||||
if (!res.ok) {
|
||||
@@ -73,7 +86,11 @@
|
||||
return;
|
||||
}
|
||||
showEditModal = false;
|
||||
data.user.displayName = displayName;
|
||||
showRoleModal = false;
|
||||
data.user.displayName = displayName || data.user.displayName;
|
||||
if (selectedRoles !== undefined) {
|
||||
data.user.roles = selectedRoles;
|
||||
}
|
||||
modalError = '';
|
||||
}
|
||||
|
||||
@@ -102,25 +119,18 @@
|
||||
>
|
||||
<div class="w-[95%] max-w-5xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
|
||||
<h1 class="flex items-center justify-center gap-4 text-center text-4xl font-bold">
|
||||
{#if data.user.roles.includes(1) || data.user.roles.includes(255)}
|
||||
{#if Math.max(...data.user.roles.filter((r) => r !== 403)) !== -Infinity}
|
||||
<span
|
||||
use:tooltip={roles[
|
||||
Math.max(...data.user.roles.filter((r) => r !== 403)) as keyof typeof roles
|
||||
]}
|
||||
>
|
||||
<ShieldUser class="h-8 w-8 text-neutral-400" />
|
||||
</span>
|
||||
{:else}
|
||||
<span use:tooltip={'User'}>
|
||||
<User class="h-8 w-8 text-neutral-400" />
|
||||
</span>
|
||||
{/if}
|
||||
{:else if data.user.roles.includes(403)}
|
||||
<span class="relative flex items-center justify-center" use:tooltip={roles[403]}>
|
||||
<User class="h-8 w-8 text-neutral-500 opacity-90" />
|
||||
{#if data.user.roles.includes(403)}
|
||||
<span
|
||||
class="relative flex h-7 w-7 items-center justify-center overflow-hidden rounded-full"
|
||||
use:tooltip={roles[403]}
|
||||
>
|
||||
<User class="h-8 w-8 text-neutral-500" />
|
||||
<Ban class="absolute h-7 w-7 text-red-500" />
|
||||
</span>
|
||||
{:else if data.user.roles.includes(1) || data.user.roles.includes(255)}
|
||||
<span use:tooltip={roles[Math.max(...data.user.roles) as keyof typeof roles]}>
|
||||
<ShieldUser class="h-8 w-8 text-neutral-400" />
|
||||
</span>
|
||||
{:else}
|
||||
<span use:tooltip={'User'}>
|
||||
<User class="h-8 w-8 text-neutral-400" />
|
||||
@@ -168,29 +178,43 @@
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{#if isOwner}
|
||||
<div class="mt-4 flex gap-2 justify-center flex-wrap">
|
||||
{#if isOwner || $user?.roles.some((r) => r === 255 || r === 1)}
|
||||
<div class="mt-4 flex flex-wrap justify-center gap-2">
|
||||
<button
|
||||
on:click={() => {
|
||||
showEditModal = true;
|
||||
newDisplayName = data.user.displayName || data.user.username;
|
||||
modalError = '';
|
||||
}}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 hover:bg-neutral-600 px-4 py-2 text-sm font-medium transition-colors"
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600"
|
||||
>
|
||||
<Pencil class="h-4 w-4" />
|
||||
Edit Display Name
|
||||
</button>
|
||||
<button
|
||||
on:click={() => {
|
||||
showDeleteModal = true;
|
||||
modalError = '';
|
||||
}}
|
||||
class="flex items-center gap-2 rounded bg-red-900 hover:bg-red-800 px-4 py-2 text-sm font-medium transition-colors"
|
||||
>
|
||||
<Trash2 class="h-4 w-4" />
|
||||
Delete Account
|
||||
</button>
|
||||
{#if $user?.roles.some((r) => r === 255)}
|
||||
<button
|
||||
on:click={() => {
|
||||
showRoleModal = true;
|
||||
modalError = '';
|
||||
}}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600"
|
||||
>
|
||||
<ShieldUser class="h-4 w-4" />
|
||||
Edit Roles
|
||||
</button>
|
||||
{/if}
|
||||
{#if isOwner || $user?.roles.some((r) => r === 255)}
|
||||
<button
|
||||
on:click={() => {
|
||||
showDeleteModal = true;
|
||||
modalError = '';
|
||||
}}
|
||||
class="flex items-center gap-2 rounded bg-red-900 px-4 py-2 text-sm font-medium transition-colors hover:bg-red-800"
|
||||
>
|
||||
<Trash2 class="h-4 w-4" />
|
||||
Delete Account
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
<div class="mt-6 max-h-[60vh] space-y-4 overflow-y-auto">
|
||||
@@ -209,7 +233,9 @@
|
||||
</a>
|
||||
{/each}
|
||||
{:else}
|
||||
<p class="text-center text-neutral-400">{isOwner ? 'You have' : 'This user has'} not created any pastes yet.</p>
|
||||
<p class="text-center text-neutral-400">
|
||||
{isOwner ? 'You have' : 'This user has'} not created any pastes yet.
|
||||
</p>
|
||||
{/if}
|
||||
</div>
|
||||
{#if pagination.totalPages && pagination.totalPages > 1}
|
||||
@@ -242,7 +268,7 @@
|
||||
title="Edit Display Name "
|
||||
placeholder="New Display Name"
|
||||
bind:error={modalError}
|
||||
onConfirm={(value) => handleEditDisplayName(value as string)}
|
||||
onConfirm={(value) => handleUserUpdate(value as string)}
|
||||
onCancel={() => {
|
||||
showEditModal = false;
|
||||
modalError = '';
|
||||
@@ -253,7 +279,7 @@
|
||||
show={showDeleteModal}
|
||||
mode="confirm"
|
||||
title="Delete Account"
|
||||
message="This action cannot be undone. All your pastes and account data will be permanently deleted."
|
||||
message={`This action cannot be undone. ${isOwner ? 'All your' : 'This user\'s'} pastes and account data will be permanently deleted.`}
|
||||
confirmButtonText="Delete Account"
|
||||
error={modalError}
|
||||
onConfirm={() => handleDeleteAccount()}
|
||||
@@ -263,9 +289,22 @@
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
show={showRoleModal}
|
||||
mode="multiselect"
|
||||
title="Edit User Roles"
|
||||
items={roleOptions}
|
||||
initialValue={data.user.roles}
|
||||
error={modalError}
|
||||
onConfirm={(value) => handleUserUpdate(undefined, value as number[])}
|
||||
onCancel={() => {
|
||||
showRoleModal = false;
|
||||
modalError = '';
|
||||
}}
|
||||
/>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
button {
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -67,6 +67,10 @@ namespace SharpbinV3.Server.Controllers
|
||||
user.Email = updatedUser.Email ?? user.Email;
|
||||
user.Visibility = updatedUser.Visibility ?? user.Visibility;
|
||||
}
|
||||
if (userRoles.Any(r => r == 255))
|
||||
{
|
||||
user.Roles = updatedUser.Roles ?? user.Roles;
|
||||
}
|
||||
await _db.SaveChangesAsync();
|
||||
return Ok(new { message = "User updated successfully." });
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace SharpbinV3.Server.DTOs
|
||||
public string? DisplayName { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public int? Visibility { get; set; } = 0;
|
||||
public int[]? Roles { get; set; }
|
||||
|
||||
public IEnumerable<ValidationResult> Validate()
|
||||
{
|
||||
@@ -30,6 +31,18 @@ namespace SharpbinV3.Server.DTOs
|
||||
if (Visibility < 0 || Visibility > 2)
|
||||
results.Add(new ValidationResult("Visibility must be between 0 and 2.", [nameof(Visibility)]));
|
||||
}
|
||||
if (Roles != null)
|
||||
{
|
||||
if (Roles.Length == 0)
|
||||
results.Add(new ValidationResult("Roles cannot be an empty array.", [nameof(Roles)]));
|
||||
int[] validRoles = [0, 1, 255, 403];
|
||||
foreach (var role in Roles)
|
||||
{
|
||||
if (!validRoles.Contains(role))
|
||||
results.Add(new ValidationResult($"Invalid role: {role}.", [nameof(Roles)]));
|
||||
}
|
||||
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user