Script Runner make users inactive after no activity for specific period of time

Dawid June 30, 2020

Hi.

I was investigating couple of scripts from Script Runner but for Jira version 7 and higher I was not able to find proper example which is existing for previous jira version here:

https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users

I'm looking for solution which will fit to higher version of JIRA because the old solution is not working.

It Thoughts this error:

And recommended solution from Adaptavist points to this script:

https://library.adaptavist.com/entity/deactivate-idle-users

Which is just deactivating users who have never logged in, I would like to find same solution but for users who was not using Jira for last 100 days so was not logged in for more than 100 days.

Any one who could help with it?

Thank you in advance for your help!

1 answer

2 votes
Katy Kelly
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 30, 2020

Hi,

You need to add parameters and customisation to it if you would like it to do additional things. What you need to do is the following: 

You need to define a Date object to store the date object that represents 100 days before the script is run:

int numOfDays = 3 // Number of days the user was not logged inDate dateLimit = (new Date())- numOfDays

You then need to get the last login time from the user, which you can do with the crowd service class

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
 
CrowdService crowdService = ComponentAccessor.crowdService
 
// other code logic... 
UserWithAttributes userWithAtribs = crowdService.getUserWithAttributes(username)
String lastLoginMillis = userWithAtribs.getValue('login.lastLoginMillis')

 Now you have to compare the last login time to your dateLimit.

//check the last login time is actually a there and is a numberif (lastLoginMillis?.isNumber()) {
             
    Date d = new Date(Long.parseLong(lastLoginMillis))
    if(d.before(dateLimit)) {
 
      log.error("last login was $numOfDays or more ago days")
      //Inside here you can do whatever you need to do with the user who has not logged in within the set time period       
      //e.g. here would be where you run the deactivation logic for the user 
    }else{
      log.error("User has logged in in last $numOfDays days so will not deactivate")
    }
}
 

This script below can be used in the script console as a test to just log out a message if the user logged in in the last x days.

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
def userManager = ComponentAccessor.userManager
def userService = ComponentAccessor.getComponent(UserService)
 
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)
 
CrowdService crowdService = ComponentAccessor.crowdService
int numOfDays = 3 // Number of days the user was not logged inDate dateLimit = (new Date())- numOfDays
[
    'userOne','userTwo','userThree'
    // add more as required].each { username ->
    def user = userManager.getUserByName(username)
    if (!user) {
        log.warn "Failed to find user with name ${username}"
        return
    }
   
    UserWithAttributes userWithAtribs = crowdService.getUserWithAttributes(username)
    String lastLoginMillis = userWithAtribs.getValue('login.lastLoginMillis')
     
    log.error(lastLoginMillis)
     
        if (lastLoginMillis?.isNumber()) {
             
            Date d = new Date(Long.parseLong(lastLoginMillis))
            if(d.before(dateLimit)) {
                log.error("last login was $numOfDays or more ago days")
            }else{
                log.error("User has logged in in last $numOfDays days")
            }
     
        }
}

Kind regards,

Katy 

Suggest an answer

Log in or Sign up to answer