Hi!
I am using the below script to deactivate the users not logged in the last 150 days:
https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users - For JIRA 7
But, we observed that it also deactivates other users who did not log in for long but we still require them for other purpose. And so basically we want to exclude these set of users.
What we have thought of is that we will include them in a group and exclude this group from the script.
Could someone please help with the necessary modification to the script to exclude a mentioned group from getting deactivated?
If you have better suggestions, we would love to hear from you.
Thanks a lot!
Cheers,
Parag Thakkar
Here you are. Replace "groupName" with actual name of your group:
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil
int numOfDays = 100 // Number of days the user was not logged in
Date dateLimit = (new Date())- numOfDays
UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService userService = ComponentAccessor.getComponent(UserService)
ApplicationUser updateUser
UserService.UpdateUserValidationResult updateUserValidationResult
long count = 0
GroupManager groupManager = ComponentAccessor.getGroupManager();
userUtil.getUsers().findAll{it.isActive()}.each {
if(groupManager.isUserInGroup(it.getName(),"groupName"))
UserWithAttributes user = crowdService.getUserWithAttributes(it.getName())
String lastLoginMillis = user.getValue('login.lastLoginMillis')
if (lastLoginMillis?.isNumber()) {
Date d = new Date(Long.parseLong(lastLoginMillis))
if (d.before(dateLimit)) {
updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid()) {
userService.updateUser(updateUserValidationResult)
log.info "Deactivated ${updateUser.name}"
count++
} else {
log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
}
}
}
}
"${count} users deactivated.\n"
Hi Vasiliy,
Thanks a lot!!
Just to confirm: This will only deactivate the user if he belongs to the "groupName" group? If yes, I need that it deactivates only if it DOES NOT belong to the group.
Cheers,
Parag
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here is corrected code. User into specified group are ignored
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil
int numOfDays = 100 // Number of days the user was not logged in
Date dateLimit = (new Date())- numOfDays
UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService userService = ComponentAccessor.getComponent(UserService)
ApplicationUser updateUser
UserService.UpdateUserValidationResult updateUserValidationResult
long count = 0
GroupManager groupManager = ComponentAccessor.getGroupManager();
userUtil.getUsers().findAll{it.isActive()}.each {
if(groupManager.isUserInGroup(it.getName(),"groupName"))
return;
UserWithAttributes user = crowdService.getUserWithAttributes(it.getName())
String lastLoginMillis = user.getValue('login.lastLoginMillis')
if (lastLoginMillis?.isNumber()) {
Date d = new Date(Long.parseLong(lastLoginMillis))
if (d.before(dateLimit)) {
updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid()) {
userService.updateUser(updateUserValidationResult)
log.info "Deactivated ${updateUser.name}"
count++
} else {
log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
}
}
}
}
"${count} users deactivated.\n"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I guess this is just for Crowd, how about LDAP and Internal Directory?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For us it worked on internal derictory and on LDAP.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In the same script, once the loop begin over the user list i.e. ".each { <closure >} "
Then in the first line of closure you can add a if statement which checks if the user is part of the specific group and if it's not then proceed with "deactivating" the user otherwise don't.
You can get GroupManager using
ComponentAccessor.groupManager
and then use this API to check if the user belongs to specific group or not.
boolean | isUserInGroup(ApplicationUser user, String groupName) Returns true if the user is a member of the named group. |
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.