From 30f74555522bed6e332e84c3fe09a03e7b94c3e7 Mon Sep 17 00:00:00 2001 From: hongwei Date: Tue, 22 Feb 2022 14:12:11 +0100 Subject: [PATCH] refactor/tweaked the methods names --- .../main/scala/code/api/GatewayLogin.scala | 4 +-- .../main/scala/code/api/OBPRestHelper.scala | 2 +- .../main/scala/code/api/util/APIUtil.scala | 2 +- .../main/scala/code/api/util/NewStyle.scala | 2 +- .../scala/code/api/v2_0_0/APIMethods200.scala | 2 +- .../scala/code/api/v3_1_0/APIMethods310.scala | 2 +- .../code/model/dataAccess/AuthUser.scala | 33 ++++++++++--------- .../BankAccountCreationDispatcher.scala | 2 +- .../test/scala/code/model/AuthUserTest.scala | 2 +- 9 files changed, 26 insertions(+), 25 deletions(-) diff --git a/obp-api/src/main/scala/code/api/GatewayLogin.scala b/obp-api/src/main/scala/code/api/GatewayLogin.scala index e66c33160..8bd856341 100755 --- a/obp-api/src/main/scala/code/api/GatewayLogin.scala +++ b/obp-api/src/main/scala/code/api/GatewayLogin.scala @@ -271,7 +271,7 @@ object GatewayLogin extends RestHelper with MdcLoggable { val isFirst = getFieldFromPayloadJson(jwtPayload, "is_first") // Update user account views, only when is_first == true in the GatewayLogin token's payload . if(APIUtil.isFirst(isFirst)) { - AuthUser.updateUserAccountViews(u, accounts) + AuthUser.refreshViewsAccountAccessAndHolders(u, accounts) } Full((u, Some(getCbsTokens(s).head),callContext)) // Return user case Empty => @@ -325,7 +325,7 @@ object GatewayLogin extends RestHelper with MdcLoggable { val isFirst = getFieldFromPayloadJson(jwtPayload, "is_first") // Update user account views, only when is_first == true in the GatewayLogin token's payload . if(APIUtil.isFirst(isFirst)) { - AuthUser.updateUserAccountViews(u, accounts) + AuthUser.refreshViewsAccountAccessAndHolders(u, accounts) } Full(u, Some(getCbsTokens(s).head), callContext) // Return user case (Empty, _) => diff --git a/obp-api/src/main/scala/code/api/OBPRestHelper.scala b/obp-api/src/main/scala/code/api/OBPRestHelper.scala index ca4f749fa..3fc94438c 100644 --- a/obp-api/src/main/scala/code/api/OBPRestHelper.scala +++ b/obp-api/src/main/scala/code/api/OBPRestHelper.scala @@ -331,7 +331,7 @@ trait OBPRestHelper extends RestHelper with MdcLoggable { val (user, callContext) = OAuth2Login.getUser(cc) user match { case Full(u) => - AuthUser.refreshUserAccountAccess(u, callContext) + AuthUser.refreshUser(u, callContext) fn(cc.copy(user = Full(u))) // Authentication is successful case ParamFailure(a, b, c, apiFailure : APIFailure) => ParamFailure(a, b, c, apiFailure : APIFailure) case Failure(msg, t, c) => Failure(msg, t, c) diff --git a/obp-api/src/main/scala/code/api/util/APIUtil.scala b/obp-api/src/main/scala/code/api/util/APIUtil.scala index ca634feea..d87482bd0 100644 --- a/obp-api/src/main/scala/code/api/util/APIUtil.scala +++ b/obp-api/src/main/scala/code/api/util/APIUtil.scala @@ -2891,7 +2891,7 @@ object APIUtil extends MdcLoggable with CustomJsonFormats{ */ def refreshUserIfRequired(user: Box[User], callContext: Option[CallContext]) = { if(user.isDefined && UserRefreshes.UserRefreshes.vend.needToRefreshUser(user.head.userId)) - user.map(AuthUser.refreshUserAccountAccess(_, callContext)) + user.map(AuthUser.refreshUser(_, callContext)) else None } diff --git a/obp-api/src/main/scala/code/api/util/NewStyle.scala b/obp-api/src/main/scala/code/api/util/NewStyle.scala index 6958dfe90..f9930abb7 100644 --- a/obp-api/src/main/scala/code/api/util/NewStyle.scala +++ b/obp-api/src/main/scala/code/api/util/NewStyle.scala @@ -1022,7 +1022,7 @@ object NewStyle extends MdcLoggable{ //We will call the `refreshUserAccountAccess` after we successfully create the UserAuthContext // because `createUserAuthContext` is a connector method, here is the entry point for OBP to refreshUserAccountAccess if(callContext.isDefined && callContext.get.user.isDefined) { - AuthUser.refreshUserAccountAccess(callContext.get.user.head, callContext) + AuthUser.refreshUser(callContext.get.user.head, callContext) } else { logger.info(s"AuthUser.refreshUserAccountAccess can not be run properly. The user is missing in the current callContext.") } diff --git a/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala b/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala index 9f5196ca0..b9f8e0fd5 100644 --- a/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala +++ b/obp-api/src/main/scala/code/api/v2_0_0/APIMethods200.scala @@ -1899,7 +1899,7 @@ trait APIMethods200 { _ <- booleanToBox(UserCustomerLink.userCustomerLink.vend.getUserCustomerLink(postedData.user_id, postedData.customer_id).isEmpty == true) ?~! CustomerAlreadyExistsForUser userCustomerLink <- UserCustomerLink.userCustomerLink.vend.createUserCustomerLink(postedData.user_id, postedData.customer_id, new Date(), true) ?~! CreateUserCustomerLinksError _ <- Connector.connector.vend.UpdateUserAccoutViewsByUsername(user.name) - _ <- Full(AuthUser.refreshUserAccountAccess(user, callContext)) + _ <- Full(AuthUser.refreshUser(user, callContext)) } yield { val successJson = Extraction.decompose(code.api.v2_0_0.JSONFactory200.createUserCustomerLinkJSON(userCustomerLink)) diff --git a/obp-api/src/main/scala/code/api/v3_1_0/APIMethods310.scala b/obp-api/src/main/scala/code/api/v3_1_0/APIMethods310.scala index 1e9000ba6..619aade95 100644 --- a/obp-api/src/main/scala/code/api/v3_1_0/APIMethods310.scala +++ b/obp-api/src/main/scala/code/api/v3_1_0/APIMethods310.scala @@ -1972,7 +1972,7 @@ trait APIMethods310 { _ <- NewStyle.function.hasEntitlement("", userId, canRefreshUser, callContext) startTime <- Future{Helpers.now} _ <- NewStyle.function.findByUserId(userId, callContext) - _ = AuthUser.refreshUserAccountAccess(_, callContext) + _ = AuthUser.refreshUser(_, callContext) endTime <- Future{Helpers.now} durationTime = endTime.getTime - startTime.getTime } yield { diff --git a/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala b/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala index 54c13869d..b9402cf16 100644 --- a/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala +++ b/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala @@ -1136,7 +1136,7 @@ def restoreSomeSessions(): Unit = { for { u <- Users.users.vend.getUserByUserName(username) } yield { - refreshUserAccountAccess(u, None) + refreshUser(u, None) } } } @@ -1255,19 +1255,18 @@ def restoreSomeSessions(): Unit = { } /** - * In this method is used for onboarding bank customer to OBP. - * 1st: we will get all the accounts from CBS side. + * This method is used for onboarding bank customer to OBP. + * 1st: we will get all the accountsHeld from CBS side. * 2rd: we will create the account Holder, view and account accesses. */ - def refreshUserAccountAccess(user: User, callContext: Option[CallContext]) = { + def refreshUser(user: User, callContext: Option[CallContext]) = { for{ - (accounts, _) <- Connector.connector.vend.getBankAccountsForUser(user.name,callContext) map { + (accountsHeld, _) <- Connector.connector.vend.getBankAccountsForUser(user.name,callContext) map { connectorEmptyResponse(_, callContext) } - _ = logger.debug(s"-->AuthUser.refreshUserAccountAccess.accounts : ${accounts}") + _ = logger.debug(s"-->AuthUser.refreshUserAccountAccess.accounts : ${accountsHeld}") }yield { - updateUserAccountViews(user, accounts) - UserRefreshes.UserRefreshes.vend.createOrUpdateRefreshUser(user.userId) + refreshViewsAccountAccessAndHolders(user, accountsHeld) } } @@ -1277,18 +1276,18 @@ def restoreSomeSessions(): Unit = { * This method can only be used by the original user(account holder). * InboundAccount return many fields, but in this method, we only need bankId, accountId and viewId so far. */ - def updateUserAccountViews(user: User, cbsAccounts: List[InboundAccount]): Unit = { + def refreshViewsAccountAccessAndHolders(user: User, accountsHeld: List[InboundAccount]): Unit = { if(user.isOriginalUser){ //first, we compare the accounts in obp and the accounts in cbs, - val (_, privateAccountAccesses) = Views.views.vend.privateViewsUserCanAccess(user) - val obpAccountAccessBankAccountIds = privateAccountAccesses.map(accountAccesses =>BankIdAccountId(BankId(accountAccesses.bank_id.get), AccountId(accountAccesses.account_id.get))).toSet + val (_, privateAccountAccess) = Views.views.vend.privateViewsUserCanAccess(user) + val obpAccountAccessBankAccountIds = privateAccountAccess.map(accountAccess =>BankIdAccountId(BankId(accountAccess.bank_id.get), AccountId(accountAccess.account_id.get))).toSet val userOwnBankAccountIds = AccountHolders.accountHolders.vend.getAccountsHeldByUser(user) //The accounts from AccountAccess may contains other users' account info, so here we filter the accounts By account holder, only show the user's own accounts val obpBankAccountIds = obpAccountAccessBankAccountIds.filter(bankAccountId => userOwnBankAccountIds.contains(bankAccountId)).toSet //The accounts from AccountAccess may contains other users' account info, so here we filter the accounts By account holder, only show the user's own accounts - val cbsBankAccountIds = cbsAccounts.map(account =>BankIdAccountId(BankId(account.bankId),AccountId(account.accountId))).toSet + val cbsBankAccountIds = accountsHeld.map(account =>BankIdAccountId(BankId(account.bankId),AccountId(account.accountId))).toSet //cbs removed this accounts, but OBP still contains the data for them, so we need to clean data in OBP side. val cbsRemovedBankAccountIds = obpBankAccountIds diff cbsBankAccountIds @@ -1296,8 +1295,8 @@ def restoreSomeSessions(): Unit = { //cbs has new accounts which are not in obp yet, we need to create new data for these accounts. val csbNewBankAccountIds = cbsBankAccountIds diff obpBankAccountIds - logger.debug("updateUserAccountViews.cbsRemovedBankAccountIds-------"+cbsRemovedBankAccountIds) - logger.debug("updateUserAccountViews.csbNewBankAccountIds-------" + csbNewBankAccountIds) + logger.debug("refreshViewsAccountAccessAndHolders.cbsRemovedBankAccountIds-------"+cbsRemovedBankAccountIds) + logger.debug("refreshViewsAccountAccessAndHolders.csbNewBankAccountIds-------" + csbNewBankAccountIds) //1rd remove the deprecated accounts //TODO. need to double check if we need to clean accountidmapping table, account meta data (MappedTag) .... for{ @@ -1306,7 +1305,7 @@ def restoreSomeSessions(): Unit = { accountId = cbsRemovedBankAccountId.accountId _ = Views.views.vend.revokeAllAccountAccesses(bankId, accountId, user) _ = AccountHolders.accountHolders.vend.deleteAccountHolder(user,cbsRemovedBankAccountId) - cbsAccount = cbsAccounts.find(cbsAccount =>cbsAccount.bankId == bankId.value && cbsAccount.accountId == accountId.value) + cbsAccount = accountsHeld.find(cbsAccount =>cbsAccount.bankId == bankId.value && cbsAccount.accountId == accountId.value) viewId <- cbsAccount.map(_.viewsToGenerate).getOrElse(List.empty[String]) } yield { Views.views.vend.removeCustomView(ViewId(viewId), cbsRemovedBankAccountId) @@ -1318,7 +1317,7 @@ def restoreSomeSessions(): Unit = { _ = AccountHolders.accountHolders.vend.getOrCreateAccountHolder(user,newBankAccountId) bankId = newBankAccountId.bankId accountId = newBankAccountId.accountId - newBankAccount = cbsAccounts.find(cbsAccount =>cbsAccount.bankId == bankId.value && cbsAccount.accountId == accountId.value) + newBankAccount = accountsHeld.find(cbsAccount =>cbsAccount.bankId == bankId.value && cbsAccount.accountId == accountId.value) viewId <- newBankAccount.map(_.viewsToGenerate).getOrElse(List.empty[String]) view <- Views.views.vend.getOrCreateAccountView(newBankAccountId, viewId)//this method will return both system views and custom views back. } yield { @@ -1327,6 +1326,8 @@ def restoreSomeSessions(): Unit = { else //otherwise, we will call `grantAccessToCustomView` Views.views.vend.grantAccessToCustomView(view.uid, user) } + + UserRefreshes.UserRefreshes.vend.createOrUpdateRefreshUser(user.userId) } else { } } diff --git a/obp-api/src/main/scala/code/model/dataAccess/BankAccountCreationDispatcher.scala b/obp-api/src/main/scala/code/model/dataAccess/BankAccountCreationDispatcher.scala index 9f9081512..f773e7454 100644 --- a/obp-api/src/main/scala/code/model/dataAccess/BankAccountCreationDispatcher.scala +++ b/obp-api/src/main/scala/code/model/dataAccess/BankAccountCreationDispatcher.scala @@ -91,7 +91,7 @@ package code.model.dataAccess { AccountHolders.accountHolders.vend.getOrCreateAccountHolder(user: User, BankIdAccountId(bankId, accountId)) // 2rd-refreshUserAccountAccess: in this method, we will simulate onboarding bank user processes. @refreshUserAccountAccess definition. - AuthUser.refreshUserAccountAccess(user, callContext) + AuthUser.refreshUser(user, callContext) } private def addPermissionToSystemOwnerView(bankId : BankId, accountId : AccountId, user: User): Unit = { diff --git a/obp-api/src/test/scala/code/model/AuthUserTest.scala b/obp-api/src/test/scala/code/model/AuthUserTest.scala index 93e616b13..b7e2a5ef5 100644 --- a/obp-api/src/test/scala/code/model/AuthUserTest.scala +++ b/obp-api/src/test/scala/code/model/AuthUserTest.scala @@ -38,7 +38,7 @@ class AuthUserTest extends ServerSetup with DefaultUsers { scenario("we fake the output from getBankAccounts(), and check the functions there") { When("We call the method use resourceUser1") - val result = Await.result(AuthUser.refreshUserAccountAccess(resourceUser1, None), Duration.Inf) + val result = Await.result(AuthUser.refreshUser(resourceUser1, None), Duration.Inf) Then("We check the accountHolders") var accountholder1 = MapperAccountHolders.getAccountHolders(bankIdAccountId.bankId, bankIdAccountId.accountId)