Add createRole method to userServiceAPI

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/3/head
trivernis 4 years ago
parent 96916b0fd1
commit fcbd65d53b
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -9,3 +9,5 @@ export type GetRolesResponse = [number, string, string][]
// Permissions map where each roleId maps to an array of permissions // Permissions map where each roleId maps to an array of permissions
export type GetRolesPermissionsResponse = {[id: string]: [number, string, string][]} export type GetRolesPermissionsResponse = {[id: string]: [number, string, string][]}
export type CreateRoleResponse = [number, string, string]

@ -3,7 +3,13 @@ import { Socket } from 'net'
import { PromiseSocket } from 'promise-socket' import { PromiseSocket } from 'promise-socket'
import { RPCMessage } from './message' import { RPCMessage } from './message'
import { Method } from './method' import { Method } from './method'
import { GetInfoResponse, GetRolesPermissionsResponse, GetRolesResponse, ValidateTokenResponse } from './responses' import {
CreateRoleResponse,
GetInfoResponse,
GetRolesPermissionsResponse,
GetRolesResponse,
ValidateTokenResponse
} from './responses'
import { Permission, requiredPermissions } from './permission' import { Permission, requiredPermissions } from './permission'
/** /**
@ -40,6 +46,22 @@ export class UserServerAPI extends DataSource {
await this.send<any>(new RPCMessage(Method.CreatePermissions, { permissions: requiredPermissions })) await this.send<any>(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<CreateRoleResponse> {
const response = await this.send<CreateRoleResponse>(new RPCMessage<any>(Method.CreateRole, {
name,
description,
permissions: permissionIDs
}))
return response.data
}
/** /**
* validates user token * validates user token
*/ */
@ -89,6 +111,10 @@ export class UserServerAPI extends DataSource {
return promiseSocket return promiseSocket
} }
/**
* Sends a message and reads and parses the response of it
* @param message
*/
async send<T> (message: RPCMessage<any>): Promise<RPCMessage<T>> { async send<T> (message: RPCMessage<any>): Promise<RPCMessage<T>> {
const socket = await this.getSocket() const socket = await this.getSocket()
await socket.writeAll(message.toBuffer()) await socket.writeAll(message.toBuffer())

@ -9,6 +9,12 @@ import express from 'express'
require('dotenv').config() 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) { async function authenticate (req: any, res: any, next: any) {
if (process.env.NODE_ENV === 'develop') { if (process.env.NODE_ENV === 'develop') {
next() next()

Loading…
Cancel
Save