Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created February 18, 2014 22:24
Show Gist options
  • Save jechlin/9081710 to your computer and use it in GitHub Desktop.
Save jechlin/9081710 to your computer and use it in GitHub Desktop.
package examples.usermgr
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
/**
* Remove user from all groups and deactivate
*/
/* CONFIGURABLE SECTION */
// custom field names
final def userNameField = "Username"
/* END CONFIGURABLE SECTION */
def issue = issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customFields = customFieldManager.getCustomFieldObjects(issue)
def userUtil = ComponentAccessor.getUserUtil()
[userNameField].each {fieldName ->
def field = customFields.find { it.name == fieldName }
assert field // fields not found or not associated with issue
}
def username = issue.getCustomFieldValue(customFields.find {it.name == userNameField}) as String
def userService = ComponentAccessor.getComponent(UserService.class)
def user = userUtil.getUserByName(username).getDirectoryUser()
def updateUser = ImmutableUser.newUser(user).active(false).toUser()
def updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid()) {
userService.updateUser(updateUserValidationResult)
} else {
log.warn "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment