Permission : Permission control

更新时间:
2024-05-15
下载文档

Permission : Permission control

This module is EdgerOS permission check and asynchronous reminder module.

User can use the following code to import the permission module.

var permission = require('permission');

Support

The following shows permission module APIs available for each permissions.

 User ModePrivilege Mode
permission.update
permission.check
permission.device
permission.request
permission.fetch
permission.isDenied

Permission Introduction

EdgerOS application permissions are a tree structure, stored in an object, and App can check whether they have this permission by checking related properties.

EdgerOS permission tree object contains the following members:

  • ainn {Boolean} Whether AI Neural Network Computing is allowed.
  • alarm {Boolean} Is there permission to add alarms.
  • share {Boolean} Does this app allow sharing of information with other apps.
  • notify {Boolean} Whether this app allows push messages.
  • advnwc {Boolean} Whether this app allows advanced network control.
  • network {Boolean} Whether this app allows network communication.
  • display {Boolean} Whether this app allows use display output.
  • rtsp {Boolean} Whether this app allows RTSP network such as: Webcam, Network microphone.
  • lora {Boolean} Whether this app allows send or receive data via LoRaWAN network.
  • coap {Boolean} Whether this app allows CoAP IoT network protocol.
  • wallpaper {Boolean} Whether this app allows set wallpaper.
  • account {Boolean} Whether to allow get the user list and group list.
  • printer {Boolean} Whether this app allows to use printer.
  • auxstorage {Boolean} Whether this app allows to use auxiliary storage.
  • vpn {Boolean} Whether this app allows to create and manage VPN networks (temporarily closed).
  • mqtt {Object} MQTT Client sub object.
    • publish {Boolean} Whether to allow applications to publish data using the MQTT protocol.
    • subscribe {Boolean} Whether to allow applications to subscribe to messages using MQTT protocol.
  • mediacenter {Object} Media Center access permission.
    • readable {Boolean} Can read media center content.
    • writable {Boolean} Can write media center content.
    • removable {Boolean} Can remove media center content.
  • vehicle {Object} Vehicle features.
    • media {Boolean} Audio-visual entertainment system.
    • geolocation {Boolean} Vehicle geolocation.
    • diagnostic {Boolean} Vehicle diagnostic information.
    • cockpit {Boolean} Cockpit controls, such as air conditioning, car windows.
    • drive {Boolean} Driving and autonomous driving related functions.
  • devices {Array} List of allowed device IDs.

Enabling auxstorage permission requires restarting the App before EdgerOS 2.1.3 to take effect. For EdgerOS 2.1.3 and above, it can take effect immediately without restarting the App.

Permission Functions

permission.update(callback)

  • callback {Function} This callback function will be called when the current application has permission to change.
    • perm {Object} New complete permission table including devices.

Install a permission change callback function.

Example

permission.update(function(perm) {
  console.log('Current App permissions are changed.');
});

permission.check(permChk, callback)

  • permChk {Object} Need to determine the permission object.
  • callback {Function} Permission comparison result callback function.
    • res {Boolean} Whether the current application has the specified permissions.

Checks if the application has the specified permissions.

Example

// Can detect one or more permissions at the same time.
var perm = {
  alarm: true
};

permission.check(perm, function(res) {
  if (res) {
    console.log('Current App allows creating alarms.');
  }
});

permission.device(devid, callback)

  • devid {String} Device ID.
  • callback {Function} Permission comparison result callback function.
    • res {Boolean} Whether the current application has the specified device operate permissions.

Checks if the application has the specified device operate permissions.

Example

permission.device('a2637efe80893d22', function(res) {
  if (res) {
    console.log('Good!');
  }
});

permission.request([perm])

  • perm {Object} Need to tell the Setting App what permissions we wants.

Call Setting App to set permissions. This App must have share permissions, otherwise can only notify customers via UI to set permissions.

Example

// Can detect one or more permissions at the same time.
var perm = {
  alarm: true
};

permission.check(perm, function(res) {
  if (!res) {
    permission.request(perm);
  }
});

permission.request([devid])

  • devid {Object} Need to tell the Setting App what device we wants to control.

Call Setting App to set device permissions. This App must have share permissions, otherwise can only notify customers via UI to set permissions.

Example

permission.device('a2637efe80893d22', function(res) {
  if (!res) {
    permission.request('a2637efe80893d22');
  }
});

permission.fetch([callback])

  • callback {Function} Callback.
    • error {Error} Specify an error if it fails.
    • perm {Object} Complete permission table including devices.

Fetch the current App permissions, if successful, permission.check() and permission.device() are both synchronous operations after this call.

Example

permission.fetch(function(error, perm) {
  if (perm) {
    if (perm.network) {
      console.log('Current App has network permission!');
    }
    if (perm.devices.includes('xxxx'))  {
      console.log('Current App can request and control device xxxx!');
    }
  }
});

permission.isDenied(error)

  • error {String | Error} Error.
  • Returns: {String} Is this error a permission denied error.

Determine whether the error message is a permission denied error message.

Example

var Device = require('device');

var dev = new Device();
dev.request(devid, function(error) {
  if (permission.isDenied(error)) {
    console.log('No permission....');
  }
});
文档内容是否对您有所帮助?
有帮助
没帮助