@Override
public List<Group> getAdministeredGroupsForUser(User user) {
// All UserDomains for User with UserRole.ROLE_ORG_ADMIN
JPASubQuery userDomainsOrgAdmin = subQuery().from(userDomain)
.where(userDomain.user.eq(user),
userDomain.role.authority.eq(UserRole.ROLE_ORG_ADMIN.toString()));
// All UserDomains for User with UserRole.ROLE_GROUP_ADMIN
JPASubQuery userDomainsGroupAdmin = subQuery().from(userDomain)
.where(userDomain.user.eq(user),
userDomain.role.authority.eq(UserRole.ROLE_GROUP_ADMIN.toString()));
// GroupAdmin domains
JPASubQuery groupAdminDomains = subQuery().from(domain)
.where(domain.in(
userDomainsGroupAdmin.list(userDomain.domain)
));
// All Organizations which the User is an admin
JPASubQuery adminOrganizations = subQuery().from(organization)
.where(organization.in(
subQuery().from(domain)
.where(domain.in(
userDomainsOrgAdmin.list(userDomain.domain))
).list(domain.as(organization.getClass()))
).and(getActiveOrganizationBooleanExpression(organization, user)));
// All groups which are part of an organization which the users is an admin of
JPASubQuery adminOrganizationGroups = subQuery().from(group)
.where(group.organization.in(
adminOrganizations.list(organization)
));
// Main Query - All Groups which the user is a direct admin (ROLE_GROUP_ADMIN) or through organizations which the user is an admin (ROLE_ORG_ADMIN)
JPAQuery mainQuery = cacheableQuery().from(group)
.where((group.in(
adminOrganizationGroups.list(group))
.or(group.in(groupAdminDomains.list(domain.as(group.getClass()))))).and(getActiveOrganizationBooleanExpression(group.organization, user))
);
return mainQuery.distinct().list(group);
}