Deactivate inactive users in Confluence

Overview

Maintain your Confluence instance by deactivating users based on the time since they last logged in.

Example

As an administrator, I have the responsibility of maintaining an accurate and up-to-date list of who is using the Atlassian tools, to avoid paying for users that are not getting value from the products. I want to deactivate all users that haven't logged in for the last three months. This script allows me to find all users that haven't logged in for the specified time, remove them from the confluence-users group, and add them to a deactivated user group, thus reducing my licensed user count.

Good to Know

  • In this example, we consider both successful and failed login attempts, though you can adjust it to match your use case.

Requirements

Confluence Confluence (6.6 - 7.10)

    
import com.atlassian.confluence.security.login.LoginManager import com.atlassian.confluence.user.UserAccessor import com.atlassian.sal.api.component.ComponentLocator import com.atlassian.user.GroupManager import java.time.LocalDateTime import java.time.ZoneId def groupManager = ComponentLocator.getComponent(GroupManager) def loginManager = ComponentLocator.getComponent(LoginManager) def userAccessor = ComponentLocator.getComponent(UserAccessor) def inactiveGroup = groupManager.getGroup("Deactivated users") ?: groupManager.createGroup("Deactivated users") def confluenceUsersGroup = groupManager.getGroup('confluence-users') def users = groupManager.getMemberNames(confluenceUsersGroup) users.each { userName -> def user = userAccessor.getUserByName(userName) if (user) { log.info "Inspecting $user.name to determine whether user should be deactivated" def loginInfo = loginManager.getLoginInfo(user) def lastSuccessfulDate = convertDate(loginInfo?.lastSuccessfulLoginDate) def lastFailedDate = convertDate(loginInfo?.lastFailedLoginDate) def threeMonthsAgo = LocalDateTime.now().minusMonths(3) if (lastFailedDate?.isBefore(threeMonthsAgo) && lastSuccessfulDate?.isBefore(threeMonthsAgo)) { log.info "User ${user.name} last successuflly logged in ${lastSuccessfulDate} and last failed to login on ${lastFailedDate}" log.info "Removing user ${user.name} from confluence-users" groupManager.removeMembership(confluenceUsersGroup, user) log.info "Add user ${user.name} to the group: 'Deactivated Users'" groupManager.addMembership(inactiveGroup, user) log.info "Deactivating user ${user.name}" userAccessor.deactivateUser(user) } } } /** * Note: In the latest versions of ScriptRunner, you can simply use http://docs.groovy-lang.org/docs/latest/html/groovy-jdk/java/util/Date.html#toLocalDateTime() to convert the Date object to a LocalDateTime, as we have upgraded to Groovy 2.5. This convenience method is here for backward compatibility only. * @param date * @return */ private static LocalDateTime convertDate(Date date) { date?.toInstant()?.atZone(ZoneId.systemDefault())?.toLocalDateTime() }
Discovered an issue? Report it here

Suggested for you