Greetings Everyone!
I am using Conf DC 7.13.7 and just installed scriptrunner. I was given a groovy Script template by adaptavist. I have tried modifying and running. It runs fully then nothing happens.
I was supposed to get an email with the List of Deactivated users who have not logged in 90+ days and also deactivate them in Confluence. I have added the Script below.
ANY HELP WILL GREATLY BE APPRECIATED.
//MY SCRIPT
I can't imagine why this script would work. It seems to combine elements from Jira and Confluence API.
Here is a version that should work (provided you don't have too many users (otherwise it might either run very long or crash your instance with an out-of-memory or long GC).
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.confluence.security.login.LoginManager
import com.atlassian.confluence.user.ConfluenceUser
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.mail.Email
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.GroupManager
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)
}
sendEmailAboutRemovedUsers(deactivated*.name)
}
def sendEmailAboutRemovedUsers(List<String> userIds) {
def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.defaultSMTPMailServer
Email email = new Email("john.doe@company.net")
email.setFromName("TEST Plugin")
email.setSubject("Deactivated Users")
email.setBody("Users deactivated: " + userIds.join(", "))
email.setMimeType("text/html")
mailServer.send(email)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.