[Script Runner] Last Login Date

Maciej Olszewski November 21, 2016

Hello,

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

Cheers,
Maciej O. 

5 answers

1 accepted

2 votes
Answer accepted
Daniel Bajrak
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 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

5 votes
Krupasindhu Nayak July 15, 2019

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import java.text.SimpleDateFormat;
import java.util.Date;

def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)
def users=ComponentAccessor.getUserUtil().getUsers()
StringBuilder builder=new StringBuilder()
builder.append("<table border = 1><tr><td><b>User Name</b></td><td><b>Full Name</b></td><td><b>eMail Address</b></td><td><b>Last Login</b></td><td><b>Status</b></td></tr>")
users.each{
Long lastLoginTime = loginManager.getLoginInfo(it.username).getLastLoginTime()
String activeStatus=it.active
if(activeStatus=="false")
builder.append("<tr><td>"+it.username+"</td><td>"+it.displayName+"</td><td>"+it.emailAddress+"</td><td>Inactive User</td><td>"+it.active+"</td></tr>")
else if(lastLoginTime==null)
builder.append("<tr><td>"+it.username+"</td><td>"+it.displayName+"</td><td>"+it.emailAddress+"</td><td>Logon not found</td><td>"+it.active+"</td></tr>")
else{
Date date=new Date(lastLoginTime);
SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy hh:mm");
String dateText = df2.format(date);
builder.append("<tr><td>"+it.username+"</td><td>"+it.displayName+"</td><td>"+it.emailAddress+"</td><td>"+dateText+"</td><td>"+it.active+"</td></tr>")
}

}
builder.append("</table>")
return builder

sanjay July 30, 2020

Hi Krupasindhu Nayak,

You have done a great job best script for the requirement.

Thanks for your help

Murthy Dusanapudi June 14, 2021

Hi @Krupasindhu Nayak can anyone please help me with the script for getting the list of users in Confluence,  Name, Email Address, and active and in active users list and created , last updated date in a single script? is that possible to get all these details in a single script? please help me in this thank you in advance..

 

The Exact scenario in confluence is needed  

0 votes
L Chaptal March 27, 2019

I've got an error with Jira 7.12 in

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

0 votes
Maciej Olszewski November 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 = "&lt;table style=\"width:100%\"&gt;&lt;tr&gt;&lt;th&gt;name&lt;/th&gt;&lt;th&gt;display name&lt;/th&gt;&lt;th&gt;email&lt;/th&gt;&lt;th&gt;directory&lt;/th&gt;&lt;th&gt;last login&lt;/th&gt;&lt;/tr&gt;"
userUtil.getUsers().findAll{user -&gt; user.isActive()}.each{ user -&gt;
    	Long lastLoginTime = getLoginInfo(user.name).getLastLoginTime()
    	result += "&lt;tr&gt;&lt;th&gt;" + user.name + "&lt;/th&gt;" + "&lt;th&gt;" + user.displayName + "&lt;/th&gt;" + "&lt;th&gt;" + user.emailAddress + "&lt;/th&gt;" + "&lt;th&gt;" + user.getDirectoryId() + "&lt;/th&gt;" 
        result += "&lt;th&gt;" + lastLogin + "&lt;/th&gt;&lt;/tr&gt;"
}
result += "&lt;/table&gt;"
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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 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 November 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
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2016

Suggest an answer

Log in or Sign up to answer