Forums

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

I need help to desactivate users that have never logged

Firas hammami March 25, 2021

Hi 

I need to make a scriptRunner to desactivate users that have never logged, I'm using Jira 7

Have you any idea please?

 

Thank you 

2 answers

1 accepted

3 votes
Answer accepted
Fabio Oliveira Silva March 25, 2021

Hello @Firas hammami there is another topic here with detailed information about this task, please take a look and let me know if you need any other information:

https://community.atlassian.com/t5/Adaptavist-questions/Scriptrunner-deactivate-inactive-users-script-needs-to-be/qaq-p/914110

Firas hammami March 25, 2021

Hi @Fabio Oliveira Silva  

thank you for your answer, this script can desactivate all users that have been inactive for a certain period, I dit something like this ans it works, but my problem is to desactivate users that have never logged in 

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.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
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 = 300 // 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)
UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
UserSearchParams userSearchParams = new UserSearchParams(true, true, false);
List<ApplicationUser> userList = userSearchService.findUsers("", userSearchParams);
ApplicationUser updateUser
UserService.UpdateUserValidationResult updateUserValidationResult

long count = 0

GroupManager groupManager = ComponentAccessor.getGroupManager();

userList.findAll{it.isActive()}.each {

if(groupManager.isUserInGroup(it.getName(),"jira-administrators"))
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"
Fabio Oliveira Silva March 25, 2021

Hello @Firas hammami in this case, the never logged-in users should be the ones that have an empty lastLoginMillis information so an else block on the if (lastLoginMillis?.isNumber()) should work, but the problem is that you can deactivate users recently created. I tried to found a way to check created date for a user without success, maybe you can create a user list directly from the database and export it as CSV

Firas hammami March 25, 2021

I jut puted an else after if (lastLoginMillis?.isNumber())  to get all  the never active users, it works, thank you very much 

3 votes
Mahima_miniOrange_SSO
Atlassian Partner
March 25, 2021

Hello @Firas hammami ,

This is Mahima from miniOrange.

If you are looking for plugin which can fulfill your requirements, I would like to suggest you User Management Plugin which can automatically deactivate the users who have never logged in or who have not been active since long time which can help you to manage your users easily in just one click

Feel free to reach out to us at atlassiansupport@xecurify.com or you can raise the ticket in case you need any help!

Thanks,

Mahima

Firas hammami March 25, 2021

Hello @Mahima_miniOrange_SSO  

 

Thank you for your answer, yes we will use this plugin in the near future 

Like Mahima_miniOrange_SSO likes this

Suggest an answer

Log in or Sign up to answer