From 6145af48ac0e99c4ed6be079c71a7f052e28e2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Fri, 1 Dec 2023 12:28:57 +0100 Subject: [PATCH 1/2] feature/Add endpoint updateConsumerRedirectUrl v5.1.0 --- .../scala/code/api/v5_1_0/APIMethods510.scala | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala b/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala index 34d2ba0562..ea4270e286 100644 --- a/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala +++ b/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala @@ -11,6 +11,7 @@ import code.api.util.FutureUtil.{EndpointContext, EndpointTimeout} import code.api.util.NewStyle.HttpCode import code.api.util._ import code.api.util.newstyle.RegulatedEntityNewStyle.{createRegulatedEntityNewStyle, deleteRegulatedEntityNewStyle, getRegulatedEntitiesNewStyle, getRegulatedEntityByEntityIdNewStyle} +import code.api.v2_1_0.{ConsumerRedirectUrlJSON, JSONFactory210} import code.api.v3_0_0.JSONFactory300 import code.api.v3_0_0.JSONFactory300.createAggregateMetricJson import code.api.v3_1_0.ConsentJsonV310 @@ -1767,6 +1768,60 @@ trait APIMethods510 { } } + + staticResourceDocs += ResourceDoc( + updateConsumerRedirectUrl, + implementedInApiVersion, + "updateConsumerRedirectUrl", + "PUT", + "/management/consumers/CONSUMER_ID/consumer/redirect_url", + "Update Consumer RedirectUrl", + s"""Update an existing redirectUrl for a Consumer specified by CONSUMER_ID. + | + | CONSUMER_ID can be obtained after you register the application. + | + | Or use the endpoint 'Get Consumers' to get it + | + """.stripMargin, + consumerRedirectUrlJSON, + consumerJSON, + List( + UserNotLoggedIn, + UserHasMissingRoles, + UnknownError + ), + List(apiTagConsumer), + Some(List(canUpdateConsumerRedirectUrl)) + ) + + lazy val updateConsumerRedirectUrl: OBPEndpoint = { + case "management" :: "consumers" :: consumerId :: "consumer" :: "redirect_url" :: Nil JsonPut json -> _ => { + cc => + implicit val ec = EndpointContext(Some(cc)) + for { + (Full(u), callContext) <- authenticatedAccess(cc) + _ <- APIUtil.getPropsAsBoolValue("consumers_enabled_by_default", false) match { + case true => Future(Full(Unit)) + case false => NewStyle.function.hasEntitlement("", u.userId, ApiRole.canUpdateConsumerRedirectUrl, callContext) + } + postJson <- NewStyle.function.tryons(InvalidJsonFormat, 400, callContext) { + json.extract[ConsumerRedirectUrlJSON] + } + consumer <- NewStyle.function.getConsumerByConsumerId(consumerId, callContext) + //only the developer that created the Consumer should be able to edit it + _ <- Helper.booleanToFuture(UserNoPermissionUpdateConsumer, 400, callContext) { + consumer.createdByUserId.equals(u.userId) + } + //update the redirectURL and isactive (set to false when change redirectUrl) field in consumer table + updatedConsumer <- NewStyle.function.updateConsumer(consumer.id.get, None, None, Some(APIUtil.getPropsAsBoolValue("consumers_enabled_by_default", false)), None, None, None, None, Some(postJson.redirect_url), None, callContext) + } yield { + val json = JSONFactory210.createConsumerJSON(updatedConsumer) + (json, HttpCode.`200`(callContext)) + } + } + } + + } } From f9e5cc61486334722689f35c907c07cd3e358dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Fri, 1 Dec 2023 12:35:32 +0100 Subject: [PATCH 2/2] feature/Tweak endpoint updateConsumerRedirectUrl v5.1.0 --- .../scala/code/api/v5_1_0/APIMethods510.scala | 2 +- .../code/api/v5_1_0/JSONFactory5.1.0.scala | 44 ++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala b/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala index ea4270e286..41545ead08 100644 --- a/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala +++ b/obp-api/src/main/scala/code/api/v5_1_0/APIMethods510.scala @@ -1815,7 +1815,7 @@ trait APIMethods510 { //update the redirectURL and isactive (set to false when change redirectUrl) field in consumer table updatedConsumer <- NewStyle.function.updateConsumer(consumer.id.get, None, None, Some(APIUtil.getPropsAsBoolValue("consumers_enabled_by_default", false)), None, None, None, None, Some(postJson.redirect_url), None, callContext) } yield { - val json = JSONFactory210.createConsumerJSON(updatedConsumer) + val json = JSONFactory510.createConsumerJSON(updatedConsumer) (json, HttpCode.`200`(callContext)) } } diff --git a/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala b/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala index 9f18abd192..ccd8eb5d8e 100644 --- a/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala +++ b/obp-api/src/main/scala/code/api/v5_1_0/JSONFactory5.1.0.scala @@ -30,12 +30,13 @@ import code.api.Constant import code.api.util.{APIUtil, ConsentJWT, CustomJsonFormats, JwtUtil, Role} import code.api.util.APIUtil.gitCommit import code.api.v1_4_0.JSONFactory1_4_0.{LocationJsonV140, MetaJsonV140, transformToLocationFromV140, transformToMetaFromV140} +import code.api.v2_1_0.ResourceUserJSON import code.api.v3_0_0.JSONFactory300.{createLocationJson, createMetaJson, transformToAddressFromV300} import code.api.v3_0_0.{AccountIdJson, AccountsIdsJsonV300, AddressJsonV300, OpeningTimesV300} import code.api.v4_0_0.{EnergySource400, HostedAt400, HostedBy400} import code.atmattribute.AtmAttribute import code.atms.Atms.Atm -import code.users.UserAttribute +import code.users.{UserAttribute, Users} import code.views.system.{AccountAccess, ViewDefinition} import com.openbankproject.commons.model.{Address, AtmId, AtmT, BankId, BankIdAccountId, Customer, Location, Meta, RegulatedEntityTrait} import com.openbankproject.commons.util.{ApiVersion, ScannedApiVersion} @@ -43,7 +44,8 @@ import com.openbankproject.commons.util.{ApiVersion, ScannedApiVersion} import java.util.Date import code.consent.MappedConsent import code.metrics.APIMetric -import net.liftweb.common.Box +import code.model.Consumer +import net.liftweb.common.{Box, Full} import net.liftweb.json import net.liftweb.json.{JValue, parse} @@ -279,6 +281,18 @@ case class MetricJsonV510( ) case class MetricsJsonV510(metrics: List[MetricJsonV510]) +case class ConsumerJsonV510(consumer_id: String, + app_name: String, + app_type: String, + description: String, + developer_email: String, + redirect_url: String, + created_by_user_id: String, + created_by_user: ResourceUserJSON, + enabled: Boolean, + created: Date + ) + object JSONFactory510 extends CustomJsonFormats { def createCustomersIds(customers : List[Customer]): CustomersIdsJsonV510 = @@ -606,6 +620,32 @@ object JSONFactory510 extends CustomJsonFormats { MetricsJsonV510(metrics.map(createMetricJson)) } + def createConsumerJSON(c: Consumer): ConsumerJsonV510 = { + + val resourceUserJSON = Users.users.vend.getUserByUserId(c.createdByUserId.toString()) match { + case Full(resourceUser) => ResourceUserJSON( + user_id = resourceUser.userId, + email = resourceUser.emailAddress, + provider_id = resourceUser.idGivenByProvider, + provider = resourceUser.provider, + username = resourceUser.name + ) + case _ => null + } + + ConsumerJsonV510(consumer_id = c.consumerId.get, + app_name = c.name.get, + app_type = c.appType.toString(), + description = c.description.get, + developer_email = c.developerEmail.get, + redirect_url = c.redirectURL.get, + created_by_user_id = c.createdByUserId.get, + created_by_user = resourceUserJSON, + enabled = c.isActive.get, + created = c.createdAt.get + ) + } + }