You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2023/01/19 03:22:14 UTC

[james-project] branch master updated: JAMES-3756 - Update Delegate/get, DelegatedAccount/get - return DelegationId in response (#1395)

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


The following commit(s) were added to refs/heads/master by this push:
     new b75967c414 JAMES-3756 - Update Delegate/get, DelegatedAccount/get - return DelegationId in response (#1395)
b75967c414 is described below

commit b75967c414c22184956d9e72972daaa2c55efd12
Author: vttran <vt...@linagora.com>
AuthorDate: Thu Jan 19 10:22:08 2023 +0700

    JAMES-3756 - Update Delegate/get, DelegatedAccount/get - return DelegationId in response (#1395)
---
 .../contract/DelegateGetMethodContract.scala       | 15 +++++++------
 .../DelegatedAccountGetMethodContract.scala        | 26 +++++++++-------------
 .../rfc8621/contract/probe/DelegationProbe.scala   |  6 ++---
 .../apache/james/jmap/delegation/DelegateGet.scala | 25 ++++++++-------------
 .../jmap/delegation/DelegatedAccountGet.scala      |  4 ++--
 .../james/jmap/method/DelegateGetMethod.scala      |  3 ++-
 .../jmap/method/DelegatedAccountGetMethod.scala    |  3 ++-
 7 files changed, 36 insertions(+), 46 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/DelegateGetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/DelegateGetMethodContract.scala
index 2557e9dcb7..5518aef9e3 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/DelegateGetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/DelegateGetMethodContract.scala
@@ -28,6 +28,7 @@ import net.javacrumbs.jsonunit.core.internal.Options
 import org.apache.http.HttpStatus.SC_OK
 import org.apache.james.GuiceJamesServer
 import org.apache.james.jmap.core.ResponseObject.SESSION_STATE
+import org.apache.james.jmap.delegation.DelegationId
 import org.apache.james.jmap.http.UserCredential
 import org.apache.james.jmap.rfc8621.contract.DelegateGetMethodContract.BOB_ACCOUNT_ID
 import org.apache.james.jmap.rfc8621.contract.Fixture.{ACCEPT_RFC8621_VERSION_HEADER, ANDRE, ANDRE_ACCOUNT_ID, ANDRE_PASSWORD, BOB, BOB_PASSWORD, CEDRIC, DOMAIN, authScheme, baseRequestSpecBuilder}
@@ -134,7 +135,7 @@ trait DelegateGetMethodContract {
            |                "notFound": [],
            |                "list": [
            |                    {
-           |                        "id": "${Fixture.ANDRE_ACCOUNT_ID}",
+           |                        "id": "${DelegationId.from(BOB, ANDRE).serialize}",
            |                        "username": "${ANDRE.asString()}"
            |                    }
            |                ]
@@ -233,10 +234,10 @@ trait DelegateGetMethodContract {
   def delegateGetShouldReturnNotFoundAndListWhenMixCases(server: GuiceJamesServer): Unit = {
     val delegatedId = server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(BOB, ANDRE)
-      .id.value
+      .serialize
     val delegatedId2 = server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(BOB, CEDRIC)
-      .id.value
+      .serialize
 
     val response = `given`
       .body(
@@ -330,7 +331,7 @@ trait DelegateGetMethodContract {
   def delegateGetShouldNotReturnDelegateOfOtherUserWhenProvideIds(server: GuiceJamesServer): Unit = {
     val delegateId = server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(ANDRE, BOB)
-      .id.value
+      .serialize
 
     val response = `given`
       .body(
@@ -373,10 +374,10 @@ trait DelegateGetMethodContract {
   def bobShouldNotGetDelegateListOfAliceEvenDelegated(server: GuiceJamesServer): Unit = {
     val delegateId1 = server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(ANDRE, BOB)
-      .id.value
+      .serialize
     val delegateId2 =  server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(ANDRE, CEDRIC)
-      .id.value
+      .serialize
 
     val response = `given`
       .body(
@@ -423,7 +424,7 @@ trait DelegateGetMethodContract {
   def delegateGetReturnIdWhenNoPropertiesRequested(server: GuiceJamesServer): Unit = {
     val delegateId = server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(BOB, ANDRE)
-      .id.value
+      .serialize
 
     val response = `given`
       .body(
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/DelegatedAccountGetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/DelegatedAccountGetMethodContract.scala
index 7a30c377e5..3c5b855a86 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/DelegatedAccountGetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/DelegatedAccountGetMethodContract.scala
@@ -27,9 +27,8 @@ import net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER
 import net.javacrumbs.jsonunit.core.internal.Options
 import org.apache.http.HttpStatus.SC_OK
 import org.apache.james.GuiceJamesServer
-import org.apache.james.core.Username
-import org.apache.james.jmap.core.AccountId
 import org.apache.james.jmap.core.ResponseObject.SESSION_STATE
+import org.apache.james.jmap.delegation.DelegationId
 import org.apache.james.jmap.http.UserCredential
 import org.apache.james.jmap.rfc8621.contract.DelegatedAccountGetMethodContract.BOB_ACCOUNT_ID
 import org.apache.james.jmap.rfc8621.contract.Fixture.{ACCEPT_RFC8621_VERSION_HEADER, ANDRE, ANDRE_ACCOUNT_ID, ANDRE_PASSWORD, BOB, BOB_PASSWORD, CEDRIC, DOMAIN, authScheme, baseRequestSpecBuilder}
@@ -136,7 +135,7 @@ trait DelegatedAccountGetMethodContract {
            |                "notFound": [],
            |                "list": [
            |                    {
-           |                        "id": "${getAccountId(ANDRE)}",
+           |                        "id": "${DelegationId.from(ANDRE, BOB).serialize}",
            |                        "username": "${ANDRE.asString()}"
            |                    }
            |                ]
@@ -235,10 +234,10 @@ trait DelegatedAccountGetMethodContract {
   def delegatedAccountGetShouldReturnNotFoundAndListWhenMixCases(server: GuiceJamesServer): Unit = {
     server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(ANDRE, BOB)
-      .id.value
+      .serialize
     server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(CEDRIC, BOB)
-      .id.value
+      .serialize
 
     val response = `given`
       .body(
@@ -250,7 +249,7 @@ trait DelegatedAccountGetMethodContract {
            |    "DelegatedAccount/get",
            |    {
            |      "accountId": "$BOB_ACCOUNT_ID",
-           |      "ids": [ "notFound1", "${getAccountId(ANDRE)}", "${getAccountId(CEDRIC)}" ]
+           |      "ids": [ "notFound1", "${DelegationId.from(ANDRE, BOB).serialize}", "${DelegationId.from(CEDRIC, BOB).serialize}" ]
            |    },
            |    "c1"]]
            |}""".stripMargin)
@@ -272,11 +271,11 @@ trait DelegatedAccountGetMethodContract {
          |      "accountId": "$BOB_ACCOUNT_ID",
          |      "list": [
          |        {
-         |          "id": "${getAccountId(ANDRE)}",
+         |          "id": "${DelegationId.from(ANDRE, BOB).serialize}",
          |          "username": "${ANDRE.asString()}"
          |        },
          |        {
-         |          "id": "${getAccountId(CEDRIC)}",
+         |          "id": "${DelegationId.from(CEDRIC, BOB).serialize}",
          |          "username": "${CEDRIC.asString()}"
          |        }
          |      ],
@@ -331,8 +330,8 @@ trait DelegatedAccountGetMethodContract {
   @Test
   def delegatedAccountGetShouldNotReturnDelegateOfOtherUserWhenProvideIds(server: GuiceJamesServer): Unit = {
     val delegateId = server.getProbe(classOf[DelegationProbe])
-      .addAuthorizedUser(ANDRE, BOB)
-      .id.value
+      .addAuthorizedUser(ANDRE, CEDRIC)
+      .serialize
 
     val response = `given`
       .body(
@@ -375,10 +374,8 @@ trait DelegatedAccountGetMethodContract {
   def bobShouldNotGetDelegatedAccountListOfAliceEvenAuthorized(server: GuiceJamesServer): Unit = {
     server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(ANDRE, BOB)
-      .id.value
     server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(CEDRIC, ANDRE)
-      .id.value
 
     val response = `given`
       .body(
@@ -425,7 +422,6 @@ trait DelegatedAccountGetMethodContract {
   def delegatedAccountGetReturnIdWhenNoPropertiesRequested(server: GuiceJamesServer): Unit = {
     server.getProbe(classOf[DelegationProbe])
       .addAuthorizedUser(ANDRE, BOB)
-      .id.value
 
     val response = `given`
       .body(
@@ -459,7 +455,7 @@ trait DelegatedAccountGetMethodContract {
          |    {
          |      "accountId": "$BOB_ACCOUNT_ID",
          |      "list": [
-         |        { "id" : "${getAccountId(ANDRE)}" }
+         |        { "id" : "${DelegationId.from(ANDRE, BOB).serialize}" }
          |      ],
          |      "notFound": []
          |    },
@@ -630,6 +626,4 @@ trait DelegatedAccountGetMethodContract {
          |}""".stripMargin)
   }
 
-  private def getAccountId(username: Username): String =
-    AccountId.from(username).toOption.get.id.value
 }
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/probe/DelegationProbe.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/probe/DelegationProbe.scala
index 2d2aa7ba1d..bf5b332368 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/probe/DelegationProbe.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/probe/DelegationProbe.scala
@@ -23,7 +23,7 @@ import com.google.inject.AbstractModule
 import com.google.inject.multibindings.Multibinder
 import javax.inject.Inject
 import org.apache.james.core.Username
-import org.apache.james.jmap.core.AccountId
+import org.apache.james.jmap.delegation.DelegationId
 import org.apache.james.user.api.DelegationStore
 import org.apache.james.utils.GuiceProbe
 import reactor.core.scala.publisher.{SFlux, SMono}
@@ -46,8 +46,8 @@ class DelegationProbe @Inject()(delegationStore: DelegationStore) extends GuiceP
       .collectSeq()
       .block()
 
-  def addAuthorizedUser(baseUser: Username, userWithAccess: Username): AccountId =
+  def addAuthorizedUser(baseUser: Username, userWithAccess: Username): DelegationId =
     SMono.fromPublisher(delegationStore.addAuthorizedUser(baseUser, userWithAccess))
-      .`then`(SMono.just(AccountId.from(userWithAccess).toOption.get))
+      .`then`(SMono.just(DelegationId.from(baseUser, userWithAccess)))
       .block()
 }
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegateGet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegateGet.scala
index 590d2a8a9a..b1422f85f4 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegateGet.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegateGet.scala
@@ -20,11 +20,10 @@
 package org.apache.james.jmap.delegation
 
 import java.util.UUID
-
 import eu.timepit.refined.auto._
 import org.apache.james.core.Username
 import org.apache.james.jmap.core.Id.Id
-import org.apache.james.jmap.core.{AccountId, Properties}
+import org.apache.james.jmap.core.{AccountId, Id, Properties}
 import org.apache.james.jmap.method.WithAccountId
 
 import scala.util.Try
@@ -54,20 +53,14 @@ case class DelegateIds(value: List[UnparsedDelegateId])
 case class DelegateGetRequest(accountId: AccountId,
                               ids: Option[DelegateIds],
                               properties: Option[Properties]) extends WithAccountId
-
-object Delegate {
-  def from(username: Username): Delegate =
-    AccountId.from(username)
-      .map(accountId => Delegate(accountId, username)) match {
-      case Right(value) => value
-      case Left(error) => throw error
-    }
-
+case class Delegate(id: DelegationId,
+                    username: Username) {
+  def delegationIdAsId(): Id = Id.validate(id.serialize) match {
+    case Right(value) => value
+    case Left(error) => throw error
+  }
 }
 
-case class Delegate(id: AccountId,
-                    username: Username)
-
 case class DelegateNotFound(value: Set[UnparsedDelegateId])
 
 object DelegateGetResult {
@@ -75,8 +68,8 @@ object DelegateGetResult {
     requestIds match {
       case None => DelegateGetResult(delegates)
       case Some(value) => DelegateGetResult(
-        list = delegates.filter(delegate => value.contains(delegate.id.id)),
-        notFound = DelegateNotFound(value.diff(delegates.map(_.id.id).toSet).map(UnparsedDelegateId)))
+        list = delegates.filter(delegate => value.contains(delegate.delegationIdAsId())),
+        notFound = DelegateNotFound(value.diff(delegates.map(_.delegationIdAsId()).toSet).map(UnparsedDelegateId)))
     }
 }
 
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegatedAccountGet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegatedAccountGet.scala
index c3ae683288..aba972feb7 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegatedAccountGet.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/delegation/DelegatedAccountGet.scala
@@ -42,8 +42,8 @@ object DelegatedAccountGetResult {
     requestIds match {
       case None => DelegatedAccountGetResult(delegates)
       case Some(value) => DelegatedAccountGetResult(
-        list = delegates.filter(delegate => value.contains(delegate.id.id)),
-        notFound = DelegatedAccountNotFound(value.diff(delegates.map(_.id.id).toSet).map(UnparsedDelegateId)))
+        list = delegates.filter(delegate => value.contains(delegate.delegationIdAsId())),
+        notFound = DelegatedAccountNotFound(value.diff(delegates.map(_.delegationIdAsId()).toSet).map(UnparsedDelegateId)))
     }
 }
 case class DelegatedAccountGetResult(list: Seq[Delegate],
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegateGetMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegateGetMethod.scala
index 4d140ed188..b56fe00959 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegateGetMethod.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegateGetMethod.scala
@@ -26,6 +26,7 @@ import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JA
 import org.apache.james.jmap.core.Invocation.{Arguments, MethodName}
 import org.apache.james.jmap.core.{ErrorCode, Invocation, Properties, SessionTranslator}
 import org.apache.james.jmap.delegation.{Delegate, DelegateGet, DelegateGetRequest, DelegateGetResult, ForbiddenAccountManagementException}
+import org.apache.james.jmap.delegation.{Delegate, DelegateGet, DelegateGetRequest, DelegateGetResult, DelegationId}
 import org.apache.james.jmap.json.{DelegationSerializer, ResponseSerializer}
 import org.apache.james.jmap.routes.SessionSupplier
 import org.apache.james.mailbox.MailboxSession
@@ -73,7 +74,7 @@ class DelegateGetMethod @Inject()(val metricFactory: MetricFactory,
 
   private def getAuthorizedUser(baseUser: Username, request: DelegateGetRequest): SMono[DelegateGetResult] =
     SFlux(delegationStore.authorizedUsers(baseUser))
-      .map(Delegate.from)
+      .map(authorizedUser => Delegate(DelegationId.from(baseUser, authorizedUser), authorizedUser))
       .collectSeq()
       .map(delegates => DelegateGetResult.from(delegates, request.ids.map(_.value.map(_.id).toSet)))
 
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegatedAccountGetMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegatedAccountGetMethod.scala
index 0687f35502..96330d7eae 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegatedAccountGetMethod.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/DelegatedAccountGetMethod.scala
@@ -26,6 +26,7 @@ import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JA
 import org.apache.james.jmap.core.Invocation.{Arguments, MethodName}
 import org.apache.james.jmap.core.{ErrorCode, Invocation, Properties, SessionTranslator}
 import org.apache.james.jmap.delegation.{Delegate, DelegatedAccountGet, DelegatedAccountGetRequest, DelegatedAccountGetResult, ForbiddenAccountManagementException}
+import org.apache.james.jmap.delegation.{Delegate, DelegatedAccountGet, DelegatedAccountGetRequest, DelegatedAccountGetResult, DelegationId}
 import org.apache.james.jmap.json.{DelegationSerializer, ResponseSerializer}
 import org.apache.james.jmap.routes.SessionSupplier
 import org.apache.james.mailbox.MailboxSession
@@ -73,7 +74,7 @@ class DelegatedAccountGetMethod @Inject()(val metricFactory: MetricFactory,
 
   private def getDelegatedUser(baseUser: Username, request: DelegatedAccountGetRequest): SMono[DelegatedAccountGetResult] =
     SFlux(delegationStore.delegatedUsers(baseUser))
-      .map(Delegate.from)
+      .map(delegatedUser => Delegate(DelegationId.from(delegatedUser, baseUser), delegatedUser))
       .collectSeq()
       .map(delegates => DelegatedAccountGetResult.from(delegates, request.ids.map(_.value.map(_.id).toSet)))
 


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