cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Programmatic way of setting permissions

vgala
1-Newbie

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 8
ibanham
1-Newbie
(To:vgala)

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

vgala
1-Newbie
(To:ibanham)

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.

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

Dear Team,

I have tried using service but getting error.

AddPermission   service

var params = {

    principal: "ReliabilityEngineer"/* STRING */,

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

    resource: "*" /* STRING */,

    type: "DENY" /* STRING */,

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

};

Things[ThingName].AddRunTimePermission(params);

Error :  Wrapped java.lang.Exception: Invalid Permission Type : [DENY] Cause: Invalid Permission Type : [DENY]

ankigupta
5-Regular Member
(To:bgupta-2)

Hi Bhuwaneshwar Gupta​,

Type does not have option for DENY. Please refer to Service definition below:

I hope it helps.

var params = {

    principal: "ReliabilityEngineer" /* STRING */,

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

    type: "ServiceExecute" /* STRING */,

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

};

Things["Asset1"].AddRunTimePermission(params);

On running above service

Wrapped java.lang.Exception: Invalid Permission Type : [ServiceExecute] Cause: Invalid Permission Type : [ServiceExecute]

ankigupta
5-Regular Member
(To:bgupta-2)

Please try ServiceInvoke

Permission: "PropertyWrite" /* STRING */,

OR

 type: Permission /* STRING */,

I need to set permission for all types (EventInvoke, EventSubscribe, PropertyRead, PropertyWrite, ServiceInvoke ) at once, any help in that would be highly appreciated. Thanks....

Regards,

Abhishek Kumar

Top Tags