Skip to main content
12-Amethyst
April 14, 2016
Question

Programmatic way of setting permissions

  • April 14, 2016
  • 1 reply
  • 5665 views

I want to set and retrieve runtime permissions for a group on Thing and runtime instance permission on ThingTemplates through Java code. Also once permissions are set  for a group through Java code it should reflect in Composer by navigating to the appropriate entity, correct?

Has anyone done it. Any help would be appreciated.

1 reply

1-Visitor
April 15, 2016

This is what I've done in the past... this is demo standard code so is missing validations and error handling....

1) You need a service for adding Permissions - here's an example:

ServiceName : AddPermissionsToThingForGroup

Inputs :

     Thing - ThingName

     Permission - Permission to grant/deny

     GroupName - GroupName

Output - Nothing

Code:

var params = {

    principal: GroupName /* STRING */,

    allow: true /* BOOLEAN */, //assume false here creates a deny rule

    resource: "*" /* STRING */,

    type: Permission /* STRING */,

    principalType: "Group" /* STRING */ // note this could be user

};

Things[Thing].AddRunTimePermission(params); //This is an OOTB service, but wrapping it like this means I can control access to the service much          more easily.

2) This is an example of how you would call this service in another service:

var params = {

    Thing: <thing to set permissions on> /* THINGNAME */,

    Permission: "PropertyWrite" /* STRING */,

    GroupName: <Name of group who get this permission> /* GROUPNAME */

};

me.AddPermissionsToThingForGroup(params);

Once you've worked out what the correct strings are for the various permissions, this works fine and you do get to see the permissions in composer.

Regards

Ian

vgala12-AmethystAuthor
12-Amethyst
April 29, 2016

Thanks Ian. I actually was actually looking for some samples in Java extension side code had figured it out later. But thanks for taking your time and replying on how you did it through java script. It might help others.

12-Amethyst
November 10, 2016

VivekOm,

     Did you figure it out, how to do it in java extension?

     I have this code, which I was hoping will work, but apparently does not:

          thing.AddRunTimePermission("PropertyRead", "*", "myGroup", "Group", true);

          thing.RestartThing();

EDIT: got it to work by setting super user context before the code above and clearing it after.

Thanks,

Sajid