new(security.gblib): Params support in users.

This commit is contained in:
rodrigorodriguez 2023-01-31 10:08:48 -03:00
parent 99e7619494
commit 20911af3d7
4 changed files with 73 additions and 11 deletions

View file

@ -137,7 +137,12 @@ CREATE TABLE [dbo].[GuaribasSchedule]
GO
# 3.0.0
ALTER TABLE dbo.GuaribasInstance ADD botKey nvarchar(64) NULL;
# 2.3.9
GO
ALTER TABLE dbo.GuaribasUser ADD
params nvarchar(4000) NULL
GO

View file

@ -371,6 +371,8 @@ export class GBVMService extends GBService {
}
];
// Based on https://github.com/uweg/vbscript-to-typescript.
keywords[i++] = [/^\s*else(?!{)/gim, '}\nelse {'];
keywords[i++] = [/^\s*select case +(.*)/gim, 'switch ($1) {'];
@ -378,6 +380,7 @@ export class GBVMService extends GBService {
keywords[i++] = [/^\s*end select/gim, '}'];
keywords[i++] = [/^\s*end function/gim, '}'];
keywords[i++] = [/^\s*function +(.*)\((.*)\)/gim, '$1 = ($2) => {\n'];
keywords[i++] = [/^\s*for +(.*to.*)/gim, 'for ($1) {'];
@ -780,21 +783,21 @@ export class GBVMService extends GBService {
keywords[i++] = [
/^\s*(\btransfer\b)(?=(?:[^"]|"[^"]*")*$)/gim,
() => {
return `await dk.transferTo ({pid: pid, })`;
return `await dk.transferTo ({pid: pid})`;
}
];
keywords[i++] = [
/^\s*(exit)/gim,
() => {
return ``;
return `return;`;
}
];
keywords[i++] = [
/^\s*(END)/gim,
() => {
return ``;
return `return;`;
}
];

View file

@ -99,6 +99,9 @@ export class GuaribasUser extends Model<GuaribasUser> {
@Column(DataType.STRING(64))
declare hearOnDialog: string;
@Column(DataType.STRING(4000))
declare params: string;
}
/**

View file

@ -1,10 +1,8 @@
import Fs from 'fs';
import urlJoin from 'url-join';
import { GBServer } from '../../../src/app.js';
import { ConversationReference } from 'botbuilder';
import { GBLog, GBMinInstance, GBService, IGBInstance } from 'botlib';
import { CollectionUtil } from 'pragmatismo-io-framework';
import { GuaribasGroup, GuaribasUser, GuaribasUserGroup } from '../models/index.js';
import { GuaribasUser } from '../models/index.js';
import { FindOptions } from 'sequelize';
/**
@ -228,4 +226,57 @@ export class SecService extends GBService {
}
});
}
}
/**
* Get a dynamic param from user. Dynamic params are defined in .gbdialog SET
* variables and other semantics during conversation.
*
* @param name Name of param to get from instance.
* @param defaultValue Value returned when no param is defined.
*/
public getParam<T> (user: IGBInstance, name: string, defaultValue?: T): any {
let value = null;
if (user.params) {
const params = JSON.parse(user.params);
value = params ? params[name] : defaultValue;
}
if (typeof defaultValue === 'boolean') {
return new Boolean(value ? value.toString().toLowerCase() === 'true' : defaultValue);
}
if (typeof defaultValue === 'string') {
return value ? value : defaultValue;
}
if (typeof defaultValue === 'number') {
return new Number(value ? value : defaultValue ? defaultValue : 0);
}
if (user['dataValues'] && !value) {
value = user['dataValues'][name];
if (value === null) {
switch(name)
{
case 'language':
value = 'en';
break;
}
}
}
return value;
}
/**
* Saves user instance object to the storage handling
* multi-column JSON based store 'params' field.
*/
public async setParam (userId: number, name: string, value:any) {
const options = { where: {} };
options.where = { botId: userId };
let user = await GuaribasUser.findOne(options);
// tslint:disable-next-line:prefer-object-spread
let obj = JSON.parse(user.params);
obj['name'] = value;
user.params = JSON.stringify(obj);
return await user.save();
}
}