You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/08/14 09:07:39 UTC

[james-project] 11/13: JAMES-3354: handle InsufficientRightsException

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

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

commit 1c6809c87f21612d6a16632bf7093479741a4b4b
Author: duc91 <vd...@linagora.com>
AuthorDate: Wed Aug 12 17:08:08 2020 +0700

    JAMES-3354: handle InsufficientRightsException
---
 .../contract/MailboxSetMethodContract.scala        | 67 +++++++++++++++++++++-
 .../james/jmap/method/MailboxSetMethod.scala       | 36 ++++++------
 2 files changed, 84 insertions(+), 19 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxSetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxSetMethodContract.scala
index 893c111..5f86a34 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxSetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MailboxSetMethodContract.scala
@@ -27,8 +27,9 @@ import org.apache.http.HttpStatus.SC_OK
 import org.apache.james.GuiceJamesServer
 import org.apache.james.jmap.http.UserCredential
 import org.apache.james.jmap.rfc8621.contract.Fixture._
-import org.apache.james.mailbox.model.{MailboxId, MailboxPath}
-import org.apache.james.modules.MailboxProbeImpl
+import org.apache.james.mailbox.model.MailboxACL.Right
+import org.apache.james.mailbox.model.{MailboxACL, MailboxId, MailboxPath}
+import org.apache.james.modules.{ACLProbeImpl, MailboxProbeImpl}
 import org.apache.james.utils.DataProbeImpl
 import org.assertj.core.api.Assertions
 import org.junit.jupiter.api.{BeforeEach, Disabled, Test}
@@ -606,4 +607,66 @@ trait MailboxSetMethodContract {
          |    "c1"]]
          |}""".stripMargin)
   }
+
+  @Test
+  def mailboxSetShouldNotCreateMailboxWhenMailboxItShared(server: GuiceJamesServer): Unit = {
+    val path = MailboxPath.forUser(ANDRE, "mailbox")
+    val mailboxId: MailboxId = server.getProbe(classOf[MailboxProbeImpl]).createMailbox(path)
+
+    server.getProbe(classOf[ACLProbeImpl])
+      .replaceRights(path, BOB.asString, new MailboxACL.Rfc4314Rights(Right.Lookup, Right.Read, Right.CreateMailbox))
+    val request =
+      s"""
+        |{
+        |   "using": [ "urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail" ],
+        |   "methodCalls": [
+        |       [
+        |           "Mailbox/set",
+        |           {
+        |                "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+        |                "create": {
+        |                    "C42": {
+        |                      "name": "childMailbox",
+        |                      "parentId":"${mailboxId.serialize}"
+        |                    }
+        |                }
+        |           },
+        |    "c1"
+        |       ]
+        |   ]
+        |}
+        |""".stripMargin
+
+     val response = `given`
+        .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+        .body(request)
+      .when
+        .post
+      .`then`
+        .log().ifValidationFails()
+        .statusCode(SC_OK)
+        .contentType(JSON)
+        .extract
+        .body
+        .asString
+
+    assertThatJson(response).isEqualTo(
+      s"""{
+         |  "sessionState": "75128aab4b1b",
+         |  "methodResponses": [[
+         |    "Mailbox/set",
+         |    {
+         |      "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+         |      "newState": "000001",
+         |      "notCreated": {
+         |        "C42": {
+         |          "type": "invalidArguments",
+         |          "description": "Insufficient rights",
+         |          "properties":{"value":["parentId"]}
+         |        }
+         |      }
+         |    },
+         |    "c1"]]
+         |}""".stripMargin)
+  }
 }
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetMethod.scala
index 3ebc3bc..1bee2f9 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetMethod.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/MailboxSetMethod.scala
@@ -27,7 +27,7 @@ import org.apache.james.jmap.mail.{IsSubscribed, MailboxCreationRequest, Mailbox
 import org.apache.james.jmap.model.CapabilityIdentifier.CapabilityIdentifier
 import org.apache.james.jmap.model.Invocation.{Arguments, MethodName}
 import org.apache.james.jmap.model.{Invocation, State}
-import org.apache.james.mailbox.exception.{MailboxExistsException, MailboxNameException, MailboxNotFoundException}
+import org.apache.james.mailbox.exception.{InsufficientRightsException, MailboxExistsException, MailboxNameException, MailboxNotFoundException}
 import org.apache.james.mailbox.model.{MailboxId, MailboxPath}
 import org.apache.james.mailbox.{MailboxManager, MailboxSession}
 import org.apache.james.metrics.api.MetricFactory
@@ -43,36 +43,38 @@ sealed trait CreationResult {
 }
 
 case class CreationSuccess(mailboxCreationId: MailboxCreationId, mailboxId: MailboxId) extends CreationResult
+
 case class CreationFailure(mailboxCreationId: MailboxCreationId, exception: Exception) extends CreationResult {
   def asMailboxSetError: MailboxSetError = exception match {
     case e: MailboxNotFoundException => MailboxSetError.invalidArgument(Some(SetErrorDescription(e.getMessage)), Some(Properties(List("parentId"))))
     case e: MailboxExistsException => MailboxSetError.invalidArgument(Some(SetErrorDescription(e.getMessage)), Some(Properties(List("name"))))
     case e: MailboxNameException => MailboxSetError.invalidArgument(Some(SetErrorDescription(e.getMessage)), Some(Properties(List("name"))))
+    case _: InsufficientRightsException => MailboxSetError.invalidArgument(Some(SetErrorDescription("Insufficient rights")), Some(Properties(List("parentId"))))
     case _ => MailboxSetError.serverFail(Some(SetErrorDescription(exception.getMessage)), None)
   }
 }
 
 case class CreationResults(created: Seq[CreationResult]) {
   def retrieveCreated: Map[MailboxCreationId, MailboxId] = created
-      .flatMap(result => result match {
-        case success: CreationSuccess => Some((success.mailboxCreationId, success.mailboxId))
-        case _ => None
-      })
-      .toSet
-      .toMap
+    .flatMap(result => result match {
+      case success: CreationSuccess => Some(success.mailboxCreationId, success.mailboxId)
+      case _ => None
+    })
+    .toSet
+    .toMap
 
   def retrieveErrors: Map[MailboxCreationId, MailboxSetError] = created
-      .flatMap(result => result match {
-        case failure: CreationFailure => Some((failure.mailboxCreationId, failure.asMailboxSetError))
-        case _ => None
-      })
-      .toSet
-      .toMap
+    .flatMap(result => result match {
+      case failure: CreationFailure => Some(failure.mailboxCreationId, failure.asMailboxSetError)
+      case _ => None
+    })
+    .toSet
+    .toMap
 }
 
-class MailboxSetMethod @Inject() (serializer: Serializer,
-                                  mailboxManager: MailboxManager,
-                                  metricFactory: MetricFactory) extends Method {
+class MailboxSetMethod @Inject()(serializer: Serializer,
+                                 mailboxManager: MailboxManager,
+                                 metricFactory: MetricFactory) extends Method {
   override val methodName: MethodName = MethodName("Mailbox/set")
 
 
@@ -80,7 +82,7 @@ class MailboxSetMethod @Inject() (serializer: Serializer,
     metricFactory.decoratePublisherWithTimerMetricLogP99(JMAP_RFC8621_PREFIX + methodName.value,
       asMailboxSetRequest(invocation.arguments)
         .flatMap(mailboxSetRequest => {
-          val (unparsableCreateRequests, createRequests) =  parseCreateRequests(mailboxSetRequest)
+          val (unparsableCreateRequests, createRequests) = parseCreateRequests(mailboxSetRequest)
           for {
             creationResults <- createMailboxes(mailboxSession, createRequests)
           } yield createResponse(invocation, mailboxSetRequest, unparsableCreateRequests, creationResults)


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