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?
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
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I will test this in my environment and get back to you.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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:-
I hope this helps so solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Has your question been answered?
If yes, please accept the answer provided.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.