From 311749ae811e3dd967b87149828d32e5c543509b Mon Sep 17 00:00:00 2001 From: "Christopher S. Case" Date: Tue, 21 May 2024 13:25:20 -0500 Subject: [PATCH] split roles out from account RA. --- src/server/decoders/role.ts | 22 ------ src/server/managers/account.ts | 6 +- src/server/managers/roles.ts | 10 ++- src/server/models/role.ts | 68 ------------------- src/server/resource-access/role.ts | 15 +++- src/server/resource-access/transforms/role.ts | 35 ++++++++++ 6 files changed, 56 insertions(+), 100 deletions(-) delete mode 100644 src/server/decoders/role.ts delete mode 100644 src/server/models/role.ts create mode 100644 src/server/resource-access/transforms/role.ts diff --git a/src/server/decoders/role.ts b/src/server/decoders/role.ts deleted file mode 100644 index 66167348..00000000 --- a/src/server/decoders/role.ts +++ /dev/null @@ -1,22 +0,0 @@ -// --------------------------------------------------------------------------------------------------------------------- -// Role Decoders -// --------------------------------------------------------------------------------------------------------------------- - -import { array, integer, string, object } from 'decoders'; -import { jsonArrayString, stringWithLength, withDefault } from './utils'; - -// --------------------------------------------------------------------------------------------------------------------- - -export const roleRecDecoder = object({ - id: integer, - name: stringWithLength(3, 255), - permissions: jsonArrayString(string) -}); - -export const roleJsonDecoder = object({ - id: integer, - name: stringWithLength(3, 255), - permissions: withDefault(array(string), []) -}); - -// --------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/managers/account.ts b/src/server/managers/account.ts index 174f9424..cfb94383 100644 --- a/src/server/managers/account.ts +++ b/src/server/managers/account.ts @@ -18,7 +18,7 @@ export async function list(filters : accountRA.AccountFilters) : Promise { - const roles = await roleRA.getRoles(account.id); + const roles = await roleRA.getForAccount(account.id); return { ...account, groups: roles @@ -29,7 +29,7 @@ export async function list(filters : accountRA.AccountFilters) : Promise { const account = await accountRA.get(accountID); - const roles = await roleRA.getRoles(accountID); + const roles = await roleRA.getForAccount(accountID); return { ...account, @@ -40,7 +40,7 @@ export async function get(accountID : string) : Promise export async function getByEmail(email : string) : Promise { const account = await accountRA.getByEmail(email); - const roles = await roleRA.getRoles(account.id); + const roles = await roleRA.getForAccount(account.id); return { ...account, diff --git a/src/server/managers/roles.ts b/src/server/managers/roles.ts index 48fd64f7..fca95ab9 100644 --- a/src/server/managers/roles.ts +++ b/src/server/managers/roles.ts @@ -3,18 +3,16 @@ // --------------------------------------------------------------------------------------------------------------------- // Models -import { Role } from '../models/role'; +import { Role } from '../../common/interfaces/models/role'; -// Utils -import { getDB } from '../utils/database'; +// Resource Access +import * as roleRA from '../resource-access/role'; // --------------------------------------------------------------------------------------------------------------------- export async function list() : Promise { - const db = await getDB(); - return (await db('role as r').select('r.role_id as id', 'r.name', 'r.permissions')) - .map(Role.fromDB); + return roleRA.list(); } // --------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/models/role.ts b/src/server/models/role.ts deleted file mode 100644 index 478f568e..00000000 --- a/src/server/models/role.ts +++ /dev/null @@ -1,68 +0,0 @@ -//---------------------------------------------------------------------------------------------------------------------- -// Role -//---------------------------------------------------------------------------------------------------------------------- - -import * as JsonDecoder from 'decoders'; - -// Decoders -import { roleJsonDecoder, roleRecDecoder } from '../decoders/role'; - -// Models -import { RoleOptions } from '../../common/interfaces/models/role'; - -//---------------------------------------------------------------------------------------------------------------------- - -export class Role -{ - public readonly id : number; - public readonly name : string; - public permissions : string[] = []; - - constructor(options : RoleOptions) - { - this.id = options.id; - this.name = options.name; - this.permissions = options.permissions ?? []; - } - - //------------------------------------------------------------------------------------------------------------------ - // Serialization - //------------------------------------------------------------------------------------------------------------------ - - public toJSON() : Record - { - return { - id: this.id, - name: this.name, - permissions: this.permissions - }; - } - - public toDB() : Record - { - const { id, ...jsonObj } = this.toJSON(); - return { - ...jsonObj, - role_id: id, - permissions: JSON.stringify(this.permissions) - }; - } - - //------------------------------------------------------------------------------------------------------------------ - // Deserialization - //------------------------------------------------------------------------------------------------------------------ - - static fromDB(roleRecord : Record) : Role - { - const decoder = JsonDecoder.guard(roleRecDecoder); - return new Role(decoder(roleRecord)); - } - - static fromJSON(jsonObj : Record) : Role - { - const decoder = JsonDecoder.guard(roleJsonDecoder); - return new Role(decoder(jsonObj)); - } -} - -//---------------------------------------------------------------------------------------------------------------------- diff --git a/src/server/resource-access/role.ts b/src/server/resource-access/role.ts index a8cb48af..aed495d2 100644 --- a/src/server/resource-access/role.ts +++ b/src/server/resource-access/role.ts @@ -2,11 +2,24 @@ // Role Resource Access Layer // --------------------------------------------------------------------------------------------------------------------- +import { Role } from '../../common/interfaces/models/role'; + +// Transforms +import * as RoleTransforms from './transforms/role'; + +// Utils import { getDB } from '../utils/database'; // --------------------------------------------------------------------------------------------------------------------- -export async function getRoles(accountID : string) : Promise +export async function list() : Promise +{ + const db = await getDB(); + return (await db('role as r').select('r.role_id as id', 'r.name', 'r.permissions')) + .map(RoleTransforms.fromDB); +} + +export async function getForAccount(accountID : string) : Promise { const db = await getDB(); const roles = await db('account as ac') diff --git a/src/server/resource-access/transforms/role.ts b/src/server/resource-access/transforms/role.ts new file mode 100644 index 00000000..32b16bb4 --- /dev/null +++ b/src/server/resource-access/transforms/role.ts @@ -0,0 +1,35 @@ +// --------------------------------------------------------------------------------------------------------------------- +// Role Database Transform +// --------------------------------------------------------------------------------------------------------------------- + +import { Role } from '../../../common/interfaces/models/role'; + +// --------------------------------------------------------------------------------------------------------------------- + +export interface RoleDBSchema { + role_id : number; + name : string; + permissions : string; +} + +// --------------------------------------------------------------------------------------------------------------------- + +export function toDB(role : Role) : RoleDBSchema +{ + return { + role_id: role.id, + name: role.name, + permissions: JSON.stringify(role.permissions ?? []) + }; +} + +export function fromDB(role : RoleDBSchema) : Role +{ + return { + id: role.role_id, + name: role.name, + permissions: JSON.parse(role.permissions) ?? [] + }; +} + +// ---------------------------------------------------------------------------------------------------------------------