Skip to main content
12-Amethyst
Apr 14, 2016 11:56 AM UTC
Question

Programmatic way of setting permissions

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.

8 replies

1-Visitor
Apr 15, 2016 1:37 PM UTC

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

Translate
vgala12-AmethystAuthor
12-Amethyst
Apr 29, 2016 6:26 AM UTC

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.

Translate
12-Amethyst
Nov 10, 2016 6:11 PM UTC

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

Translate