Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to get user last login details without connecting to database ?

Karthick Kumar M
January 4, 2024

I am using below script in scriptrunner to get a list of user details who are part of a group, similarly tried to get last login details along with user details. But it looks like it is not possible to get last login details without connecting to database. Is there any method to get last login details without connecting to DB?

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.Group
import com.atlassian.user.GroupManager

GroupManager groupManager = ComponentLocator.getComponent(GroupManager)
Group group = groupManager.getGroup("confluence-users")

final StringBuilder stringBuilder = new StringBuilder()
final String LF = "<br>"

for (String username : groupManager.getMemberNames(group)) {
stringBuilder.append(username).append(LF)
}

return stringBuilder.toString()

1 answer

1 vote
Radek Dostál
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 Champions.
January 5, 2024

I think that's where LoginManager comes in: https://docs.atlassian.com/software/jira/docs/api/9.4.11/com/atlassian/jira/security/login/LoginManager.html

 

Example:

import com.atlassian.jira.bc.security.login.LoginInfo
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

UserManager userManager = ComponentAccessor.getUserManager()
LoginManager loginManager = ComponentAccessor.getComponent(LoginManager)

ApplicationUser applicationUser = userManager.getUserByName("xxx")
if (applicationUser == null)
return "User not found."

LoginInfo loginInfo = loginManager.getLoginInfo(applicationUser.getUsername());
return loginInfo.getLastLoginTime();

 

And LoginInfo has a lot of stuff you can get about the login details: https://docs.atlassian.com/software/jira/docs/api/9.4.11/com/atlassian/jira/bc/security/login/LoginInfo.html

 

Edit: then noticed the question was tagged as Jira but the snippet was Confluence, but it is basically the same there - just different packages but there is also LoginManager and LoginInfo so you get to it in the same way.

Suggest an answer

Log in or Sign up to answer