Forums

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

scriptRunner deactivate all user that never been logged in

Firas hammami March 24, 2021

Hello 

I have a script that desactivate all users that are inactive, it works fine, now I need to desactive all users that never been logged in

this is my script 

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.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.bc.security.login.LoginService

//
def loginManager = ComponentAccessor.getComponent(LoginService)
//
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
def list = ['115058']
def userIsInList = false
//

//
userUtil.getUsers().findAll{it.isActive() || !loginManager.getLoginInfo(it.username).lastLoginTime}.each {
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) && !(user.name in list) ) {
updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
updateUserValidationResult = userService.validateUpdateUser(updateUser)
//if (count < 10) {
if (updateUserValidationResult.isValid()) {

//userService.updateUser(updateUserValidationResult)
//log.info "Deactivated ${updateUser.name}"

count++

log.info "this one --> ${user.displayName}"
} else {
log.info "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
}
//}
}
}
}

"${count} users deactivated.\n"

 

I think !loginManager.getLoginInfo(it.username).lastLoginTime is a wrong condition 

have you any idea please?

 

Thank you

 

1 answer

0 votes
Hana Kučerová
Community Champion
March 24, 2021

Hi @Firas hammami ,

according to this script the condition

!loginManager.getLoginInfo(it.username).lastLoginTime

looks ok.

I think the problem is you are later checking last login, but these users don't have any. 

So these users won't get through the condition:

if (lastLoginMillis?.isNumber()) {
Firas hammami March 25, 2021

Hi 

thank you for your respense, I tried to put 

!loginManager.getLoginInfo(it.username).lastLoginTime

outside the  condition 

if (lastLoginMillis?.isNumber())  

 I dont't have any output 

Hana Kučerová
Community Champion
March 26, 2021

Hi @Firas hammami ,

please, what do you mean by you don't have any output?

What I was trying to say is that you need to deactivate two different groups of users.

How does your code looks like now?

Thank you.

Suggest an answer

Log in or Sign up to answer