[Script Runner] Last Login Date

Hello,

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

Cheers,
Maciej O. 

3 answers

1 accepted

This widget could not be displayed.

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

This widget could not be displayed.
Vasiliy Zverev Community Champion Nov 21, 2016
This widget could not be displayed.

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!

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

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. 

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 06, 2018 in Jira Service Desk

A is for Activate: Share your top Jira Service Desk onboarding tips for new users!

Hi, everyone! Molly here from the Jira Service Desk Product Marketing Team :).  In the spirit of this month's  august-challenge, we're sourcing stories of Jira Service Desk activation fro...

536 views 23 15
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you