From fa00c64b2c7e48d77bda895c21874ea43211e7ae Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Tue, 9 Jul 2024 02:57:21 +1000 Subject: [PATCH] feat(server/player): add hasAccountPermission method --- server/accounts/db.ts | 25 ++----------------------- server/accounts/roles.ts | 23 +++++++++++++++++++++++ server/player/class.ts | 19 +++++++++++++++++-- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/server/accounts/db.ts b/server/accounts/db.ts index ff5ebf31..98345bbb 100644 --- a/server/accounts/db.ts +++ b/server/accounts/db.ts @@ -1,10 +1,9 @@ import { Connection, GetConnection, db } from 'db'; import { OxPlayer } from 'player/class'; -import type { OxAccount, OxAccountPermissions, OxAccountRoles } from 'types'; +import type { OxAccount, OxAccountRoles } from 'types'; import locales from '../../common/locales'; import { getRandomInt } from '@overextended/ox_lib'; -import { CheckRolePermission } from './roles'; -import { GetGroup } from 'groups'; +import { CanPerformAction } from './roles'; const addBalance = `UPDATE accounts SET balance = balance + ? WHERE id = ?`; const removeBalance = `UPDATE accounts SET balance = balance - ? WHERE id = ?`; @@ -160,26 +159,6 @@ export function SelectAccountRole(accountId: number, charId: number) { return db.column(selectAccountRole, [accountId, charId]); } -async function CanPerformAction( - player: OxPlayer, - accountId: number, - role: OxAccountRoles | null, - action: keyof OxAccountPermissions -) { - if (CheckRolePermission(role, action)) return true; - - const groupName = (await SelectAccount(accountId))?.group; - - if (groupName) { - const group = GetGroup(groupName); - const groupRole = group.accountRoles[player.getGroup(groupName)]; - - if (CheckRolePermission(groupRole, action)) return true; - } - - return false; -} - export async function DepositMoney( playerId: number, accountId: number, diff --git a/server/accounts/roles.ts b/server/accounts/roles.ts index 681e4ed7..f8f2cf2b 100644 --- a/server/accounts/roles.ts +++ b/server/accounts/roles.ts @@ -1,5 +1,8 @@ import { db } from 'db'; import type { OxAccountPermissions, OxAccountRoles } from 'types'; +import { SelectAccount } from './db'; +import { GetGroup } from 'groups'; +import { OxPlayer } from 'player/class'; type DbAccountRow = OxAccountPermissions & { id?: number; name?: OxAccountRoles }; @@ -11,6 +14,26 @@ export function CheckRolePermission(roleName: OxAccountRoles | null, permission: return accountRoles?.[roleName]?.[permission]; } +export async function CanPerformAction( + player: OxPlayer, + accountId: number, + role: OxAccountRoles | null, + action: keyof OxAccountPermissions +) { + if (CheckRolePermission(role, action)) return true; + + const groupName = (await SelectAccount(accountId))?.group; + + if (groupName) { + const group = GetGroup(groupName); + const groupRole = group.accountRoles[player.getGroup(groupName)]; + + if (CheckRolePermission(groupRole, action)) return true; + } + + return false; +} + async function LoadRoles() { const roles = await db.execute(`SELECT * FROM account_roles`); diff --git a/server/player/class.ts b/server/player/class.ts index 8d4cbd8d..cfb949ed 100644 --- a/server/player/class.ts +++ b/server/player/class.ts @@ -23,9 +23,18 @@ import { UpdateCharacterGroup, SetActiveGroup, } from 'groups/db'; -import { GetCharacterAccount, GetCharacterAccounts } from 'accounts'; -import type { Character, Dict, NewCharacter, PlayerMetadata, OxGroup, CharacterLicense } from 'types'; +import { GetAccountRole, GetCharacterAccount, GetCharacterAccounts } from 'accounts'; +import type { + Character, + Dict, + NewCharacter, + PlayerMetadata, + OxGroup, + CharacterLicense, + OxAccountPermissions, +} from 'types'; import { GetGroupPermissions } from '../../common'; +import { CanPerformAction } from 'accounts/roles'; export type PlayerInstance = InstanceType; @@ -186,6 +195,12 @@ export class OxPlayer extends ClassInterface { return GetCharacterAccounts(this.charId, getShared); } + async hasAccountPermission(accountId: number, action: keyof OxAccountPermissions) { + return ( + this.charId && CanPerformAction(this, accountId, (await GetAccountRole(accountId, this.charId)) || null, action) + ); + } + setActiveGroup(groupName?: string, temp?: boolean) { if (!this.charId || (groupName && !(groupName in this.#groups))) return false;