Groovy script that lists currently authenticated (logged in right now) users.

Brian Peters May 25, 2022

Hi,

I'm looking for a groovy script that will give me a list of users that are currently logged in to Jira.  In the Scriptrunner console, I can get a list of all users or users that have never logged in but I don't see a way to get just users that are logged in right now ("right now" = when I run the script in the console).

Thanks in advance for any help.

- B

1 answer

1 accepted

2 votes
Answer accepted
Djiga Sene May 26, 2022

Hi,

you can retrieve all application users.

And getting the last login time of user compares it with a delay (like 1 hour ago).

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.security.login.LoginManager
import groovy.time.TimeCategory

def twoHoursAg
use(TimeCategory) {
twoHoursAgo = 1.hours.ago
}

UserManager userManager = ComponentAccessor.getUserManager()
LoginManager loginManager = ComponentAccessor.getComponent(LoginManager)
userManager.users.findAll{loginManager.getLoginInfo(it.username)?.lastLoginTime!=null && new Date(loginManager.getLoginInfo(it.username)?.lastLoginTime) >= twoHoursAgo}
Brian Peters May 31, 2022

Great! This will work well enough for my purpose.  Thank you!

Like Djiga Sene likes this

Suggest an answer

Log in or Sign up to answer