Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Almond
//
// Copyright 2016 Giovanni Campagna
//
// See COPYING for details
"use strict";
const Q = require('q');
const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;
const SEMPRESyntax = ThingTalk.SEMPRESyntax;
const Describe = ThingTalk.Describe;
const ValueCategory = require('./semantic').ValueCategory;
const Dialog = require('./dialog');
const ContactSearchDialog = require('./contact_search_dialog');
module.exports = class SetupDialog extends Dialog {
constructor() {
super();
this.person = null;
this.principal = null;
this.program = null;
this.reconstructed = null;
this.contactSearch = null;
}
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Almond
//
// Copyright 2016-2018 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna
//
// See COPYING for details
"use strict";
const { Intent } = require('./intent');
const ThingTalk = require('thingtalk');
const Describe = ThingTalk.Describe;
const fakeGettext = {
dgettext(domain, sentence) {
return sentence;
},
dngettext(domain, sentence, plural, num) {
if (num === 1)
return sentence;
else
return plural;
}
};
function makeContext() {
return {
runProgram(program, uniqueId) {
console.log('Adding program ' + uniqueId + ' to engine ' + this._engineId);
logfile.write(`${Date.now()}\trun-program\t${this._engineId} ${uniqueId}\n`);
let name = ThingTalk.Describe.getProgramName(this._gettext, program);
let description = ThingTalk.Describe.describeProgram(this._gettext, program);
let appMeta = {};
let code = ThingTalk.Ast.prettyprint(program);
return this._engine.apps.loadOneApp(code, appMeta, uniqueId, undefined,
name, description, true).then((app) => {
if (!app)
return;
this.emit('program-loaded', app.uniqueId);
});
}
}
module.exports = async function testPermissions(engine) {
const permissions = engine.permissions;
assert.deepStrictEqual(permissions.getAllPermissions(), []);
const prog1 = await parseProgram(engine, `now => @org.thingpedia.builtin.test.eat_data(data='foo');`);
assert.deepStrictEqual(await permissions.checkCanBeAllowed('mock:user1', prog1), false);
assert.deepStrictEqual(await permissions.checkIsAllowed('mock:user1', prog1), null);
const permrule = await parseProgram(engine, `source == "mock:user1"^^tt:contact("User 1") :
now => @org.thingpedia.builtin.test.eat_data, starts_with(data, 'foo');`);
const description = ThingTalk.Describe.describePermissionRule(engine.platform.getCapability('gettext'), permrule);
const permruleId = await permissions.addPermission(permrule, description, {
metadataKey: 'value1'
});
assert.deepStrictEqual(permissions.getAllPermissions(), [{
uniqueId: permruleId,
code: permrule.prettyprint(),
description: `User 1 is allowed to consume any data if the data starts with “foo”`,
metadata: {
$description: `User 1 is allowed to consume any data if the data starts with “foo”`,
metadataKey: 'value1'
}
}]);
assert.deepStrictEqual(await engine.getAllPermissions(), [{
uniqueId: permruleId,
description: `User 1 is allowed to consume any data if the data starts with “foo”`,
async createApp(program, options = {}) {
const uniqueId = options.uniqueId || 'uuid-' + uuid.v4();
const gettext = this._platform.getCapability('gettext');
const describer = new ThingTalk.Describe.Describer(gettext, this._platform.locale, this._platform.timezone);
const name = options.name || ThingTalk.Describe.getProgramName(gettext, program);
delete options.name;
const description = options.description || describer.describeProgram(program);
delete options.description;
console.log('Loading new app: ' + program.prettyprint());
if (program.principal !== null) {
const remote = this._engine.remote;
if (remote === null)
throw new Error(`Remote execution is not available in this Almond`);
const identity = this._engine.messaging.getPrimaryIdentity();
const principal = program.principal;
let ownprograms = program.lowerReturn(this._engine.messaging);
let app = null;
if (ownprograms.length > 0) {
let op = 'is';
if (type.isArray) {
type = type.elem;
op = 'contains';
}
let valueList = Generate.genRandomValue(argname, type);
let placeholder = getPlaceholder(type);
if (valueList.length === 0)
return placeholder || 'something';
if (placeholder && isInput[argname] && isRequired[argname]) {
if (coin(0.1))
return placeholder;
}
let value = uniform(valueList);
let description = ThingTalk.Describe.describeArg(gettext, value);
if (value.isDate) {
value.value.setHours(0);
value.value.setMinutes(0);
value.value.setSeconds(0);
description = value.value.toLocaleDateString();
}
let [sempreType, sempreValue] = valueToSEMPRE(value);
args.push({ name: { id: 'tt:param.' + argname }, operator: op,
type: sempreType, value: sempreValue });
return description;
});
async addPermission(permissionRule, description, extra = {}) {
if (typeof permissionRule === 'string') {
permissionRule = await ThingTalk.Grammar.parseAndTypecheck(permissionRule, this._schemas, true);
description = ThingTalk.Describe.describePermissionRule(this._platform.getCapability('gettext'), permissionRule);
}
let uniqueId = uuid.v4();
let compat_key = getCompatKey(permissionRule);
let code = permissionRule.prettyprint();
this._permissiondb.set(uniqueId, permissionRule);
extra.$description = description;
this._permissionextra.set(uniqueId, extra);
await this._checker.allowed(permissionRule);
await this._db.insertOne(uniqueId, {
code, compat_key,
extra: JSON.stringify(extra)
});
return uniqueId;
}
function computeDescriptions(dlg, stream, query, action) {
return [
stream !== null ? ThingTalk.Describe.describeStream(dlg.manager.gettext, stream) : dlg._("now"),
query !== null ? ThingTalk.Describe.describeTable(dlg.manager.gettext, query) : '',
action !== null ? ThingTalk.Describe.describePrimitive(dlg.manager.gettext, action) : dlg._("notify me")
];
}
function computeDescriptions(dlg, stream, query, action) {
return [
stream !== null ? ThingTalk.Describe.describeStream(dlg.manager.gettext, stream) : dlg._("now"),
query !== null ? ThingTalk.Describe.describeTable(dlg.manager.gettext, query) : '',
action !== null ? ThingTalk.Describe.describePrimitive(dlg.manager.gettext, action) : dlg._("notify me")
];
}