Groovy script to inactive users who are not logged in to Jira for past 90 days

I have code to inactive users who are not logged in to Jira for past 90 days. It is working fine in Jira 6.4 version.

Now, we upgraded our Jira to 7.2.9 version. The script what we have now is not compatible with 7 version.

Can someone provide me the resepective classes and method which can be replaced in the below 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
import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;

Logger log = log
log.setLevel(Level.DEBUG)

int numOfDays = 90 // Number of days the user was not logged in
Date dateLimit = (new Date())- numOfDays

UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService.UpdateUserValidationResult updateUserValidationResult

// Important:
// Most of this code will just run in the ScriptRunner Console as-is
// but will not work when done as a Service or as a post-function.
// The reason is that in the case of the former, the currently logged
// on User provides the AuthenticationContext. In the case of the latter
// there is no ‘User’ per se who is running the code, and so we cannot
// deactivate anybody because there is no privilege that the Script
// has to do so. So we explicitly set the AuthenticationContext.

def authenticationContextUser = userUtil.getUserByName(“V116406”);
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(authenticationContextUser);

long count = 0
def sb = new StringBuffer();

sb.append(“Logon cutoff time stamp: " + dateLimit + “\n”);
sb.append(”-----------------------------------------\n\n");

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)) {
sb.append(“Found a candidate: " + user.getName()+”\t " + d.toString()+"\t " + user.getDisplayName().trim()+"\t " + user.getEmailAddress()+"\n");
updateUser = ImmutableUser.newUser(user).active(false).toUser()
updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid()) {
userService.updateUser(updateUserValidationResult)
sb.append("=====> Deactivated: "+ user.getName() + “\n”);
count++;
} else {
sb.append(“Could not deactivate :frowning: " + user.getName() + “. Errors: " + updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(’,’)+”\n”);
}
}
}
}

sb.append("-----------------------------------------\n");
sb.append("${count} users were deactivated.\n");
sb.append("-----------------------------------------");

// Set up Mail Server
MailServerManager mailServerManager = ComponentAccessor.getMailServerManager();
SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer();

    // If the mail server configuration is valid, send an email.
    if (mailServer) {
        Email email = new Email("lorraine.wildman@verizon.com"); 
        email.setSubject("User Deactivation ["+ numOfDays +" day threshold] Summary (" + new Date().toString()+")"); // Set the SUBJECT
        email.setFrom("No-Reply DevOps User Deactivation <devops-userDeactivation@verizon.com>"); // Set the FROM address
		// Just put the vzids of folks to cc here
        email.setCc(userUtil.getUserByName("V648049").getEmailAddress());
        String content = """\

Hello,

Below is the information for the deactivation service run for today.

Thank You for your support.

DevOps Engineering Team

“”";
content += sb.toString();

        email.setBody(content)
        mailServer.send(email)
    }
    else {
        log.error "No SMTP mail server defined.";
    }

log.debug “User Deactivation Service finished running.”

Is this script compatible with JIRA in Cloud ?

Cheers

See https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users