7 Replies Latest reply on May 16, 2017 7:29 AM by Ankit Gupta RSS
    vgala Apprentice

    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.

      • Re: Programmatic way of setting permissions
        ianban Creator

        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