Skip to content

Commit 52754db

Browse files
authored
Merge pull request #2611 from constantine2nd/develop
Get Current User Version 6.0.0 - add on_behalf_of object
2 parents 5f8d830 + edfef8e commit 52754db

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

obp-api/src/main/scala/code/api/util/ApiSession.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ case class CallContext(
3030
dauthResponseHeader: Option[String] = None,
3131
spelling: Option[String] = None,
3232
user: Box[User] = Empty,
33+
onBehalfOfUser: Option[User] = None,
3334
consenter: Box[User] = Empty,
3435
consumer: Box[Consumer] = Empty,
3536
ipAddress: String = "",

obp-api/src/main/scala/code/api/util/ConsentUtil.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ object Consent extends MdcLoggable {
431431

432432
def applyConsentRules(consent: ConsentJWT): Future[(Box[User], Option[CallContext])] = {
433433
val cc = callContext
434+
if(consent.createdByUserId.nonEmpty) {
435+
val onBehalfOfUser = Users.users.vend.getUserByUserId(consent.createdByUserId)
436+
cc.copy(onBehalfOfUser = onBehalfOfUser.toOption)
437+
}
434438
// 1. Get or Create a User
435439
getOrCreateUser(consent.sub, consent.iss, Some(consent.jti), None, None) map {
436440
case (Full(user), newUser) =>

obp-api/src/main/scala/code/api/v6_0_0/APIMethods600.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ import code.api.util.APIUtil._
55
import code.api.util.ApiTag._
66
import code.api.util.ErrorMessages.{$UserNotLoggedIn, InvalidJsonFormat, UnknownError, _}
77
import code.api.util.FutureUtil.EndpointContext
8+
import code.api.util.NewStyle
9+
import code.api.util.NewStyle.HttpCode
810
import code.bankconnectors.LocalMappedConnectorInternal
911
import code.bankconnectors.LocalMappedConnectorInternal._
12+
import code.entitlement.Entitlement
13+
import code.views.Views
1014
import com.github.dwickern.macros.NameOf.nameOf
1115
import com.openbankproject.commons.model._
1216
import com.openbankproject.commons.util.{ApiVersion, ScannedApiVersion}
17+
import net.liftweb.common.Full
1318
import net.liftweb.http.rest.RestHelper
19+
import com.openbankproject.commons.ExecutionContext.Implicits.global
1420

1521
import scala.collection.immutable.{List, Nil}
1622
import scala.collection.mutable.ArrayBuffer
@@ -31,6 +37,46 @@ trait APIMethods600 {
3137
val apiRelations = ArrayBuffer[ApiRelation]()
3238
val codeContext = CodeContext(staticResourceDocs, apiRelations)
3339

40+
41+
staticResourceDocs += ResourceDoc(
42+
getCurrentUser,
43+
implementedInApiVersion,
44+
nameOf(getCurrentUser), // TODO can we get this string from the val two lines above?
45+
"GET",
46+
"/users/current",
47+
"Get User (Current)",
48+
s"""Get the logged in user
49+
|
50+
|${userAuthenticationMessage(true)}
51+
""".stripMargin,
52+
EmptyBody,
53+
userJsonV300,
54+
List(UserNotLoggedIn, UnknownError),
55+
List(apiTagUser))
56+
57+
lazy val getCurrentUser: OBPEndpoint = {
58+
case "users" :: "current" :: Nil JsonGet _ => {
59+
cc => {
60+
implicit val ec = EndpointContext(Some(cc))
61+
for {
62+
(Full(u), callContext) <- authenticatedAccess(cc)
63+
entitlements <- NewStyle.function.getEntitlementsByUserId(u.userId, callContext)
64+
} yield {
65+
val permissions: Option[Permission] = Views.views.vend.getPermissionForUser(u).toOption
66+
val currentUser = UserV600(u, entitlements, permissions)
67+
val onBehalfOfUser = if(cc.onBehalfOfUser.isDefined) {
68+
val entitlements = Entitlement.entitlement.vend.getEntitlementsByUserId(cc.onBehalfOfUser.get.userId).headOption.toList.flatten
69+
val permissions: Option[Permission] = Views.views.vend.getPermissionForUser(cc.onBehalfOfUser.get).toOption
70+
Some(UserV600(cc.onBehalfOfUser.get, entitlements, permissions))
71+
} else {
72+
None
73+
}
74+
(JSONFactory600.createUserInfoJSON(currentUser, onBehalfOfUser), HttpCode.`200`(callContext))
75+
}
76+
}
77+
}
78+
}
79+
3480
staticResourceDocs += ResourceDoc(
3581
createTransactionRequestCardano,
3682
implementedInApiVersion,

obp-api/src/main/scala/code/api/v6_0_0/JSONFactory6.0.0.scala

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
*/
2727
package code.api.v6_0_0
2828

29+
import code.api.util.APIUtil.stringOrNull
2930
import code.api.util._
31+
import code.api.v2_0_0.{EntitlementJSONs, JSONFactory200}
32+
import code.api.v3_0_0.{UserJsonV300, ViewJSON300, ViewsJSON300}
33+
import code.entitlement.Entitlement
3034
import code.util.Helper.MdcLoggable
3135
import com.openbankproject.commons.model._
3236

@@ -59,6 +63,41 @@ case class TransactionRequestBodyCardanoJsonV600(
5963
metadata: Option[Map[String, CardanoMetadataStringJsonV600]] = None
6064
) extends TransactionRequestCommonBodyJSON
6165

66+
case class UserJsonV600(
67+
user_id: String,
68+
email : String,
69+
provider_id: String,
70+
provider : String,
71+
username : String,
72+
entitlements : EntitlementJSONs,
73+
views: Option[ViewsJSON300],
74+
on_behalf_of: Option[UserJsonV300]
75+
)
76+
77+
case class UserV600(user: User, entitlements: List[Entitlement], views: Option[Permission])
78+
case class UsersJsonV600(current_user: UserV600, on_behalf_of_user: UserV600)
79+
6280
object JSONFactory600 extends CustomJsonFormats with MdcLoggable{
63-
81+
def createUserInfoJSON(current_user: UserV600, onBehalfOfUser: Option[UserV600]): UserJsonV600 = {
82+
UserJsonV600(
83+
user_id = current_user.user.userId,
84+
email = current_user.user.emailAddress,
85+
username = stringOrNull(current_user.user.name),
86+
provider_id = current_user.user.idGivenByProvider,
87+
provider = stringOrNull(current_user.user.provider),
88+
entitlements = JSONFactory200.createEntitlementJSONs(current_user.entitlements),
89+
views = current_user.views.map(y => ViewsJSON300(y.views.map((v => ViewJSON300(v.bankId.value, v.accountId.value, v.viewId.value))))),
90+
on_behalf_of = onBehalfOfUser.map { obu =>
91+
UserJsonV300(
92+
user_id = obu.user.userId,
93+
email = obu.user.emailAddress,
94+
username = stringOrNull(obu.user.name),
95+
provider_id = obu.user.idGivenByProvider,
96+
provider = stringOrNull(obu.user.provider),
97+
entitlements = JSONFactory200.createEntitlementJSONs(obu.entitlements),
98+
views = obu.views.map(y => ViewsJSON300(y.views.map((v => ViewJSON300(v.bankId.value, v.accountId.value, v.viewId.value)))))
99+
)
100+
}
101+
)
102+
}
64103
}

0 commit comments

Comments
 (0)