new (basic.gbapp): ALLOW ROLE keyword.

This commit is contained in:
Rodrigo Rodriguez 2023-08-23 11:24:48 -03:00
parent fb94d6cf8a
commit 3a8be1b536
3 changed files with 54 additions and 16 deletions

View file

@ -32,7 +32,7 @@
'use strict'; 'use strict';
import { GBLog, GBMinInstance } from 'botlib'; import { GBError, GBLog, GBMinInstance } from 'botlib';
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js'; import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
import { ChartServices } from './ChartServices.js'; import { ChartServices } from './ChartServices.js';
import urlJoin from 'url-join'; import urlJoin from 'url-join';
@ -60,6 +60,7 @@ import pkg from 'whatsapp-web.js';
import { ActivityTypes } from 'botbuilder'; import { ActivityTypes } from 'botbuilder';
const { List, Buttons } = pkg; const { List, Buttons } = pkg;
import mime from 'mime-types'; import mime from 'mime-types';
import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
/** /**
* Default check interval for user replay * Default check interval for user replay
@ -525,6 +526,33 @@ export class DialogKeywords {
await sec.updateUserLocale(user.userId, language); await sec.updateUserLocale(user.userId, language);
} }
/**
* Defines the current security context for dialogs based on roles.
*
* @example ALLOW ROLE "DevOps"
*
*/
public async allowRole({ pid, value }) {
const { min, user, proc } = await DialogKeywords.getProcessInfo(pid);
const sys = new SystemKeywords();
// Updates current roles allowed from now on this dialog/process.
proc.roles = value;
// Checks access.
const filters = [`${value}=x`, `id=${user.userSystemId}`];
const people = sys.find({pid, handle:"People.xlsx", args:[filters]});
if (!people){
throw new Error(`Invalid access. Check if People sheet has the role ${value} checked.`);
}
GBLogEx.info(min, `Allowed access for ${user.userSystemId} on ${value}`);
}
/** /**
* Defines the id generation policy. * Defines the id generation policy.
* *

View file

@ -209,19 +209,6 @@ export class GBVMService extends GBService {
const createRpcClient = require("@push-rpc/core").createRpcClient; const createRpcClient = require("@push-rpc/core").createRpcClient;
const createHttpClient = require("@push-rpc/http").createHttpClient; const createHttpClient = require("@push-rpc/http").createHttpClient;
// Setups interprocess communication from .gbdialog run-time to the BotServer API.
const optsRPC = {callTimeout: this.callTimeout};
let url;
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/dk';
const dk = (await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote;
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/sys';
const sys = (await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote;
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/wa';
const wa = (await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote;
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/img';
const img = (await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote;
// Unmarshalls Local variables from server VM. // Unmarshalls Local variables from server VM.
let pid = this.pid; let pid = this.pid;
@ -287,8 +274,24 @@ export class GBVMService extends GBService {
const base64 = (v) => { return (async () => { return await dk.getCoded({v}) })(); }; const base64 = (v) => { return (async () => { return await dk.getCoded({v}) })(); };
const tolist = (v) => { return (async () => { return await dk.getToLst({v}) })(); }; const tolist = (v) => { return (async () => { return await dk.getToLst({v}) })(); };
// Setups interprocess communication from .gbdialog run-time to the BotServer API.
const optsRPC = {callTimeout: this.callTimeout};
let url;
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/dk';
const dk = caseInsensitive ((await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote);
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/sys';
const sys = caseInsensitive ((await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote);
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/wa';
const wa = caseInsensitive ((await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote);
url = 'http://localhost:${GBVMService.API_PORT}/api/v3/${min.botId}/img';
const img = caseInsensitive ((await createRpcClient(0, () => createHttpClient(url), optsRPC)).remote);
${code} ${code}
// Closes handles if any.
await wa.closeHandles({pid: pid}); await wa.closeHandles({pid: pid});
})(); })();
@ -498,7 +501,8 @@ export class GBVMService extends GBService {
pid: pid, pid: pid,
userId: user ? user.userId : 0, userId: user ? user.userId : 0,
instanceId: min.instance.instanceId, instanceId: min.instance.instanceId,
channel: channel channel: channel,
roles: 'everyone'
}; };
return pid; return pid;
} }

View file

@ -343,7 +343,6 @@ export class KeywordsExpressions {
} }
]; ];
keywords[i++] = [ keywords[i++] = [
/^\s*hear (\w+\$*) as (\w+( \w+)*.xlsx)/gim, /^\s*hear (\w+\$*) as (\w+( \w+)*.xlsx)/gim,
($0, $1, $2) => { ($0, $1, $2) => {
@ -609,6 +608,13 @@ export class KeywordsExpressions {
} }
]; ];
keywords[i++] = [
/^\s*(allow role)(\s*)(.*)/gim,
($0, $1, $2, $3) => {
return `await dk.allowRole ({pid: pid, role: ${$3}})`;
}
];
keywords[i++] = [ keywords[i++] = [
/^\s*(set filter)(\s*)(.*)/gim, /^\s*(set filter)(\s*)(.*)/gim,
($0, $1, $2, $3) => { ($0, $1, $2, $3) => {