mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat(roles, visibility): refactor user roles to use flags and introduce visibility enum
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
import { user } from '$lib/stores/user';
|
||||
import { resolve } from '$app/paths';
|
||||
import { page } from '$app/state';
|
||||
import { clearTokens } from '$lib/utils/auth';
|
||||
import { clearTokens, hasRole } from '$lib/utils/auth';
|
||||
import logo from '$lib/assets/favicon.svg';
|
||||
import type { PageData } from '../../routes/$types';
|
||||
import { slide } from 'svelte/transition';
|
||||
@@ -50,7 +50,7 @@
|
||||
</div>
|
||||
{#if $user}
|
||||
<div class="ml-auto flex items-center space-x-4">
|
||||
{#if $user.roles.includes(1) || $user.roles.includes(255)}
|
||||
{#if hasRole($user.roles, 2) || hasRole($user.roles, 4)}
|
||||
<a
|
||||
href={resolve('/reports')}
|
||||
class="rounded bg-neutral-700 px-3 py-1 text-white hover:bg-neutral-800">Reports</a
|
||||
@@ -116,7 +116,7 @@
|
||||
on:click={() => (menuOpen = false)}>Recent Pastes</a
|
||||
>
|
||||
{#if $user}
|
||||
{#if $user.roles.includes(1) || $user.roles.includes(255)}
|
||||
{#if hasRole($user.roles, 2) || hasRole($user.roles, 4)}
|
||||
<a
|
||||
href={resolve('/reports')}
|
||||
class="mt-1 block w-full rounded bg-neutral-800 px-3 py-2 text-center text-neutral-100 transition-colors hover:bg-neutral-700"
|
||||
|
||||
@@ -211,13 +211,6 @@ export const expiresOptions = [
|
||||
{ value: 315360000000, label: 'Expire in 10 Years' }
|
||||
];
|
||||
|
||||
export const roles = {
|
||||
0: 'User',
|
||||
1: 'Moderator',
|
||||
255: 'Administrator',
|
||||
403: 'Banned'
|
||||
};
|
||||
|
||||
export const trustedDomains = [
|
||||
'img.youtube.com',
|
||||
'i.ytimg.com',
|
||||
@@ -249,4 +242,4 @@ export const trustedDomains = [
|
||||
'cloudflare-ipfs.com',
|
||||
|
||||
'upload.wikimedia.org'
|
||||
];
|
||||
];
|
||||
@@ -8,7 +8,8 @@ export interface User {
|
||||
displayName: string | null;
|
||||
email: string | null;
|
||||
lastLogin: number | null;
|
||||
roles: number[];
|
||||
roles: number; // bitfield; 5 = User | Admin
|
||||
isBanned: boolean;
|
||||
visibility: 0 | 1 | 2;
|
||||
pastes: Paste[] | null;
|
||||
pagination: Pagination | null;
|
||||
|
||||
@@ -2,6 +2,14 @@ import { browser } from '$app/environment';
|
||||
import type { Cookies } from '@sveltejs/kit';
|
||||
import { user } from '$lib/stores/user';
|
||||
|
||||
export const roles = {
|
||||
User: 1,
|
||||
Moderator: 2,
|
||||
Admin: 4
|
||||
} as const;
|
||||
|
||||
export type Role = keyof typeof roles;
|
||||
|
||||
const TOKEN_KEY = 'token';
|
||||
const REFRESH_TOKEN_KEY = 'refreshToken';
|
||||
|
||||
@@ -137,3 +145,8 @@ export function clearServerTokens(cookies: Cookies) {
|
||||
cookies.delete('token', { path: '/' });
|
||||
cookies.delete('refreshToken', { path: '/' });
|
||||
}
|
||||
|
||||
export const hasRole = (userRoles: number, role: number) => (userRoles & role) !== 0;
|
||||
|
||||
export const getUserRoles = (userRoles: number): Role[] =>
|
||||
(Object.keys(roles) as Role[]).filter((role) => hasRole(userRoles, roles[role]));
|
||||
|
||||
@@ -10,9 +10,9 @@ export function parseTotpEnabled(tokenValue: string | null) {
|
||||
}
|
||||
}
|
||||
|
||||
export function needsAdminTotp(totpEnabled: boolean, currentRoles: number[], nextRoles: number[]) {
|
||||
export function needsAdminTotp(totpEnabled: boolean, currentRoles: number, nextRoles: number) {
|
||||
if (!totpEnabled) return false;
|
||||
const nextHasAdmin = nextRoles.includes(255);
|
||||
const alreadyAdmin = currentRoles.includes(255);
|
||||
const nextHasAdmin = (nextRoles & 4) !== 0;
|
||||
const alreadyAdmin = (currentRoles & 4) !== 0;
|
||||
return nextHasAdmin && !alreadyAdmin;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user