james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject [07/27] james-project git commit: MAILBOX-310 MailboxManager::search should allow retrieving delegated mailboxes
Date Thu, 05 Oct 2017 18:02:19 GMT
MAILBOX-310 MailboxManager::search should allow retrieving delegated mailboxes


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d26baea3
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d26baea3
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d26baea3

Branch: refs/heads/master
Commit: d26baea3b715ff028c2c8d9bf17fcf840037c4a7
Parents: 2650028
Author: benwa <btellier@linagora.com>
Authored: Tue Oct 3 18:26:47 2017 +0700
Committer: Matthieu Baechler <matthieu@apache.org>
Committed: Thu Oct 5 20:00:37 2017 +0200

----------------------------------------------------------------------
 .../james/mailbox/model/MailboxQuery.java       |  6 ++
 .../james/mailbox/MailboxManagerTest.java       | 82 +++++++++++++++++++-
 .../mailbox/store/StoreMailboxManager.java      | 15 +++-
 3 files changed, 100 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d26baea3/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
index 5153ffa..a812206 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java
@@ -146,6 +146,12 @@ public final class MailboxQuery {
         pattern = constructEscapedRegex();
     }
 
+    public boolean isPrivateMailboxes(MailboxSession session) {
+        MailboxSession.User sessionUser = session.getUser();
+        return namespace.map(MailboxConstants.USER_NAMESPACE::equals).orElse(false)
+            && user.map(sessionUser::isSameUser).orElse(false);
+    }
+
     public MailboxPath getPathLike() {
         String combinedName = getCombinedName()
             .replace(getFreeWildcard(), SQL_WILDCARD_CHAR)

http://git-wip-us.apache.org/repos/asf/james-project/blob/d26baea3/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
index 93282df..66ff86c 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java
@@ -33,6 +33,7 @@ import org.apache.james.mailbox.exception.AnnotationException;
 import org.apache.james.mailbox.exception.BadCredentialsException;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.mock.MockMailboxManager;
+import org.apache.james.mailbox.model.MailboxACL;
 import org.apache.james.mailbox.model.MailboxAnnotation;
 import org.apache.james.mailbox.model.MailboxAnnotationKey;
 import org.apache.james.mailbox.model.MailboxId;
@@ -436,4 +437,83 @@ public abstract class MailboxManagerTest {
         builder.add(MailboxAnnotation.newInstance(new MailboxAnnotationKey("/private/comment4"),
"AnyValue"));
 
         mailboxManager.updateAnnotations(inbox, session, builder.build());
-    }}
+    }
+
+    @Test
+    public void searchShouldIncludeDelegatedMailboxes() throws MailboxException {
+        Assume.assumeTrue(mailboxManager.hasCapability(MailboxCapabilities.RightSearch));
+        MailboxSession session1 = mailboxManager.createSystemSession(USER_1);
+        MailboxSession session2 = mailboxManager.createSystemSession(USER_2);
+        MailboxPath inbox1 = MailboxPath.inbox(session1);
+        mailboxManager.createMailbox(inbox1, session1);
+        mailboxManager.setRights(inbox1,
+            MailboxACL.EMPTY.apply(MailboxACL.command()
+                .forUser(USER_2)
+                .rights(MailboxACL.Right.Read)
+                .asAddition()),
+            session1);
+
+        MailboxQuery mailboxQuery = MailboxQuery.builder()
+            .mailboxSession(session2)
+            .matchesAll()
+            .build();
+
+        assertThat(mailboxManager.search(mailboxQuery, session2))
+            .extracting(MailboxMetaData::getPath)
+            .containsOnly(inbox1);
+    }
+
+    @Test
+    public void searchShouldCombinePrivateAndDelegatedMailboxes() throws MailboxException
{
+        Assume.assumeTrue(mailboxManager.hasCapability(MailboxCapabilities.ACL));
+        MailboxSession session1 = mailboxManager.createSystemSession(USER_1);
+        MailboxSession session2 = mailboxManager.createSystemSession(USER_2);
+        MailboxPath inbox1 = MailboxPath.inbox(session1);
+        MailboxPath inbox2 = MailboxPath.inbox(session2);
+        mailboxManager.createMailbox(inbox1, session1);
+        mailboxManager.createMailbox(inbox2, session2);
+        mailboxManager.setRights(inbox1,
+            MailboxACL.EMPTY.apply(MailboxACL.command()
+                .forUser(USER_2)
+                .rights(MailboxACL.Right.Read)
+                .asAddition()),
+            session1);
+
+        MailboxQuery mailboxQuery = MailboxQuery.builder()
+            .matchesAllMailboxNames()
+            .build();
+
+        assertThat(mailboxManager.search(mailboxQuery, session2))
+            .extracting(MailboxMetaData::getPath)
+            .containsOnly(inbox1, inbox2);
+    }
+
+    @Test
+    public void searchShouldNotReturnNoMoreDelegatedMailboxes() throws MailboxException {
+        Assume.assumeTrue(mailboxManager.hasCapability(MailboxCapabilities.ACL));
+        MailboxSession session1 = mailboxManager.createSystemSession(USER_1);
+        MailboxSession session2 = mailboxManager.createSystemSession(USER_2);
+        MailboxPath inbox1 = MailboxPath.inbox(session1);
+        mailboxManager.createMailbox(inbox1, session1);
+        mailboxManager.setRights(inbox1,
+            MailboxACL.EMPTY.apply(MailboxACL.command()
+                .forUser(USER_2)
+                .rights(MailboxACL.Right.Read)
+                .asAddition()),
+            session1);
+
+        mailboxManager.setRights(inbox1,
+            MailboxACL.EMPTY.apply(MailboxACL.command()
+                .forUser(USER_2)
+                .rights(MailboxACL.Right.Read)
+                .asRemoval()),
+            session1);
+
+        MailboxQuery mailboxQuery = MailboxQuery.builder()
+            .matchesAllMailboxNames()
+            .build();
+
+        assertThat(mailboxManager.search(mailboxQuery, session2))
+            .isEmpty();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/d26baea3/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
index b7e8a8d..446e216 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
@@ -26,6 +26,7 @@ import java.util.Objects;
 import java.util.Optional;
 import java.util.Random;
 import java.util.Set;
+import java.util.stream.Stream;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
@@ -674,9 +675,12 @@ public class StoreMailboxManager implements MailboxManager {
     @Override
     public List<MailboxMetaData> search(MailboxQuery mailboxExpression, MailboxSession
session) throws MailboxException {
         MailboxMapper mailboxMapper = mailboxSessionMapperFactory.getMailboxMapper(session);
-        List<Mailbox> mailboxes = mailboxMapper
+        Stream<Mailbox> baseMailboxes = mailboxMapper
             .findMailboxWithPathLike(mailboxExpression.getPathLike())
-            .stream()
+            .stream();
+        Stream<Mailbox> delegatedMailboxes = getDelegatedMailboxes(mailboxMapper, mailboxExpression,
session);
+        List<Mailbox> mailboxes = Stream.concat(baseMailboxes,
+                delegatedMailboxes)
             .filter(Throwing.predicate(mailbox -> isReadable(session, mailbox)))
             .collect(Guavate.toImmutableList());
 
@@ -688,6 +692,13 @@ public class StoreMailboxManager implements MailboxManager {
             .collect(Guavate.toImmutableList());
     }
 
+    private Stream<Mailbox> getDelegatedMailboxes(MailboxMapper mailboxMapper, MailboxQuery
mailboxQuery, MailboxSession session) throws MailboxException {
+        if (mailboxQuery.isPrivateMailboxes(session)) {
+            return Stream.of();
+        }
+        return mailboxMapper.findMailboxes(session.getUser().getUserName(), Right.Read).stream();
+    }
+
     private boolean isReadable(MailboxSession session, Mailbox mailbox) throws MailboxException
{
         return (isSameUser(session, mailbox) && isUserNamespace(mailbox))
                 || hasRight(mailbox, Right.Read, session);


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message