Skip to content

Commit

Permalink
feat(server/player): add hasAccountPermission method
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jul 8, 2024
1 parent 42daab4 commit fa00c64
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
25 changes: 2 additions & 23 deletions server/accounts/db.ts
Original file line number Diff line number Diff line change
@@ -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 = ?`;
Expand Down Expand Up @@ -160,26 +159,6 @@ export function SelectAccountRole(accountId: number, charId: number) {
return db.column<OxAccount['role']>(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,
Expand Down
23 changes: 23 additions & 0 deletions server/accounts/roles.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand All @@ -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<DbAccountRow>(`SELECT * FROM account_roles`);

Expand Down
19 changes: 17 additions & 2 deletions server/player/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof OxPlayer>;

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit fa00c64

Please sign in to comment.