All users not login in within last 6 month should marked as inactive.

I have logged into JIRA with admin rights. I have written a code to deactivate the users who are not logged in from last 6 months. But i see that users who are part of Internal Directory are marked as in active but users from Active directory are not. So how can i make my code works for both the directory users.

Below is my code.

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil

int numOfDays = 8
Date dateLimit = (new Date())- numOfDays

StringBuffer st = new StringBuffer();
UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService userService = ComponentAccessor.getComponent(UserService)
ApplicationUser updateUser
UserService.UpdateUserValidationResult updateUserValidationResult

long count = 0
userUtil.getUsers().findAll{it.isActive()}.each {
UserWithAttributes user = crowdService.getUserWithAttributes(it.getName())
String lastLoginMillis = user.getValue('login.lastLoginMillis')

if (lastLoginMillis?.isNumber()) {
Date d = new Date(Long.parseLong(lastLoginMillis))
st.append("date is " +d);
if (d.before(dateLimit)) {

updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid()) {
userService.updateUser(updateUserValidationResult)
st.append("Final result of Updated Users are::: " +updateUser.name)
log.info "Deactivated ${updateUser.name}"
count++
} else {
log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
}
}
}
}


"${count} users deactivated.\n"
st.toString();

 

1 answer

1 accepted

0 vote
Daniel Eads Community Champion Sep 20, 2017

Hi Prasanna,

In most setups, Active Directory users cannot be modified in JIRA. If you're using the Microsoft Active Directory user directory type, all users meeting a specific criteria in AD (usually filtered by OU or specific group membership) are synced into JIRA.

From the User Directories page, check and see what setup you have. If it's "Microsoft Active Directory (Read Only, with Local Groups)" for your AD, you can't modify the users in JIRA.

What you want to do requires you to be using the "Internal with LDAP Authentication" directory type. This directory can be more of a pain to manage than the standard AD directory type, but allows you to manage user properties (like active/inactive) within JIRA.

If you're unable to look into changing directory types to meet your requirements (it can be a big job to move them), I might suggest using an AD group as your filter criteria in your Microsoft Active Directory user directory type. When you remove users from what it's looking for to sync, they will become inactive in JIRA. You can use the script you've got going or some SQL to generate the inactive users list, then use a PowerShell script to remove them from whatever AD group you have / can set up to control JIRA access.

Cheers,
Daniel

P.S. If you feel like this answer helped, please hit the checkmark to the left to mark it as answered. This helps us focus our efforts on unsolved questions. Thanks!

Hi Daniel Thank for the reply. 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

56 views 0 5
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