Guest User

RemoveSubscriptionsForInactiveUsers

a guest
Sep 4th, 2013
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.57 KB | None | 0 0
  1. import com.atlassian.crowd.embedded.api.User
  2. import com.atlassian.jira.ComponentManager
  3. import com.atlassian.jira.component.ComponentAccessor
  4. import com.atlassian.jira.issue.search.managers.SearchHandlerManager
  5. import com.atlassian.jira.issue.subscription.SubscriptionManager
  6. import org.ofbiz.core.entity.GenericValue
  7.  
  8. class RemoveSubscriptionsForInactiveUsersClass {
  9.     def String doRun() {
  10.         StringBuffer report = new StringBuffer()
  11.         //Remove subscription created by users who have been inactivated
  12.         SubscriptionManager manager = ComponentAccessor.getSubscriptionManager()
  13.  
  14.         //Upload all the subscriptions within a set (no double on the username)
  15.         Set<String> users = new TreeSet<String>()
  16.         manager.getAllSubscriptions().each { GenericValue subscription ->
  17.             String uname = subscription.getString("username")
  18.             if (uname) {
  19.                 users.add(uname)
  20.             }
  21.         }
  22.  
  23.         //And compare the user list with the user database. If one of them is inactivated, them remove the subscription(s)
  24.         users.each { String username ->
  25.             report.append "${user.name} have subscription(s), check the user status\n"
  26.             User user = ComponentAccessor.getUserManager().getUser(username)
  27.             if (!user.active) {
  28.                 report.append "Remove subscription(s) for the inactivated user ${user.displayName} (Username: ${user.name})"
  29.                 manager.deleteSubscriptionsForUser(user)
  30.             }
  31.  
  32.         }
  33.     }
  34. }
  35.  
  36. new RemoveSubscriptionsForInactiveUsersClass().doRun()
Advertisement
Add Comment
Please, Sign In to add comment