Example scripts
To homepage
Confluence

Check Active User Count and Disable Oldest User
App in script

ScriptRunner For Confluence
by Adaptavist
Compatibility

Confluence (7.15 - 8.6)

ScriptRunner For Confluence (7.10.0)
Language |
groovy
package examples
import com.atlassian.confluence.security.login.LoginManager
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.user.GroupManager
import com.atlassian.confluence.user.ConfluenceUser
import com.onresolve.scriptrunner.parameters.annotation.GroupPicker
import com.onresolve.scriptrunner.parameters.annotation.NumberInput
@NumberInput(label = 'Maximum users', description = 'The maximum number of users to allow in Confluence')
Integer maximumUsers
@GroupPicker(label = 'User group', description = 'The group of users to check (e.g. "confluence-users")')
Group selectedUserGroup
@GroupPicker(label = 'Deactivated user group', description = 'The group to add the deactivated users to (e.g. "deactivated-users"')
Group selectedDeactivatedGroup
def groupManager = ComponentLocator.getComponent(GroupManager)
def loginManager = ComponentLocator.getComponent(LoginManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)
// Set defaults in case the fields are not filled out
maximumUsers = maximumUsers ?: 500
def userGroup = selectedUserGroup ? groupManager.getGroup(selectedUserGroup.name) : groupManager.getGroup('confluence-users')
def deactivatedGroup = selectedDeactivatedGroup ? groupManager.getGroup(selectedDeactivatedGroup.name) : groupManager.getGroup('deactivated-users') ?: groupManager.createGroup('deactivated-users')
// Get all users from the selected user group
def users = userAccessor.getMembers(userGroup).asList()
// For the users found in the selected group, sort the users by last login date
Map<ConfluenceUser, Date> userWithLoginDate =
users.collectEntries {
[(it): (loginManager.getLoginInfo(it)?.lastSuccessfulLoginDate)]
}.sort { it -> it.value }
// Get all user keys
def sortedUsers = userWithLoginDate*.key
// Deactivate user accounts and add them to the deactivated group if the user list size is more than the allowed amount
if (sortedUsers.size() > maximumUsers) {
def deactivated = sortedUsers.subList(0, sortedUsers.size() - maximumUsers)
deactivated.each { user ->
groupManager.removeMembership(userGroup, user)
groupManager.addMembership(deactivatedGroup, user)
userAccessor.deactivateUser(user)
}
}
Having an issue with this script?
Report it here