Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Automation of User Disabling

I am looking to setup an automated process to disable users who have not signed in for X days for Confluence similar to this JIRA script:

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

 

I seem have a functioning script, however it does not provide any output like the JIRA version does. Has anyone successfully got an output that can be used for audit? It will only show "null" after it finishes running.

Right now I would have to view the log while running via the scriptrunner console. 

As of now I am running 6.13.x Confluence Server.

Thanks in advance. 

 


import com.atlassian.confluence.security.PermissionManager
import com.atlassian.confluence.security.login.LoginManager
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.UserManager
import groovy.time.TimeCategory
import org.apache.log4j.Level
import org.apache.log4j.Logger

def myLog = Logger.getLogger("com.onresolve.jira.groovy")
myLog.setLevel(Level.DEBUG)

def userManager = ComponentLocator.getComponent(UserManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)
def loginManager = ComponentLocator.getComponent(LoginManager)
def permissionManager = ComponentLocator.getComponent(PermissionManager)

def threeMonthsBack
use(TimeCategory) {
threeMonthsBack = 3.months.ago
}
def users = userManager.users

def page = users.currentPage

while (true) {

page.each { user ->
myLog.debug(user)
if (userAccessor.isDeactivated(user)) {
return
}


def userLoginInfo = loginManager.getLoginInfo(user)
def lastSuccessfulLoginDate = userLoginInfo?.lastSuccessfulLoginDate

def toDeactivate = ! lastSuccessfulLoginDate || lastSuccessfulLoginDate < threeMonthsBack
if (toDeactivate) {
log.warn "Found user: ${user.name} with last login date ${lastSuccessfulLoginDate}, will deactivate"


if (!permissionManager.isSystemAdministrator(user)) {
log.info "Deactivating user.."
userAccessor.deactivateUser(user)
}
}
}

if (users.onLastPage()) {
return
}
else {
users.nextPage()
}
}

 

1 answer

Hi Mike,

when you run a script in the console, it outputs the result („return value“) of the last line in your script. The simplest way to get output, is create an instance of java.io.StringWriter an write to that StringWriter. At the end of the script simply write „writer.toString()“ and the console will show you the output. Note that output is expected als HTML so before you output anything, I recommend issuing a „<pre>“ (for preformatted text) and closing it at the last line („</pre>“).

 

import java.io.PrintWriter
import java.io.StringWriter

writer = new StringWriter()
printer = new PrintWriter(writer)

...

printer.println(“<pre>“) // first line of output
...
printer.println(“Deactivating User: ...“)
...

printer.println(“</pre>“) // last line of output

writer.toString() // last line in the script

Note that the output will not be printed „live“. It will appear, after the script finished so it can take a long time to see something, if you have a lot of users / long running tasks and you may run into a timeout.

HTH

Kurt

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket Pipelines

Bitbucket Pipelines Runners is now in open beta

We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...

451 views 10 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you