How to get inactive users of group and remove them from group with Scriptrunner for Confluence?
Asked by Stefan Salzl:

Hi Community,

I?m implementing an automatic cleanup for user groups. This worked pretty fine for Jira but unfortunately Confluence seems really challenging.

Does anyone know why there are so many confusing (at least from my perspective) classes, why nearly same classes work so differently or have different/little methods? furhtermore the returned types are kinda frustrating.

so in Jira I got the following lines working like a charm:

```
def inactiveUsersInGroup = groupManager.getUsersInGroup(userGroup)?.findAll { it.active == false }

Groups.getByName(userGroup).remove(user)
```

For Confluence:

* There is no method "getUsersInGroup"
* furthermore there is no direct property "active" within the user
* Furthermore what are the differences between "ConfluenceUser" and "User" ? Is there any possibility to get list of Users I can then access and filter?

Any help much appreciated.

Best  
Stefan


Answers:
author: Sayed Bares [ServiceRocket]
body: [@Stefan Salzl](/t5/user/viewprofilepage/user-id/4616993) You can look into <https://library.adaptavist.com/entity/check-active-user-count-remove-oldest-listener?tab=dataCenter> as an example.

You can use [getMembers](https://docs.atlassian.com/ConfluenceServer/javadoc/7.10.1/com/atlassian/confluence/user/UserAccessor.html#getMembers-com.atlassian.user.Group-) method to get members and then [isDeactivated](https://docs.atlassian.com/ConfluenceServer/javadoc/7.10.1/bucket/user/UserAccessor.html#isDeactivated-com.atlassian.user.User-) to check if they are active.

comments: author: Stefan Salzl
body: Hi [@Sayed Bares \[ServiceRocket\]](/t5/user/viewprofilepage/user-id/1480984)

Thanks for your input. The annoying thing is I can?t filter/find inactive users from a group immediately:

This doesn?t work:  

```
def inactiveUsersInGroup = userAccessor.getMembers(groupManager.getGroup(userGroup)).findAll { it.isDeactivated == true }
```

as the .getMembers() results

```
Iterable<ConfluenceUser>
```

So is there no propper solution to get a list of users?

Furthermore some of my questions still open:

The UserAccessor (mostly) delivers "ConcluenceUser" (class: ConfluenceUserImpl) which then has the following property:

```
backingUser:EmbeddedCrowdUser{name='xxx', displayName='xxx', directoryId=nnnn}
```

The method "isDeactivated()" doesn?t work on a User resulting from UserAccessor.getUser()

When I use CrowdService.getUser() it results a User of class class com.atlassian.confluence.impl.user.crowd.Cached

It also delivers a property "active".

I wanted to use the CrowdService to collect all users (in some earlier steps I need to collect all users that come from a dedicated AD-directory, so the "directory" property is needed too). But I struggled with the findUsers() and the query :(

So there is no way to do such an easy way like in Jira to do a search for users that can be filterd with .findAll{} and results in a "useful" list of users?

Thanks in Advance

Best  
Stefan


author: Sayed Bares [ServiceRocket]
body: To check the if the user is active you would need to use **userAccessor.isDeactivated(user)**

```
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

?

def groupManager = ComponentLocator.getComponent(GroupManager)
def loginManager = ComponentLocator.getComponent(LoginManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)

def confluence_user = groupManager.getGroup("confluence-users")
def users = userAccessor.getMembers(confluence_user).each{it -> log.warn(userAccessor.isDeactivated(it))}
```


Tags: adaptavist

automation

confluence-server

data-center

group

script

scriptrunner

server

user