public QueryBuilder createQueryFilter() {
    BoolQueryBuilder filter = boolQuery();

    // anyone
    filter.should(QueryBuilders.termQuery(FIELD_ALLOW_ANYONE, true));

    // users
    Optional.ofNullable(userSession.getUuid())
      .ifPresent(uuid -> filter.should(termQuery(FIELD_USER_IDS, uuid)));

    // groups
    userSession.getGroups()
      .stream()
      .map(GroupDto::getUuid)
      .forEach(groupUuid -> filter.should(termQuery(FIELD_GROUP_IDS, groupUuid)));

    return JoinQueryBuilders.hasParentQuery(
      TYPE_AUTHORIZATION,
      QueryBuilders.boolQuery().filter(filter),
      false);
  }
@Test
  public void createQueryFilter_sets_filter_on_anyone_and_user_id_and_group_ids_if_user_is_logged_in_and_has_groups() {
    GroupDto group1 = GroupTesting.newGroupDto().setUuid("10");
    GroupDto group2 = GroupTesting.newGroupDto().setUuid("11");
    UserDto userDto = UserTesting.newUserDto();
    userSession.logIn(userDto).setGroups(group1, group2);

    HasParentQueryBuilder filter = (HasParentQueryBuilder) underTest.createQueryFilter();

    assertJson(filter.toString()).isSimilarTo("{" +
      "  \"has_parent\": {" +
      "    \"query\": {" +
      "      \"bool\": {" +
      "        \"filter\": [{" +
      "          \"bool\": {" +
      "            \"should\": [" +
      "              {" +
      "                \"term\": {" +
      "                  \"auth_allowAnyone\": {\"value\": true}" +
      "                }" +
      "              }," +
      "              {" +
      "                \"term\": {" +
      "                  \"auth_userIds\": {\"value\": \"" + userDto.getUuid() + "\"}" +
      "                }" +
      "              }," +
      "              {" +
      "                \"term\": {" +
      "                  \"auth_groupIds\": {\"value\": \"10\"}" +
      "                }" +
      "              }," +
      "              {" +
      "                \"term\": {" +
      "                  \"auth_groupIds\": {\"value\": \"11\"}" +
      "                }" +
      "              }" +
      "            ]" +
      "          }" +
      "        }]" +
      "      }" +
      "    }," +
      "    \"parent_type\": \"auth\"" +
      "  }" +
      "}");
  }