Created
August 2, 2013 14:11
-
-
Save axeda/6140186 to your computer and use it in GitHub Desktop.
Find or create a user group, add any privileges, then list out the privileges in alpha order.
This file contains hidden or 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 com.axeda.drm.sdk.Context | |
| import com.axeda.drm.sdk.privilege.PrivilegeFinder | |
| import com.axeda.drm.sdk.user.UserGroup | |
| import com.axeda.drm.sdk.user.UserGroupFinder | |
| /**** | |
| * PrivilegeList.groovy | |
| * -------- | |
| * Find or create a user group, add any privileges, then list out the privileges in alpha order. | |
| * | |
| * @param | |
| * name (REQUIRED) Str - the name of the user group | |
| * description (OPTIONAL) Str - the description of the user group | |
| * requiredPrivileges (OPTIONAL) Str - comma-delimited list of Privileges to add | |
| * | |
| **/ | |
| // sample value for requiredPrivileges | |
| def mockRequiredPrivileges = "add-action,add-contact" | |
| final Context CONTEXT = Context.getSDKContext() | |
| def response = "" | |
| try { | |
| def description = Request.parameters.description ? Request.parameters.description : "" | |
| def requiredPrivileges = Request.parameters.requiredPrivileges ? | |
| Request.parameters.requiredPrivileges.split(",") : [] | |
| def usergroup = findOrCreateUserGroup(CONTEXT, Request.parameters.name, description, requiredPrivileges) | |
| response = usergroup.privileges.collect {it.name}.sort().join(",\n") | |
| } | |
| catch (Exception e){ | |
| logger.info e.localizedMessage | |
| } | |
| return ["Content-Type": "text/plain", "Content": response] | |
| def findOrCreateUserGroup(CONTEXT,userGroupName, userGroupDescription, requiredPrivileges){ | |
| UserGroupFinder ugFinder = new UserGroupFinder(CONTEXT) | |
| ugFinder.setName(userGroupName) | |
| UserGroup uGroup = ugFinder.find() | |
| if (!uGroup){ | |
| uGroup = new UserGroup(CONTEXT,userGroupName, userGroupDescription,null); | |
| uGroup.setDeviceGroupSecurity(true); | |
| uGroup.setOrganizationSecurity(false); | |
| uGroup.setLocationSecurity(false); | |
| uGroup.setRegionSecurity(false); | |
| uGroup.store(); | |
| } | |
| if (requiredPrivileges?.size() > 0){ | |
| setPrivileges(CONTEXT, uGroup, requiredPrivileges) | |
| } | |
| return uGroup | |
| } | |
| def findPrivilege(context, privilegename){ | |
| PrivilegeFinder privFinder = new PrivilegeFinder(context) | |
| privFinder.setName( privilegename) | |
| return privFinder.find() | |
| } | |
| def setPrivileges(context, userGroup, requiredPrivileges){ | |
| PrivilegeFinder privFinder = new PrivilegeFinder(context) | |
| def allprivs = privFinder.findAll() | |
| requiredPrivileges.each{ privName -> | |
| def priv = allprivs.find{ it.name == privName } | |
| if (priv){ | |
| userGroup.assignPrivilege(priv) | |
| } | |
| } | |
| userGroup.store(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment