Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

add user to group through script strange behavior

Derek Fields (RightStar)
Community Champion
February 12, 2019

I have a script that adds a user to a particular group when the user is created in the internal directory. Users created through external directories are ignored. The Audit log shows that the user is added to the target group and then is immediately removed from that group. I can't figure out what would be causing that to happen. 

Script:

import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.crowd.embedded.api.DirectoryType
import com.atlassian.jira.security.groups.GroupManager

def targetGroup = "Customers"
def directoryManager = ComponentAccessor.getComponent(DirectoryManager)
def groupManager = ComponentAccessor.getGroupManager()
def internalDir = directoryManager.findAllDirectories().find { it.getType() == DirectoryType.INTERNAL }

def groupCustomer = groupManager.getGroup(targetGroup)
if (!groupCustomer) {
log.error("Unable to find group ${targetGroup}")
return
}

def newDirUser = event.getUser()
if (newDirUser.getDirectoryId() == internalDir.getId()) {
def newUser = ComponentAccessor.getUserManager().getUserByKey(newDirUser.getName().toLowerCase())
if (!newUser) {
log.error("Unable to retrieve ApplicationUser for ${newDirUser.getName()}")
return
}
try {
groupManager.addUserToGroup(newUser, groupCustomer)
} catch (all) {
log.error("Failed to add ${newUser.getName()} to ${targetGroup}")
}

 This picture shows the audit log records that the user was created, added and removed all at the same time.

Image 6.png

0 answers

Suggest an answer

Log in or Sign up to answer