Last active
December 21, 2015 19:29
-
-
Save axeda/6354969 to your computer and use it in GitHub Desktop.
Adds a User Group and a User and assigns a range of privileges
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.user.User | |
| import com.axeda.drm.sdk.user.UserFinder | |
| import com.axeda.drm.sdk.user.UserGroup | |
| import com.axeda.drm.sdk.user.UserGroupFinder | |
| import com.axeda.drm.sdk.device.Device | |
| import com.axeda.drm.sdk.device.DeviceGroup | |
| import com.axeda.drm.sdk.device.DeviceGroupFinder | |
| import groovyx.net.http.* | |
| import static groovyx.net.http.ContentType.* | |
| import static groovyx.net.http.Method.* | |
| import groovy.util.XmlSlurper | |
| import com.axeda.services.v2.ExtendedMap | |
| import static com.axeda.sdk.v2.dsl.Bridges.* | |
| import com.axeda.drm.sdk.privilege.PrivilegeFinder | |
| import com.axeda.drm.sdk.device.Model | |
| import com.axeda.drm.sdk.device.ModelFinder | |
| import groovy.xml.XmlUtil | |
| import com.axeda.services.v2.ExtendedMapCriteria | |
| import net.sf.json.* | |
| import com.axeda.services.v2.NamedValue | |
| import groovy.xml.MarkupBuilder | |
| import org.apache.commons.lang.exception.ExceptionUtils | |
| import com.axeda.drm.sdk.scripto.Request | |
| import java.util.regex.Pattern | |
| /** | |
| * AddUserGroupAndUser | |
| * ----------------------- | |
| * Creates a user and user group and assigns a range of privileges | |
| * | |
| * @params | |
| * user (REQUIRED) Str - username of the new user | |
| * pass (REQUIRED) Str - password of the new user | |
| * email (REQUIRED) Str - email of the new user | |
| * model (OPTIONAL) Str - model name of the device group to add | |
| * | |
| * @author sara streeter <sstreeter@axeda.com> | |
| */ | |
| final Context CONTEXT = Context.getSDKContext() | |
| final def contentType = "application/xml" | |
| final def serviceName = "AddUserGroupAndUser" | |
| // utility objects | |
| def writer = new StringWriter() | |
| def xml = new MarkupBuilder(writer) | |
| List<Map> errors = [] | |
| try { | |
| def params = Request.parameters?.size() > 0 ? Request.parameters : parameters | |
| def modelName = params.model != null && params.model != "" ? params.model : "mydefaultmodel" | |
| def userGroupName = "myusergroup" | |
| def userGroupDescription = "myusergroupdescription" | |
| ModelFinder modelFinder = new ModelFinder(CONTEXT) | |
| modelFinder.setName(modelName) | |
| Model model = modelFinder.find() | |
| PrivilegeFinder privFinder = new PrivilegeFinder(CONTEXT) | |
| // Assigns Privileges that contain the words in include_expression | |
| // Excludes Privileges that contain the words in exclude_expression | |
| def include_expression = /(?i)[a-z\-]*(add|view|alarm|extend|modify|device)[a-z\-]*/ | |
| def exclude_expression = /(?i)[a-z\-]*(gas|user|tenant|partner|instruction|report)[a-z\-]*/ | |
| def requiredPrivileges = privFinder.findAll().findAll{ it.name ==~ include_expression && !(it.name ==~ exclude_expression) } | |
| def deviceGroup = model.getDefaultDeviceGroup() | |
| def userGroup = findOrCreateUserGroup( | |
| CONTEXT | |
| , userGroupName | |
| , userGroupDescription | |
| , requiredPrivileges | |
| , deviceGroup | |
| ) | |
| // find or create the user | |
| def user = findOrCreateUser( | |
| CONTEXT | |
| , params.user | |
| , params.pass | |
| , params.email | |
| , userGroup | |
| ) | |
| } | |
| catch (e){ | |
| errors << "Error! "+ExceptionUtils.getFullStackTrace(e) | |
| } | |
| finally { | |
| if (errors.size() > 0) { | |
| xml.Errors() { | |
| errors.each() { error -> | |
| xml.Error(error) | |
| } | |
| } | |
| } else { | |
| xml.Success("User was installed successfully.") | |
| } | |
| } | |
| return createReturnMap(contentType, writer.toString()) | |
| private def createReturnMap(String contentType, String content) { | |
| ["Content-Type": contentType, "Content": content] | |
| } | |
| def findOrCreateDeviceGroup(CONTEXT, deviceGroupName){ | |
| DeviceGroupFinder dgFinder = new DeviceGroupFinder(CONTEXT) | |
| dgFinder.setName(deviceGroupName); | |
| DeviceGroup dg = dgFinder.find(); | |
| if (!dg){ | |
| dgFinder.setName(StringQuery.like("*Root*")); | |
| def rootgroup = dgFinder.find() | |
| dg = new DeviceGroup(context,rootgroup,deviceGroupName) | |
| dg.store() | |
| } | |
| dg | |
| } | |
| def findOrCreateUser(CONTEXT, username, password, email, userGroup){ | |
| UserFinder userFinder = new UserFinder(CONTEXT) | |
| userFinder.setUsername(username) | |
| def user = userFinder.find() | |
| if (!user){ | |
| user = new User(CONTEXT, username, password, email); | |
| user.store(); | |
| } | |
| userGroup.addUser(user) | |
| userGroup.store(); | |
| return user | |
| } | |
| def findOrCreateUserGroup(CONTEXT,userGroupName, userGroupDescription, requiredPrivileges, deviceGroup){ | |
| 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(); | |
| uGroup.addDeviceGroup(deviceGroup) | |
| } | |
| setPrivileges(CONTEXT, uGroup, requiredPrivileges) | |
| uGroup.store(); | |
| return uGroup | |
| } | |
| def setPrivileges(context, userGroup, requiredPrivileges){ | |
| requiredPrivileges.each{ priv -> | |
| userGroup.assignPrivilege(priv) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment