This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 07571e1f56f263d67c3bcc61d03771638fba31ae
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri Jun 28 15:08:43 2024 +0200

    JAMES-4044 Implement SenderIsInLDAPGroup
---
 docs/modules/servers/partials/LDAPMatchers.adoc    |  9 +++++++
 server/mailet/ldap/README.md                       |  9 +++++++
 .../james/transport/matchers/IsInLDAPGroup.java    |  5 ++--
 ...IsInLDAPGroup.java => SenderIsInLDAPGroup.java} | 18 +++++++------
 .../transport/matchers/IsInLDAPGroupTest.java      |  4 ---
 ...GroupTest.java => SenderIsInLDAPGroupTest.java} | 30 ++++++++++++----------
 6 files changed, 46 insertions(+), 29 deletions(-)

diff --git a/docs/modules/servers/partials/LDAPMatchers.adoc 
b/docs/modules/servers/partials/LDAPMatchers.adoc
index 3331358909..26b6bed7ad 100644
--- a/docs/modules/servers/partials/LDAPMatchers.adoc
+++ b/docs/modules/servers/partials/LDAPMatchers.adoc
@@ -8,6 +8,7 @@ Those include:
  - LDAP conditions based on attributes of the recipients
  - LDAP conditions based on attributes of the sender
  - LDAP groups of the recipients
+ - LDAP groups of the sender
 
 Support is planned for LDAP groups both for sender and recipients.
 
@@ -71,3 +72,11 @@ For matching recipients against a specific LDAP group:
     
 </mailet>
 ....
+
+For matching a sender against a specific LDAP group:
+
+....xml
+<mailet matcher="SenderIsInLDAPGroup=cn=mygroup,ou=groups, dc=james,dc=org" 
class="Null">
+    
+</mailet>
+....
diff --git a/server/mailet/ldap/README.md b/server/mailet/ldap/README.md
index 118261bc20..bd36cf574e 100644
--- a/server/mailet/ldap/README.md
+++ b/server/mailet/ldap/README.md
@@ -67,6 +67,15 @@ For matching recipients against a specific LDAP group:
 
 ```xml
 <mailet matcher="IsInLDAPGroup=cn=mygroup,ou=groups, dc=james,dc=org" 
class="Null">
+
+</mailet>
+```
+
+
+For matching senders against a specific LDAP group:
+
+```xml
+<mailet matcher="SenderIsInLDAPGroup=cn=mygroup,ou=groups, dc=james,dc=org" 
class="Null">
     
 </mailet>
 ```
diff --git 
a/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/IsInLDAPGroup.java
 
b/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/IsInLDAPGroup.java
index 65bf989c1b..128ebab867 100644
--- 
a/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/IsInLDAPGroup.java
+++ 
b/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/IsInLDAPGroup.java
@@ -23,6 +23,8 @@ import java.util.Collection;
 import java.util.Optional;
 import java.util.Set;
 
+import jakarta.inject.Inject;
+
 import org.apache.james.core.MailAddress;
 import org.apache.james.user.ldap.LDAPConnectionFactory;
 import org.apache.james.user.ldap.LdapRepositoryConfiguration;
@@ -30,15 +32,12 @@ import org.apache.mailet.Mail;
 import org.apache.mailet.base.GenericMatcher;
 
 import com.github.fge.lambdas.Throwing;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 import com.unboundid.ldap.sdk.Attribute;
 import com.unboundid.ldap.sdk.LDAPConnectionPool;
 import com.unboundid.ldap.sdk.LDAPException;
 import com.unboundid.ldap.sdk.SearchResultEntry;
-import jakarta.inject.Inject;
-import jakarta.mail.MessagingException;
 
 /**
  * Matchers that allow looking up for LDAP group membership for each recipient.
diff --git 
a/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/IsInLDAPGroup.java
 
b/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/SenderIsInLDAPGroup.java
similarity index 88%
copy from 
server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/IsInLDAPGroup.java
copy to 
server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/SenderIsInLDAPGroup.java
index 65bf989c1b..8ca5cc4cf8 100644
--- 
a/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/IsInLDAPGroup.java
+++ 
b/server/mailet/ldap/src/main/java/org/apache/james/transport/matchers/SenderIsInLDAPGroup.java
@@ -23,6 +23,8 @@ import java.util.Collection;
 import java.util.Optional;
 import java.util.Set;
 
+import jakarta.inject.Inject;
+
 import org.apache.james.core.MailAddress;
 import org.apache.james.user.ldap.LDAPConnectionFactory;
 import org.apache.james.user.ldap.LdapRepositoryConfiguration;
@@ -32,16 +34,13 @@ import org.apache.mailet.base.GenericMatcher;
 import com.github.fge.lambdas.Throwing;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Sets;
 import com.unboundid.ldap.sdk.Attribute;
 import com.unboundid.ldap.sdk.LDAPConnectionPool;
 import com.unboundid.ldap.sdk.LDAPException;
 import com.unboundid.ldap.sdk.SearchResultEntry;
-import jakarta.inject.Inject;
-import jakarta.mail.MessagingException;
 
 /**
- * Matchers that allow looking up for LDAP group membership for each recipient.
+ * Matchers that allow looking up for LDAP group membership for the sender.
  *
  * To enable this matcher one need first to add the 
james-server-mailet-ldap.jar in the externals-jars folder of your
  * James installation.
@@ -49,18 +48,18 @@ import jakarta.mail.MessagingException;
  * In order to match the group membership:
  *
  * <pre><code>
- * &lt;mailet matcher=&quot;IsInLDAPGroup=cn=mygroup,ou=groups, 
dc=james,dc=org&quot; class=&quot;Null&quot;&gt;
+ * &lt;mailet matcher=&quot;SenderIsInLDAPGroup=cn=mygroup,ou=groups, 
dc=james,dc=org&quot; class=&quot;Null&quot;&gt;
  *
  * &lt;/mailet&gt;
  * </code></pre>
  */
-public class IsInLDAPGroup extends GenericMatcher {
+public class SenderIsInLDAPGroup extends GenericMatcher {
     private final LDAPConnectionPool ldapConnectionPool;
     private final LdapRepositoryConfiguration configuration;
     private String groupDN;
 
     @Inject
-    public IsInLDAPGroup(LdapRepositoryConfiguration configuration) throws 
LDAPException {
+    public SenderIsInLDAPGroup(LdapRepositoryConfiguration configuration) 
throws LDAPException {
         this.configuration = configuration;
         ldapConnectionPool = new 
LDAPConnectionFactory(this.configuration).getLdapConnectionPool();
     }
@@ -72,7 +71,10 @@ public class IsInLDAPGroup extends GenericMatcher {
 
     @Override
     public Collection<MailAddress> match(Mail mail) {
-        return Sets.intersection(ImmutableSet.copyOf(mail.getRecipients()), 
groupMembers());
+        if (mail.getMaybeSender().asOptional().map(a -> 
groupMembers().contains(a)).orElse(false)) {
+            return mail.getRecipients();
+        }
+        return ImmutableList.of();
     }
 
     private Set<MailAddress> groupMembers() {
diff --git 
a/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/IsInLDAPGroupTest.java
 
b/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/IsInLDAPGroupTest.java
index c599b30cf7..0baf6060ac 100644
--- 
a/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/IsInLDAPGroupTest.java
+++ 
b/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/IsInLDAPGroupTest.java
@@ -22,8 +22,6 @@ package org.apache.james.transport.matchers;
 import static org.apache.james.user.ldap.DockerLdapSingleton.ADMIN;
 import static org.apache.james.user.ldap.DockerLdapSingleton.ADMIN_PASSWORD;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import java.util.Collection;
 import java.util.Optional;
@@ -42,8 +40,6 @@ import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
-import jakarta.mail.MessagingException;
-
 class IsInLDAPGroupTest {
 
     static LdapGenericContainer ldapContainer = 
DockerLdapSingleton.ldapContainer;
diff --git 
a/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/IsInLDAPGroupTest.java
 
b/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/SenderIsInLDAPGroupTest.java
similarity index 82%
copy from 
server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/IsInLDAPGroupTest.java
copy to 
server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/SenderIsInLDAPGroupTest.java
index c599b30cf7..b721cc7d08 100644
--- 
a/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/IsInLDAPGroupTest.java
+++ 
b/server/mailet/ldap/src/test/java/org/apache/james/transport/matchers/SenderIsInLDAPGroupTest.java
@@ -22,8 +22,6 @@ package org.apache.james.transport.matchers;
 import static org.apache.james.user.ldap.DockerLdapSingleton.ADMIN;
 import static org.apache.james.user.ldap.DockerLdapSingleton.ADMIN_PASSWORD;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import java.util.Collection;
 import java.util.Optional;
@@ -42,9 +40,7 @@ import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
-import jakarta.mail.MessagingException;
-
-class IsInLDAPGroupTest {
+class SenderIsInLDAPGroupTest {
 
     static LdapGenericContainer ldapContainer = 
DockerLdapSingleton.ldapContainer;
 
@@ -60,16 +56,18 @@ class IsInLDAPGroupTest {
 
     @Test
     void shouldMatchGroupMember() throws Exception {
-        IsInLDAPGroup testee = new 
IsInLDAPGroup(LdapRepositoryConfiguration.from(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer)));
+        SenderIsInLDAPGroup testee = new 
SenderIsInLDAPGroup(LdapRepositoryConfiguration.from(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer)));
         FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
-            .matcherName("IsInLDAPGroup")
+            .matcherName("SenderIsInLDAPGroup")
             .condition("cn=mygroup,ou=groups, dc=james,dc=org")
             .build();
         testee.init(matcherConfig);
 
-        MailAddress recipient = new MailAddress("[email protected]");
+        MailAddress sender = new MailAddress("[email protected]");
+        MailAddress recipient = new MailAddress("[email protected]");
         Collection<MailAddress> matched = testee.match(FakeMail.builder()
             .name("default-id")
+            .sender(sender)
             .recipient(recipient)
             .build());
 
@@ -78,16 +76,18 @@ class IsInLDAPGroupTest {
 
     @Test
     void shouldNotMatchNotGroupMember() throws Exception {
-        IsInLDAPGroup testee = new 
IsInLDAPGroup(LdapRepositoryConfiguration.from(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer)));
+        SenderIsInLDAPGroup testee = new 
SenderIsInLDAPGroup(LdapRepositoryConfiguration.from(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer)));
         FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
-            .matcherName("IsInLDAPGroup")
+            .matcherName("SenderIsInLDAPGroup")
             .condition("cn=mygroup,ou=groups, dc=james,dc=org")
             .build();
         testee.init(matcherConfig);
 
-        MailAddress recipient = new MailAddress("[email protected]");
+        MailAddress sender = new MailAddress("[email protected]");
+        MailAddress recipient = new MailAddress("[email protected]");
         Collection<MailAddress> matched = testee.match(FakeMail.builder()
             .name("default-id")
+            .sender(sender)
             .recipient(recipient)
             .build());
 
@@ -96,16 +96,18 @@ class IsInLDAPGroupTest {
 
     @Test
     void shouldNotMatchWhenGroupNotFound() throws Exception {
-        IsInLDAPGroup testee = new 
IsInLDAPGroup(LdapRepositoryConfiguration.from(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer)));
+        SenderIsInLDAPGroup testee = new 
SenderIsInLDAPGroup(LdapRepositoryConfiguration.from(ldapRepositoryConfigurationWithVirtualHosting(ldapContainer)));
         FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
-            .matcherName("IsInLDAPGroup")
+            .matcherName("SenderIsInLDAPGroup")
             .condition("cn=notfound,ou=groups, dc=james,dc=org")
             .build();
         testee.init(matcherConfig);
 
-        MailAddress recipient = new MailAddress("[email protected]");
+        MailAddress sender = new MailAddress("[email protected]");
+        MailAddress recipient = new MailAddress("[email protected]");
         Collection<MailAddress> matched = testee.match(FakeMail.builder()
             .name("default-id")
+            .sender(sender)
             .recipient(recipient)
             .build());
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to