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

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

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

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. 

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 Apr 09, 2019 in Portfolio for Jira

Portfolio for Jira 3.0 is here!

The wait is over... Portfolio for Jira Server and Data Center 3.0 is now officially here! Platform releases offer Atlassian an opportunity to shift our strategy, make bold predictions about t...

1,466 views 14 26
Read article

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