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

Axeda Groovy Script : Getting Users from a User Group and filtering by username

No ratings

This groovy script will return a list of users based off a given UserGroup and allows for filtering by username.

import com.axeda.drm.sdk.Context

import groovyx.net.http.HTTPBuilder

import static groovyx.net.http.ContentType.*

import static groovyx.net.http.Method.*

import net.sf.json.JSONObject

import groovy.json.*

import com.axeda.drm.sdk.data.*

import com.axeda.drm.sdk.device.*

import com.axeda.drm.sdk.user.UserFinder

import com.axeda.drm.sdk.user.User

import com.axeda.drm.sdk.user.UserGroupFinder

//--------------------------------------------------------------------------------------------------------------------

// Example of getting Users from a User Group and filtering by username

//

//--------------------------------------------------------------------------------------------------------------------

def response = [:]

def result = []

try {

    final def CONTEXT = Context.create(parameters.username)

 

    UserFinder uFinder = new UserFinder(CONTEXT)

    UserGroupFinder ugFinder = new UserGroupFinder(CONTEXT)

    List userGroups = getUserGroupsList(ugFinder, "*Demo*")

    List SmithsInDemoGroup = userGroups.collect{ usergroup ->

        usergroup.getUsers().findResults{ user ->

            if (user.username =~ /Smith/){                     

                user                         

            }             

        }

    }.flatten()

    SmithsInDemoGroup.each{ u ->

        result << u.fullName

    }

  response = [

    result: [

            items: result

    ]

  ]

}

catch (Exception e) {

    def m = ""

    e.message.each { ex -> m += ex }

    response = [

                faultcode: 'Groovy Exception',

                faultstring: m

            ];

}

return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(response).toString(2)];

 

def getUserGroupsList(UserGroupFinder ugFinder, String name){

    ugFinder.setName(StringQuery.like(name))

    def userGroup = ugFinder.findOne()

    List userGroups = new ArrayList();

    userGroups.add(userGroup);

    return userGroups

}

Version history
Last update:
‎May 25, 2016 11:32 AM
Updated by:
Labels (2)