Last active
August 29, 2015 13:57
-
-
Save axeda/9810733 to your computer and use it in GitHub Desktop.
Get Privileges for UserGroups of User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.sf.json.JSONObject | |
import com.axeda.drm.sdk.user.UserGroupFinder | |
import com.axeda.drm.sdk.Context | |
import com.axeda.platform.sdk.v1.services.ServiceFactory | |
import com.axeda.drm.sdk.user.User | |
import com.axeda.drm.sdk.user.UserFinder | |
/** | |
* UserGroupPrivilegesForUser.groovy | |
* | |
* @param user - (OPTIONAL) the username to get privileges for. | |
* | |
* @author sara jarjoura <sjarjoura@axeda.com> | |
*/ | |
def result = [:] | |
// start timings code | |
def timings = [:] | |
timings.wholescript = 0 | |
timings.findUser = 0 | |
timings.getPrivileges = 0 | |
def wholestart = System.currentTimeMillis() | |
def start | |
// end timings code | |
try { | |
def username = parameters.user | |
final def CONTEXT = Context.getSDKContext() | |
start = System.currentTimeMillis() | |
UserGroupFinder ugFinder = new UserGroupFinder(CONTEXT); | |
UserFinder uFinder = new UserFinder(CONTEXT) | |
uFinder.setUsername(username) | |
def theuser = uFinder.find() | |
timings.findUser += System.currentTimeMillis() - start | |
start = System.currentTimeMillis() | |
def groups = ugFinder.findUserGroupforUser(theuser) | |
result.privileges = groups.inject([]){ target, group -> | |
target += group.getPrivileges().inject([]) { sublist, priv -> | |
if (!target.contains(priv.name)){ | |
sublist << priv.name | |
} | |
sublist | |
} | |
target | |
} | |
timings.getPrivileges += System.currentTimeMillis() - start | |
timings.wholescript = System.currentTimeMillis() - wholestart | |
} | |
catch (any) { | |
logger.error any.localizedMessage | |
result = [error: any.localizedMessage] | |
} | |
finally { | |
result += [params: parameters, timings: timings] | |
} | |
return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(result).toString(2)] | |
def findUserGroup(UserGroupFinder finder, String groupName) { | |
finder.setName(groupName) | |
finder.find() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment