The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

Get and remove all groups from User in Crowd

Salim Hammar
Contributor
October 10, 2022

I am looking to be able to recover all the groups in crowd of a user and be able to delete them here is my code but I do not see where the error can you help me?

 


import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.parameters.annotation.UserPicker
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.User

def logger = Logger.getLogger("com.onresolve.ScriptRunnerLogFile")
logger.setLevel(Level.DEBUG)

//Changer ? car on ne voit pas les inactive
@UserPicker(label = "Assignee", description = "Issues with this assignee will be permanently deleted")
ApplicationUser userName

CrowdService crowdService = ComponentAccessor.crowdService
User user = crowdService.getUserWithAttributes(userName.getName())

UserUtil userUtil = ComponentAccessor.getUserUtil()
Collection<String> groups = userUtil.getGroupNamesForUser(userName.getUsername())

groups.each
{
String group ->
if(crowdService.isUserDirectGroupMember(user, crowdService.getGroup(group)))
{
logger.info(group)
 userUtil.removeUserFromGroup(group, userName.getUsername())
}
}

log.info("The all groups has deleted")

 

Thanks in advance

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Nic Brough -Adaptavist-
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 Champions.
October 11, 2022

You might be casting objects around the wrong way.  But you don't actually need to make it as complex as you have

CrowdService crowdService = ComponentAccessor.crowdService
def user = crowdService.getUserWithAttributes(userName)
UserUtil userUtil = ComponentAccessor.getUserUtil()
def groups = userUtil.getGroupNamesForUser( user)

 groups.each

{
ogger.info(group)
 userUtil.removeUserFromGroup(group, user)
}
}
I don't know where the script is running so I don't know how you might be feeding in the user you want to run this against.
0 votes
Salim Hammar
Contributor
October 11, 2022

Hi @Nic Brough -Adaptavist-  

 

I need help can you see my problem please ?

 

Thanks in advance