|
| 1 | +/** |
| 2 | + In case that there's an additional group that controls access to certain projects, |
| 3 | + it can be useful to search for users who are only members of one group, but should |
| 4 | + also have access to the second group. |
| 5 | +*/ |
| 6 | + |
| 7 | +import com.atlassian.crowd.manager.directory.DirectoryManager |
| 8 | +import com.atlassian.jira.bc.JiraServiceContextImpl |
| 9 | +import com.atlassian.jira.bc.user.UserService |
| 10 | +import com.atlassian.jira.bc.user.search.UserSearchParams |
| 11 | +import com.atlassian.jira.bc.user.search.UserSearchService |
| 12 | +import com.atlassian.jira.security.groups.GroupManager |
| 13 | +import com.atlassian.jira.component.ComponentAccessor |
| 14 | +import com.atlassian.jira.ComponentManager |
| 15 | +import com.atlassian.jira.security.login.LoginManager |
| 16 | +import com.atlassian.jira.user.ApplicationUser |
| 17 | + |
| 18 | +// Either it can be Internal JIRA or one managed by Active Directory |
| 19 | +final String directoryToCheck = "Active Directory server" |
| 20 | + |
| 21 | +def loginManager = ComponentAccessor.getComponent(LoginManager) |
| 22 | +def directoryManager = ComponentAccessor.getComponent(DirectoryManager) |
| 23 | +GroupManager groupManager = ComponentManager.getComponentInstanceOfType(GroupManager.class) |
| 24 | + |
| 25 | +UserSearchParams.Builder paramBuilder = UserSearchParams.builder() |
| 26 | + .allowEmptyQuery(true) |
| 27 | + .includeActive(true) |
| 28 | + .includeInactive(false) |
| 29 | + |
| 30 | +JiraServiceContextImpl jiraServiceContext = new JiraServiceContextImpl(ComponentAccessor.jiraAuthenticationContext.loggedInUser) |
| 31 | + |
| 32 | +def trusted = groupManager.getGroup("second-level-group") |
| 33 | + |
| 34 | +def allActiveUsers = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "", paramBuilder.build()) |
| 35 | +def directoryId = directoryManager.findAllDirectories()?.find { it.name.toLowerCase() == directoryToCheck.toLowerCase() }?.id |
| 36 | + |
| 37 | +def idleUsers = allActiveUsers.findAll { user -> |
| 38 | + user.directoryId == directoryId && loginManager.getLoginInfo(user.username)?.lastLoginTime && groupManager.getGroupNamesForUser(user).contains("jira-software-users") && groupManager.getGroupNamesForUser(user).size() == 1 |
| 39 | +} |
0 commit comments