Forums

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

ScriptRunner Adding User To Group, Fails Group check in Same Script

David Harkins
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 15, 2023

While Auditing users added from portal we check to ensure they are in a specified group.

If a new user is created we run a script against to add the user to the group but then further checks that the user is in the group then fail until the script is restarted.

The following is a sample of what I'm working with:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.embedded.api.Group

UserManager userManager = ComponentAccessor.getUserManager() as UserManager
ApplicationUser testUser

testUser = userManager.getUserByKey("JIRAUSER35000")

def groupManager = ComponentAccessor.groupManager
def usersGroups = groupManager.getGroupNamesForUser(testUser.username)
Group addToGroup = groupManager.getGroup('SC-ALL USERS')
addToGroup.add(testUser)
usersGroups = groupManager.getGroupNamesForUser(testUser.username)

I would expect the last userGroups value to contain the group that the user has just been added to.

What am I missing?

 

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
December 16, 2023

Hi David, 

Could you please provide more information, i.e. what are you using to trigger the code? Is it a Post-Function, Listener or something else?

Would be helpful if you could share a screenshot of your configuration.

Thank you and Kind regards,

Ram

David Harkins
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 18, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

The above is currently being run in the ScriptRunner Console for testing.

The finished process will be started by a Listener.

It seems there needs to be a step to refresh the user groups before querying them again in the same script.

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
December 18, 2023

Hi @David Harkins

I will test this in my environment and get back to you.

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
December 18, 2023

Hi @David Harkins

For your requirement, I suggest using a simpler approach via ScriptRunner's HAPI.

To add a user to a Group via the ScriptRunner console, all you have to do is:-

import com.atlassian.jira.component.ComponentAccessor

def user = Users.getByName('<Username>') //set the username
def group = Groups.getByName('<Group Name>') //set the group name
group.add(user)

def groupManager = ComponentAccessor.groupManager

log.warn "======>>>> ${group.contains(user)}"
log.warn "<<<<<===== ${user.isMemberOfGroup(group)}"
log.warn "========>>>>> ${groupManager.getGroupNamesForUser(user)}"

For example:-

import com.atlassian.jira.component.ComponentAccessor

def user = Users.getByName('ramk')
def group = Groups.getByName('jira-servicedesk-users')
group.add(user)

def groupManager = ComponentAccessor.groupManager

log.warn "======>>>> ${group.contains(user)}"
log.warn "<<<<<===== ${user.isMemberOfGroup(group)}"
log.warn "========>>>>> ${groupManager.getGroupNamesForUser(user)}"

Below is a screenshot of the ScriptRunner console:-

console.png

As you can see in the screenshot above, when the code is tested, the verification returns true along with the group name the user belongs to in the log output.

Also, when checking the User Management page, as expected, the user is added to the group as shown in the screenshot below:-

test1.png

I hope this helps so solve your question. :-)

Thank you and Kind regards,

Ram 

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
December 26, 2023

Hi @David Harkins

Has your question been answered?

If yes, please accept the answer provided.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer