From a9f943b7e48e1bb34006fd26c60e9f0e3d0db18c Mon Sep 17 00:00:00 2001 From: TheClashFruit Date: Sat, 7 Sep 2024 16:38:00 +0200 Subject: [PATCH] fix: auth util fixes --- .eslintrc.json | 3 ++- utils/auth_util.ts | 4 ++-- utils/permissions.ts | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index c4c1806..21819be 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,7 +3,8 @@ "rules": { "indent": [ "error", - 2 + 2, + { "SwitchCase": 1 } ], "quotes": [ "error", diff --git a/utils/auth_util.ts b/utils/auth_util.ts index 071f677..e2f442a 100644 --- a/utils/auth_util.ts +++ b/utils/auth_util.ts @@ -1,5 +1,5 @@ import Database from '@/lib/Database'; -import { hasPermission, Permission } from './permissions'; +import { getPermission, hasPermission, Permission } from './permissions'; import { User } from '@/interfaces'; import type { NextApiRequest } from 'next'; @@ -27,7 +27,7 @@ export async function isUserAdmin(sid?: string): Promise { return { user, - hasPermission: hasPermission(user.permissions, Permission.Admin) + hasPermission: hasPermission(getPermission(user.permissions), Permission.Admin) }; } diff --git a/utils/permissions.ts b/utils/permissions.ts index 07aa9b3..bf192a7 100644 --- a/utils/permissions.ts +++ b/utils/permissions.ts @@ -10,6 +10,29 @@ export enum PermissionNamed { ServerPlayer = 'server_player' } +export function getPermission(permissions: PermissionNamed[]): Permission { + let result = 0; + + for (const permission of permissions) { + switch (permission) { + case PermissionNamed.SuperAdmin: + result |= Permission.SuperAdmin; + + break; + case PermissionNamed.Admin: + result |= Permission.Admin; + + break; + case PermissionNamed.ServerPlayer: + result |= Permission.ServerPlayer; + + break; + } + } + + return result; +} + export function hasPermission(permissions: number, permission: Permission): boolean { return (permissions & permission) === permission; }