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] Last Login Date

Maciej Olszewski Nov 21, 2016

Hello,

How can i get information about user last login date via Script Runner?

Cheers,
Maciej O. 

4 answers

1 accepted

1 vote
Answer accepted
Daniel Bajrak Nov 21, 2016

Hi Maciej,

have you tried to use LoginManager and the method getLoginInfo(String userName).getLastLoginTime()? It returns date as millisecond in long.

I hope I've helped you,

Best Regards,

P.S.

Pozdrowienia z Wrocławia

0 votes
Maciej Olszewski Nov 22, 2016

I tried to use it and my script looks like that 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.core.util.StaticCrowdServiceFactory
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.login.LoginManager
import com.atlassian.jira.security.login.LoginInfo
import com.atlassian.jira.user.util.UserManager
import java.sql.Timestamp
import com.atlassian.jira.bc.security.login.LoginService

UserUtil userUtil = ComponentAccessor.getUserUtil()
CrowdService crowdService = StaticCrowdServiceFactory.getCrowdService()
result = "<table style=\"width:100%\"><tr><th>name</th><th>display name</th><th>email</th><th>directory</th><th>last login</th></tr>"
userUtil.getUsers().findAll{user -> user.isActive()}.each{ user ->
    	Long lastLoginTime = getLoginInfo(user.name).getLastLoginTime()
    	result += "<tr><th>" + user.name + "</th>" + "<th>" + user.displayName + "</th>" + "<th>" + user.emailAddress + "</th>" + "<th>" + user.getDirectoryId() + "</th>" 
        result += "<th>" + lastLogin + "</th></tr>"
}
result += "</table>"
result

Can you tell me where is mistake? <error is in 

Long lastLoginTime = getLoginInfo(user.name).getLastLoginTime()

Thanks in Advance
Maciej O.
PS: Wrocław jest piękny!
Daniel Bajrak Nov 22, 2016

You need to use LoginManager. Define it before "each" loop:

def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)

and use it inside foreach in this way:

Long lastLoginTime = loginManager.getLoginInfo(user.name).getLastLoginTime()

you need to convert this long number to format of date.

 

Best Regards

Maciej Olszewski Nov 23, 2016

Thanks laugh , one more question:
What exactly gives me loginManager.getLoginInfo(user.name).getLastLoginTime()
I get numbers like "1456303984535" is it miliseconds since last login?

Cheers,
Maciej O. 

0 votes
Laurie02* Mar 27, 2019

I've got an error with Jira 7.12 in

import com.atlassian.jira.security.login.LoginInfo

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

12,630 views 63 59
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