@Override
  public Set<EmailRecipient> findSubscribedEmailRecipients(String dispatcherKey, String projectKey,
    SubscriberPermissionsOnProject subscriberPermissionsOnProject) {
    verifyProjectKey(projectKey);

    try (DbSession dbSession = dbClient.openSession(false)) {
      Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification(
        dbSession, dispatcherKey, EmailNotificationChannel.class.getSimpleName(), projectKey);

      return keepAuthorizedEmailSubscribers(dbSession, projectKey, subscriberPermissionsOnProject, emailSubscribers);
    }
  }
@Test
  public void findSubscribedEmailRecipients_with_logins_applies_distinct_permission_filtering_global_or_project_subscribers() {
    String dispatcherKey = randomAlphabetic(12);
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    Set<String> logins = ImmutableSet.of("user1", "user2", "user3");
    when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins))
      .thenReturn(
        newHashSet(EmailSubscriberDto.create("user1", false, "user1@foo"), EmailSubscriberDto.create("user3", false, "user3@foo"),
          EmailSubscriberDto.create("user3", true, "user3@foo")));
    when(authorizationDao.keepAuthorizedLoginsOnEntity(dbSession, newHashSet("user3", "user4"), projectKey, globalPermission))
      .thenReturn(newHashSet("user3"));
    when(authorizationDao.keepAuthorizedLoginsOnEntity(dbSession, newHashSet("user1", "user3"), projectKey, projectPermission))
      .thenReturn(newHashSet("user1", "user3"));

    Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, logins,
      new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    assertThat(emailRecipients)
      .isEqualTo(ImmutableSet.of(new EmailRecipient("user1", "user1@foo"), new EmailRecipient("user3", "user3@foo")));

    // code is optimized to perform only 2 SQL requests for all channels
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnEntity(eq(dbSession), anySet(), anyString(), eq(globalPermission));
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnEntity(eq(dbSession), anySet(), anyString(), eq(projectPermission));
  }