[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

1 vote
Accepted answer

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. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

1,086 views 11 10
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