Skip to content

Instantly share code, notes, and snippets.

@saranicole
Created July 15, 2014 13:59
Show Gist options
  • Select an option

  • Save saranicole/3a3275ef55af9c26cf9e to your computer and use it in GitHub Desktop.

Select an option

Save saranicole/3a3275ef55af9c26cf9e to your computer and use it in GitHub Desktop.
PrivilegeList
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
import com.axeda.drm.sdk.scripto.Request
import java.util.regex.Pattern
/****
* 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(",") : []
PrivilegeFinder privFinder = new PrivilegeFinder(CONTEXT)
// def expr = /(?i)[a-z\-]*(add|view|alarm|extend|modify|device)[a-z\-]*/
def expr = /(?i)[a-z\-]*(audit)[a-z\-]*/
// def notexpr = /(?i)[a-z\-]*(gas|user|tenant|partner|instruction|report)[a-z\-]*/
def requiredPrivileges = privFinder.findAll(){// .findAll{ logger.info(it.dump())
it.name ==~ expr // && !(it.name ==~ notexpr)
}
requiredPrivileges.each{ logger.info(it.dump())}
// 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