Heads up! On March 5, starting at 4:30 PM Central Time, our community will be undergoing scheduled maintenance for a few hours. During this time, you will find the site temporarily inaccessible. Thanks for your patience. Read more.
×Hi!
I'm trying to retrieve users who's account is active, but who either haven't logged in for 90 days, or who's never logged in.
With the script below i get users as expected based on login times/no login, but it also returns users who's been deactivated.
I think I can use the com.atlassian.jira.user.ApplicationUser property called "isActive", but I'm not sure how to correctly add it in the script.
Any help is much appreciated!
Hi @Jakob KN
We have a similar script in the ScriptRunner Example Scripts page.
You will need to modify the code to suite your requirements.
Thank you and Kind regards,
Ram
Hi Ram, thanks for your reply!
I ended up using something slightly similar, returning users based on group membership and simply including all groups with application access.
It includes Username, Full Name, Email Address, Last login and active status. Then returns it in a table that can be easily copied to excel or google sheets.
This resolved both my initial question (since we can sort by login date in a sheet), and my question in the comment below.
Adding it below in case someone comes across this post, and finds it useful in the future.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import java.text.SimpleDateFormat;
import java.util.Date;
import com.atlassian.jira.user.util.UserUtil
UserUtil userUtil = ComponentAccessor.getUserUtil()
def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)
//def users=ComponentAccessor.UserManager.getAllApplicationUsers()
def groupManager = ComponentAccessor.getGroupManager()
def adminGroup = groupManager.getUsersInGroup('jira-administrators')
def sysadminGroup = groupManager.getUsersInGroup('system-administrators')
def softwareGroup = groupManager.getUsersInGroup('jira-users')
def extGroup = groupManager.getUsersInGroup('External')
def addonGroup = groupManager.getUsersInGroup('atlassian-addons')
def servGroup = groupManager.getUsersInGroup('Service Account')
def intGroup = groupManager.getUsersInGroup('Integration Users')
def auditGroup = groupManager.getUsersInGroup('External Auditors')
def extsupGroup = groupManager.getUsersInGroup('External Support')//def serviceDeskGroup = groupManager.getUsersInGroup('jira-servicedesk-users')
def users = adminGroup + sysadminGroup + softwareGroup + extGroup + addonGroup + servGroup + intGroup + auditGroup + extsupGroup
users.unique()
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(userUtil.getGroupsForUser(it.getName()).size() == 0)
//builder.append("<tr><td>"+it.username+"</td><td>"+it.displayName+"</td><td>"+it.emailAddress+"</td><td>No Group added</td><td>"+it.active+"</td></tr>")
return
else 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for providing yours, I built off of it and wanted to share.
This includes only active users, last active date limited to 60d, and created date.
I wanted to find users idle for more than 60 days or never logged in, but wanted to make sure they weren't recently created first.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.model.user.TimestampedUser
import java.text.SimpleDateFormat// Get required components
def userUtil = ComponentAccessor.getUserUtil()
def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)
def groupManager = ComponentAccessor.getGroupManager()
def crowdService = ComponentLocator.getComponent(CrowdService)// Define the groups to pull users from
def adminGroup = groupManager.getUsersInGroup('jira-administrators')
def sysadminGroup = groupManager.getUsersInGroup('system-administrators')
def softwareGroup = groupManager.getUsersInGroup('jira-users')
def extGroup = groupManager.getUsersInGroup('External')
def addonGroup = groupManager.getUsersInGroup('atlassian-addons')
def servGroup = groupManager.getUsersInGroup('Service Account')
def intGroup = groupManager.getUsersInGroup('Integration Users')
def auditGroup = groupManager.getUsersInGroup('External Auditors')
def extsupGroup = groupManager.getUsersInGroup('External Support')// Combine groups into a unique list of users
def users = (adminGroup + sysadminGroup + softwareGroup + extGroup + addonGroup + servGroup + intGroup + auditGroup + extsupGroup).unique()// Define a threshold: only include users whose last activity (last login or creation) is more than XX days old.
Date threshold = new Date() - 60// Set up a date formatter for display
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yy hh:mm")// Build the HTML output with embedded JavaScript
StringBuilder builder = new StringBuilder()builder.append("""
<html>
<head>
<meta charset="UTF-8">
<title>Inactive Users</title>
<style>
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px; border: 1px solid #ccc; text-align: left; }
th { background-color: #eee; }
button { margin-top: 10px; }
</style>
</head>
<body>
<div id="tableContainer">
<table id="userTable">
<tr>
<th>User Name</th>
<th>Full Name</th>
<th>Email Address</th>
<th>Created Date</th>
<th>Last Activity</th>
</tr>
""")users.each { user ->
// Only consider active users
if (!user.active) {
return
}// Retrieve last login info (may be null if the user never logged in)
def loginInfo = loginManager.getLoginInfo(user.username)
Long lastLoginTime = loginInfo ? loginInfo.getLastLoginTime() : null
Date lastLoginDate = lastLoginTime ? new Date(lastLoginTime) : null// Use CrowdService to capture the created date
def crowdUser = crowdService.getUser(user.username)
Date creationDate = (crowdUser instanceof TimestampedUser) ? crowdUser.getCreatedDate() : null// Determine the "last activity" date: if last login exists, use it; otherwise, use the created date
Date lastActivityDate = lastLoginDate ?: creationDate// Only include users whose last activity is more than 60 days old.
if (!lastActivityDate || lastActivityDate > threshold) {
return
}String createdText = creationDate ? df.format(creationDate) : "N/A"
String lastActivityText = lastLoginDate ? df.format(lastLoginDate) : "Never Logged In"builder.append("<tr>")
builder.append("<td>${user.username}</td>")
builder.append("<td>${user.displayName}</td>")
builder.append("<td>${user.emailAddress}</td>")
builder.append("<td>${createdText}</td>")
builder.append("<td>${lastActivityText}</td>")
builder.append("</tr>")
}return builder.toString()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In addition, but similar, to the above.
I'd also like to extract all users with their usernames, emails and last login date.
I'm assuming it'll be a script similar to the above.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.