Please let me know if the script is correct, running the script is not giving me any output but in the logs, I am getting lost of users failed to deactivate.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.UserService
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.security.login.LoginManager
import com.atlassian.jira.bc.security.login.LoginInfo
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.security.login.LoginManager
import com.atlassian.jira.user.util.UserManager
import java.sql.Timestamp
import com.atlassian.jira.bc.security.login.LoginService
UserManager userManager = ComponentAccessor.getUserManager()
def loginManager = ComponentAccessor.getComponent(LoginManager)
//def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)
UserService userService = ComponentAccessor.getComponent(UserService)
ApplicationUser updateUser
UserService.UpdateUserValidationResult updateUserValidationResult
UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
UserSearchParams userSearchParams = new UserSearchParams(true, true, false);
List<ApplicationUser> userList = userSearchService.findUsers("", userSearchParams);
userList.findAll{it.isActive()}.each
{
for (user in userList)
{
def lastLoginMillis = loginManager.getLoginInfo(user.name).getLastLoginTime()
def disable = false
if(!lastLoginMillis)
{
//user has never logged in
disable = true
}
else
{
Date cutoff = new Date().minus(1000)
Date last = new Date(lastLoginMillis)
if(last.before(cutoff))
{
disable = true
}
}
if(disable == true)
{
def userAsImmutable = new ImmutableUser(user.directoryId, user.name, user.displayName, user.emailAddress, false)
updateUser = ApplicationUsers.from(userAsImmutable)
updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid())
{
userService.updateUser(updateUserValidationResult)
log.info "Deactivated ${updateUser.name}"
}
else
{
log.error "Failed to deactivate ${updateUser.name}"
}
}
}}