Simplify permission handling in widgets and services
Change-Id: I5050b0dad19f84eaf62e051978f0abc5c6a22800
diff --git a/dev/js/src/plugin/server.js b/dev/js/src/plugin/server.js
index 2422892..4504e56 100644
--- a/dev/js/src/plugin/server.js
+++ b/dev/js/src/plugin/server.js
@@ -149,12 +149,6 @@
// "this".button is the button
// "that" is the server object
- // Get the URL of the widget
- let url = onClick["template"];
- // that._interpolateURI(onClick["template"], this.match);
-
- let perm = onClick["permissions"];
-
// The button has a state and the state is associated to the
// a intermediate object to toggle the view
if ('state' in this.button && this.button.state.associates() > 0) {
@@ -184,7 +178,11 @@
};
// Add the widget to the panel
- let id = that.addWidget(this, name, url, perm);
+ let id = that.addWidget(this, {
+ "name": name,
+ "src": onClick["template"], // that._interpolateURI(onClick["template"], this.match);
+ "permissions": onClick["permissions"]
+ });
plugin["widgets"].push(id);
// If a state exists, associate with a mediator object
@@ -240,16 +238,14 @@
// Lazy registration (see above!)
KorAP.Panel[panel].actions.addToggle(title, {'cls':["title"]}, state);
- // Get the URL of the service
-
- // TODO:
- // Use the "service" keyword
- let url = onClick["template"];
-
// Add the service
- let id = this.addService(name, url);
-
- let perm = onClick["permissions"];
+ let id = this.addService({
+ "name" : name,
+ // TODO:
+ // Use the "service" keyword
+ "src" : onClick["template"],
+ "permissions" : onClick["permissions"]
+ });
// TODO:
// This is a bit stupid to get the service window
@@ -338,17 +334,16 @@
/**
* Add a service in a certain panel and return the id.
*/
- addService : function (name, src, permissions) {
- if (!src)
+ addService : function (data) {
+ if (!data["src"])
return;
let id = this._getServiceID();
+ data["id"] = id;
+
// Create a new service
- let service = serviceClass.create(name, src, id);
- if (permissions != undefined) {
- service.allow(permissions);
- };
+ let service = serviceClass.create(data);
services[id] = service;
limits[id] = maxMessages;
@@ -366,15 +361,15 @@
* Open a new widget view in a certain panel and return
* the id.
*/
- addWidget : function (panel, name, src, permissions) {
+ addWidget : function (panel, data) {
+ // panel, name, src, permissions
let id = this._getServiceID();
+ data["id"] = id;
+
// Create a new widget
- var widget = widgetClass.create(name, src, id);
- if (permissions != undefined) {
- widget.allow(permissions);
- };
+ var widget = widgetClass.create(data);
// Store the widget based on the identifier
services[id] = widget;
diff --git a/dev/js/src/plugin/service.js b/dev/js/src/plugin/service.js
index 4029349..23271e5 100644
--- a/dev/js/src/plugin/service.js
+++ b/dev/js/src/plugin/service.js
@@ -2,18 +2,26 @@
"use strict";
return {
- create : function (name, src, id) {
- return Object.create(this)._init(name, src, id);
+ create : function (data) {
+ return Object.create(this)._init(data);
},
// Initialize service
- _init : function (name, src, id) {
- if (!name || !src || !id)
+ _init : function (data) {
+ if (!data || !data["name"] || !data["src"] || !data["id"])
throw Error("Service not well defined");
- this.name = name;
- this.src = src;
- this.id = id;
+
+ this.name = data["name"];
+ this.src = data["src"];
+ this.id = data["id"];
this._perm = new Set();
+
+ let perm = data["permissions"];
+ if (perm && Array.isArray(perm)) {
+ perm.forEach(
+ p => this._perm.add(p)
+ );
+ };
// There is no close method defined yet
if (!this.close) {
@@ -43,7 +51,7 @@
e.setAttribute('allowTransparency',"true");
e.setAttribute('frameborder', 0);
// Allow forms in Plugins
- e.setAttribute('sandbox', this._permString());
+ e.setAttribute('sandbox', Array.from(this._perm).sort().join(" "));
e.style.height = '0px';
e.setAttribute('name', this.id);
e.setAttribute('src', this.src);
@@ -52,25 +60,6 @@
return e;
},
- allow : function (permission) {
- if (Array.isArray(permission)) {
- permission.forEach(
- p => this._perm.add(p)
- );
- }
- else {
- this._perm.add(permission);
- };
-
- if (this._load) {
- this._load.setAttribute('sandbox', this._permString());
- }
- },
-
- _permString : function () {
- return Array.from(this._perm).sort().join(" ");
- },
-
/**
* Send a message to the embedded service.
*/
diff --git a/dev/js/src/plugin/widget.js b/dev/js/src/plugin/widget.js
index a845ebc..e661e6d 100644
--- a/dev/js/src/plugin/widget.js
+++ b/dev/js/src/plugin/widget.js
@@ -16,8 +16,8 @@
/**
* Create new widget
*/
- create : function (name, src, id) {
- return Object.create(viewClass)._init(['widget']).upgradeTo(serviceClass)._init(name, src, id).upgradeTo(this)._init();
+ create : function (data) {
+ return Object.create(viewClass)._init(['widget']).upgradeTo(serviceClass)._init(data).upgradeTo(this)._init();
},
// Initialize widget