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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Is there a way to automate deactivation of accounts based on "last login" time in JIRA?

We just connected jira to crowd and in the process of getting evertyhing cleaned up I have noticed that we have a bunch JIRA internal accounts that we can't delete. I want to deactivate them so they don't count against our license.

6 answers

1 accepted

2 votes
Answer accepted

You could implement a Script Runner service which

  1. searches for all active users where the last login is more than x days old and
  2. deactivate these users.

With the following code you'll find all users which didn't login for one year and deactivate these users.

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.User
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.util.UserUtil
import org.apache.log4j.Level
import org.apache.log4j.Logger

Logger log = log
log.setLevel(Level.INFO)

int numOfDays = 365 // 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.class)
User updateUser
UserService.UpdateUserValidationResult updateUserValidationResult

long count = 0

userUtil.getUsers().findAll{it.isActive()}.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)) {
            updateUser = 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"

Be aware that there may be users which didn't login at all, e.g. if you use users to "emulate" teams or for automated tasks. These users have to be filtered by your code.

Henning

There's no easy way for this. You could implement a plugin using JIRA API in Java or maybe with REST API but it's probably more effort than you'll gain out of it.

You can write a scheduled SQL task that runs the query

UPDATE U 
SET U.active = 'F' 
FROM dbo.user AS U
INNER JOIN dbo.cwd_user_attribute AS A 
       ON R.id = A.user_id 
WHERE A.lastAuthenticated < '<your required datetime in unix time format>'

I have a script that moves them into a seperate jira group called jira-leavers using the API. This group has no access to JIRA but it means you're not tinkering with the database where very bad things could happen.

Hello @Greg Warner ,

Can you please share the code.

 

Regards,

Abhi

The script by

Ugh.. Sorry for the incomplete post. The script buy Henning worked great. It did not deactivate users with "Not recorded" in their login details. If you have a bunch of automated users this isn't a bad thing.

Suggest an answer

Log in or Sign up to answer
TAGS

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