How to Automatically deactivate inactive JIRA users

I am trying Automatically deactivate inactive JIRA (7.1.8 ) users  by using code provided link at https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users 

Error

No signature of method: com.atlassian.jira.bc.user.DefaultUserService.validateUpdateUser() is applicable for argument types: (com.atlassian.crowd.embedded.impl.ImmutableUser) values: [com.atlassian.crowd.embedded.impl.ImmutableUser@bec3c60c] Possible solutions: validateUpdateUser(com.atlassian.jira.user.ApplicationUser), validateCreateUser(com.atlassian.jira.bc.user.UserService$CreateUserRequest)

 

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.bc.user.DefaultUserService.validateUpdateUser() is applicable for argument types: (com.atlassian.crowd.embedded.impl.ImmutableUser) values: [com.atlassian.crowd.embedded.impl.ImmutableUser@bec3c60c] Possible solutions: validateUpdateUser(com.atlassian.jira.user.ApplicationUser), validateCreateUser(com.atlassian.jira.bc.user.UserService$CreateUserRequest) at Script1$_run_closure2.doCall(Script1.groovy:32) at Script1.run(Script1.groovy:25)


Any idea what is missing  or wron in this code

 

import com.atlassian.crowd.embedded.api.CrowdService

 

import com.atlassian.crowd.embedded.api.User

 

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.util.UserUtil

 

import org.apache.log4j.Level

 

import org.apache.log4j.Logger

 

  

 

Logger log = log

 

log.setLevel(Level.INFO)

 

  

 

int numOfDays = 100 // Number of days the user was not logged in

 

Date dateLimit = (new Date())- numOfDays

 

  

 

UserUtil userUtil = ComponentAccessor.userUtil

 

CrowdService crowdService = ComponentAccessor.crowdService

 

UserService userService = ComponentAccessor.getComponent(UserService.class)

 

User 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))

 

        if (d.before(dateLimit)) {

 

            updateUser = ImmutableUser.newUser(user).active(false).toUser()
// this line causing error above

 

            updateUserValidationResult = userService.validateUpdateUser(updateUser)

 

            if (updateUserValidationResult.isValid()) {

 

                userService.updateUser(updateUserValidationResult)

 

                log.info "Deactivated ${updateUser.name}"

 

                count++

 

            } else {

 

                log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"

 

            }

 

        }

 

    }

 

}

 

"${count} users deactivated.\n"





1 answer

This widget could not be displayed.

Hi Muhammad

The above example is for JIRA v6, for JIRA v7 there are few changes, therefore the script comp for JIRA 7 should be

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 = 100 // Number of days the user was not logged in
Date dateLimit = (new Date())- numOfDays

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))
            if (d.before(dateLimit)) {
                updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
                updateUserValidationResult = userService.validateUpdateUser(updateUser)
                if (updateUserValidationResult.isValid()) {
                    userService.updateUser(updateUserValidationResult)
                    log.info "Deactivated ${updateUser.name}"
                    count++
                } else {
                    log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
                }
            }
        }
}

"${count} users deactivated.\n"

regards, Thanos

Thanos,

Thank you for this. Very helpful.

Was wondering if there is a way to expand this script to exclude certain accounts. We have service accounts used for notifications only. These accounts never actually log into JIRA. I am hoping that if we use a standard naming convention, preference the user name with SYS-, could this script be modified to exclude accounts that begin with SYS- ? We do not want these accounts deactivated.

 

Thank you

Jeanne

Hi Jeanne,

You can update the condition to be something like 

//if has not logged in for x days AND it's username does not start with the "SYS-" prefix 
if (d.before(dateLimit) && !it.username.startsWith("SYS-")) {
	//rest of the code that deactivates user
}

Silly question. But can you let me know how can i add Escalation service in script runner to perform this job daily as mentioned in the link:  https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users

I mean what should be the contents for: 

JQL Query:

Userkey:

cron expression:

additional issue action:

JQL query to select issues with. For example "issuetype = Task and updated < -7d"
Enter the key of the user that the service will run as
How often the service should run, in minutes. If not a number, CRON expression is expected
Optionally set an action...
 
1
 

Hi Utkarsh,

Probably you will need a ScriptRunner Service

Hi Utkarsh,

Apologies but the document you viewed was an old draft and incorrect. @Jamie Echlin (Adaptavist) alerted me of this discrepancy and I've since reviewed and updated it to reflect how to set this up as a JIRA Service.

I've also created an issue to update the ScriptRunner docs to include this script there too.

Regards, Mark

I am looking to add one more parameter to the script. For the JIRA 7 comp, how do I limit the users to those in the jira-users group? We have several accounts used for notification purposes only. These accounts are not in the jira-users group, thus preventing end-users from trying to log in with these accounts. The accounts will never have a "last login" but should not be deactivated. 

Hi. I want to use this script. 
But It doesn't work. 
I was login use admin id. And  Go Admin page -> Addon -> Script console.

And Run this code. It work.

 

But I go system-> Services and Run this code.

error like this

Error Messages: [You do not have the permission to update users.]

Help me ^^

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

526 views 3 19
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