diff --git a/src/datasources/userserver/responses.ts b/src/datasources/userserver/responses.ts index c1bbd21..9da667c 100644 --- a/src/datasources/userserver/responses.ts +++ b/src/datasources/userserver/responses.ts @@ -9,3 +9,5 @@ export type GetRolesResponse = [number, string, string][] // Permissions map where each roleId maps to an array of permissions export type GetRolesPermissionsResponse = {[id: string]: [number, string, string][]} + +export type CreateRoleResponse = [number, string, string] diff --git a/src/datasources/userserver/userserviceAPI.ts b/src/datasources/userserver/userserviceAPI.ts index 0dcc978..6c16ae4 100644 --- a/src/datasources/userserver/userserviceAPI.ts +++ b/src/datasources/userserver/userserviceAPI.ts @@ -3,7 +3,13 @@ import { Socket } from 'net' import { PromiseSocket } from 'promise-socket' import { RPCMessage } from './message' import { Method } from './method' -import { GetInfoResponse, GetRolesPermissionsResponse, GetRolesResponse, ValidateTokenResponse } from './responses' +import { + CreateRoleResponse, + GetInfoResponse, + GetRolesPermissionsResponse, + GetRolesResponse, + ValidateTokenResponse +} from './responses' import { Permission, requiredPermissions } from './permission' /** @@ -40,6 +46,22 @@ export class UserServerAPI extends DataSource { await this.send(new RPCMessage(Method.CreatePermissions, { permissions: requiredPermissions })) } + /** + * Creates a new role with the given parameters + * @param name - the name of the role + * @param description - a description of the role + * @param permissionIDs - an array of IDs the role is created with + */ + async createRole (name: string, description: string, permissionIDs: number[]): Promise { + const response = await this.send(new RPCMessage(Method.CreateRole, { + name, + description, + permissions: permissionIDs + })) + + return response.data + } + /** * validates user token */ @@ -89,6 +111,10 @@ export class UserServerAPI extends DataSource { return promiseSocket } + /** + * Sends a message and reads and parses the response of it + * @param message + */ async send (message: RPCMessage): Promise> { const socket = await this.getSocket() await socket.writeAll(message.toBuffer()) diff --git a/src/index.ts b/src/index.ts index 61a51b2..5ffdc5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,12 @@ import express from 'express' require('dotenv').config() +/** + * Function that is called to authenticate a user by using the user rpc server + * @param req + * @param res + * @param next + */ async function authenticate (req: any, res: any, next: any) { if (process.env.NODE_ENV === 'develop') { next()