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

    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
    }