UNPKG

node-red-contrib-snap4city-user

Version:

Nodes for Snap4city project, targeted to standard user (no developer)

246 lines (228 loc) 9.86 kB
<!--/* NODE-RED-CONTRIB-SNAP4CITY-USER Copyright (C) 2018 DISIT Lab http://www.disit.org - University of Florence This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */--> <script type="text/javascript"> RED.nodes.registerType('delegate-my-device', { category: 'S4CIoT', color: '#a6bbcf', defaults: { name: { value: "" }, deviceId: { value: "" }, deviceDataList: { value: "{}" }, selectedDeviceDataId: { value: "" }, delegatedUser: { value: "" }, kind: { value: "READ_ACCESS" }, delegatedGroup: { value: "" }, authentication: { type: "snap4city-authentication", required: false } }, outputs: 1, inputs: 1, icon: "white-globe.png", label: function () { return this.name || "delegate-my-device"; }, oneditprepare: function () { $("#node-input-currentNodeId").val(this.id); $("#node-input-deviceId").val(""); var accessToken = ""; var deviceDataList = ""; $.ajax({ url: "retrieveAccessTokenAuthentication", type: "POST", async: false, dataType: "json", data: { "authenticationNodeId": $("#node-input-authentication").val(), "nodeId": $("#node-input-currentNodeId").val() }, success: function (_data) { if (_data.accessToken == "") { $("#selectDeviceDataContainer").hide(); $('#tipAuthenticationWrong').show(); $('#tipAuthentication').hide(); } else { accessToken = _data.accessToken; }; } }); refreshDeviceDataSelection(accessToken, deviceDataList); }, oneditsave: function () { $("#node-input-selectedDeviceDataId").val($("#node-input-deviceId").val()); $("#node-input-name").val("Delegate my device" + $("#node-input-deviceId").val()); } }); var refreshDeviceDataSelection = function (accessToken, deviceDataList) { $("#node-input-deviceId").empty(); if (deviceDataList != "") { createDeviceDataSelection(deviceDataList); } else if (accessToken != "" && deviceDataList == "") { $.ajax({ url: "ownershipUrl", type: "GET", async: false, success: function (_data) { var domain = (RED.nodes.node($("#node-input-authentication").val()) != null ? RED.nodes.node($("#node-input-authentication").val()).domain : ""); if (domain != "") { _data.ownershipUrl = domain + "/ownership-api/"; } $.ajax({ url: _data.ownershipUrl + "v1/list/?type=IOTID&accessToken=" + accessToken, type: "GET", async: true, dataType: "json", success: function (_data) { createDeviceDataSelection(_data); }, error: function (err) { console.log(err); } }); } }); } } var createDeviceDataSelection = function (currentDeviceDataList) { console.log(currentDeviceDataList); $("#node-input-deviceDataList").val(JSON.stringify(currentDeviceDataList)); currentDeviceDataList.sort(function (a, b) { if (a.elementName < b.elementName) { return -1; } if (a.elementName > b.elementName) { return 1; } return 0; }); if (currentDeviceDataList.length != 0) { $("#node-input-deviceId").empty(); var currentValue = $("#node-input-selectedDeviceDataId").val(); for (var i = 0; i < currentDeviceDataList.length; i++) { $("<option value='" + currentDeviceDataList[i].elementId + "'>" + currentDeviceDataList[i].elementName + "</option>").appendTo($("#node-input-deviceId")); } $("#node-input-deviceId").val(currentValue); } } </script> <script type="text/x-red" data-template-name="delegate-my-device"> <input type="hidden" id="node-input-name"> <input type="hidden" id="node-input-currentNodeId"> <input type="hidden" id="node-input-selectedDeviceDataId"> <input type="hidden" id="node-input-deviceDataList"> <div class="form-row" id="rowAuthentication"> <label for="node-input-authentication">Authentication</label> <input type="text" id="node-input-authentication"> </div> <div class="form-row" id="selectDeviceDataContainer"> <label for="node-input-deviceId"> <i class="fa fa-dashboard"></i>Select Device</label> <select id="node-input-deviceId" style="width: 55%;"> </select> </div> <div class="form-row"> <label for="node-input-kind">Kind</label> <select id="node-input-kind"> <option value="READ_ACCESS">READ_ACCESS</option> <option value="READ_WRITE">READ_WRITE</option> <option value="MODIFY">MODIFY</option> <option value="WRITE_ONLY">WRITE_ONLY</option> </select> </div> <div class="form-row"> <label for="node-input-delegatedUser">User Delegated</label> <input type="text" id="node-input-delegatedUser" placeholder="Username Delegated"> </div> <div class="form-row"> <label for="node-input-delegatedGroup">Group Delegated</label> <input type="text" id="node-input-delegatedGroup" placeholder="Group Delegated"> </div> <div class="form-tips" id="tipAuthentication" style="max-width: none"> You must have an account with Snap4city to use this node. You can register for one <a href="https://www.snap4city.org" target="_blank">here</a>. </div> <div class="form-tips" id="tipAuthenticationWrong" style="display:none; max-width: none"> Maybe the authentication data are wrong. Check them and redeploy the node. </div> <div class="form-tips" id="tipAuthenticationRedeploy" style="display:none; max-width: none"> Deploy the node to retrieve the updated list of dashboards. </div> <div class="form-tips" id="tipAuthenticationDeployFirstTime" style="display:none; max-width: none"> Insert the authentication data and deploy the node to retrieve the updated list of dashboards. </div> </script> <script type="text/x-red" data-help-name="delegate-my-device"> <p>It allows to delegate a device.</p> <h3>Inputs</h3> A JSON with these parameters: <dl class="message-properties"> <dt>id <span class="property-type">string</span> </dt> <dd> the nome of the device (you MUST have the ownership of the device)</dd> <dt>contextbroker <span class="property-type">string</span> </dt> <dd> the contextbroker of the device</dd> <dt>kind <span class="property-type">string</span> </dt> <dd> Kind of delegation. You can choose between READ_ACCESS,READ_WRITE, MODIFY and WRITE_ONLY. <dt>usernamedelegated <span class="property-type">string</span> </dt> <dd> Username of the person to be delegated to view the device. It is recommended that you set one of the two delegations (user or group) <dt>groupdelegated <span class="property-type">string</span> </dt> <dd> Group to be delegated to view the device. To delegate to a group, you must set cn=GROUP_NAME,ou=ORGANIZATION_NAME. It is recommended that you set one of the two delegations (user or group) </dl> <p>An example of the JSON array filled with correct data for user delegation:</p> <pre> { "id": "nameDevice", "usernamedelegated": "username", "contextbroker": "contextbroker", "kind": "READ_ACCESS" } </pre> <p>An example of the JSON array filled with correct data for group delegation:</p> <pre> { "id": "nameDevice", "groupdelegated": "cn=GROUP_NAME,ou=ORGANIZATION_NAME", "contextbroker": "contextbroker", "kind": "READ_ACCESS" } </pre> <h3>Outputs</h3> <dl class="message-properties"> <dd> Returns an object containing the delegation</dd> </dl> <h3>Details</h3> <p>The node can receive a JSON with the parameters described in the Inputs section and with them generate the output JSON. If the values are not present in the input JSON, these are read by those in the configuration. If they are not present in either part, an error is generated for the necessary parameters.</p> </script>