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

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

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

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
TAGS
Community showcase
Published in Jira Software

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

1,597 views 10 16
Read article

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