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
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()) {
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.