Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Deactivate users that have no logged in since x days

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}"
}
}
}}

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

72 views 1 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you