#646 Refactor updateUserAccountViews -- move it to AuthUser and Tweaking

This commit is contained in:
hongwei1 2017-07-13 15:19:12 +02:00
parent 89a7276ca9
commit 50a28856eb
8 changed files with 90 additions and 35 deletions

View File

@ -101,6 +101,25 @@ case class InboundUser(
password: String,
displayName: String
)
// This is the common InboundAccount from all Kafka/remote, not finished yet.
trait InboundAccountCommon{
def errorCode: String
def bankId: String
def branchId: String
def accountId: String
def number: String
def accountType: String
def balanceAmount: String
def balanceCurrency: String
def owners: List[String]
def viewsToGenerate: List[String]
def bankRoutingScheme:String
def bankRoutingAddress:String
def branchRoutingScheme:String
def branchRoutingAddress:String
def accountRoutingScheme:String
def accountRoutingAddress:String
}
trait Connector extends MdcLoggable{
@ -150,6 +169,10 @@ trait Connector extends MdcLoggable{
} yield a
}
//Not implement yet, this will be called by AuthUser.updateUserAccountViews2
//when it is stable, will call this method.
def getBankAccounts(user: User): Box[List[InboundAccountCommon]] = Empty
/**
* This method is for get User from external, eg kafka/obpjvm...
* getUserId --> externalUserHelper--> getUserFromConnector --> getUser

View File

@ -18,14 +18,14 @@ case class InboundAccountJune2017(
balanceAmount: String,
balanceCurrency: String,
owners: List[String],
generateViews: List[String],
viewsToGenerate: List[String],
bankRoutingScheme:String,
bankRoutingAddress:String,
branchRoutingScheme:String,
branchRoutingAddress:String,
accountRoutingScheme:String,
accountRoutingAddress:String
) extends InboundMessageBase
) extends InboundMessageBase with InboundAccountCommon
case class BankAccountJune2017(r: InboundAccountJune2017) extends BankAccount {

View File

@ -184,7 +184,7 @@ object KafkaMappedConnector_vJun2017 extends Connector with KafkaHelper with Mdc
balanceAmount = "50",
balanceCurrency = "EUR",
owners = "Susan" :: " Frank" :: Nil,
generateViews = "Public" :: "Accountant" :: "Auditor" ::Nil,
viewsToGenerate = "Public" :: "Accountant" :: "Auditor" ::Nil,
bankRoutingScheme = "iban",
bankRoutingAddress = "bankRoutingAddress",
branchRoutingScheme = "branchRoutingScheme",
@ -202,7 +202,7 @@ object KafkaMappedConnector_vJun2017 extends Connector with KafkaHelper with Mdc
balanceAmount = "50",
balanceCurrency = "EUR",
owners = "Susan" :: " Frank" :: Nil,
generateViews = "Public" :: "Accountant" :: "Auditor" :: Nil,
viewsToGenerate = "Public" :: "Accountant" :: "Auditor" :: Nil,
bankRoutingScheme = "iban",
bankRoutingAddress = "bankRoutingAddress",
branchRoutingScheme = "branchRoutingScheme",
@ -224,11 +224,11 @@ object KafkaMappedConnector_vJun2017 extends Connector with KafkaHelper with Mdc
for {
account <- accounts // many accounts
viewName <- account.generateViews
viewId <- account.viewsToGenerate
bankId <- Full(BankId(account.bankId))
accountId <- Full(AccountId(account.accountId))
bankAccountUID <- Full(BankAccountUID(bankId, accountId))
view <- Views.views.vend.getOrCreateAccountView(bankAccountUID, viewName)
view <- Views.views.vend.getOrCreateAccountView(bankAccountUID, viewId)
viewUID <-Full(ViewUID(view.viewId,view.bankId, view.accountId))
} yield {
Views.views.vend.getOrCreateViewPrivilege(bankAccountUID, viewUID, user)
@ -701,7 +701,7 @@ object KafkaMappedConnector_vJun2017 extends Connector with KafkaHelper with Mdc
balanceAmount = "50",
balanceCurrency = "EUR",
owners = "Susan" :: " Frank" :: Nil,
generateViews = "Public" :: "Accountant" :: "Auditor" :: Nil,
viewsToGenerate = "Public" :: "Accountant" :: "Auditor" :: Nil,
bankRoutingScheme = "iban",
bankRoutingAddress = "bankRoutingAddress",
branchRoutingScheme = "branchRoutingScheme",
@ -763,7 +763,7 @@ object KafkaMappedConnector_vJun2017 extends Connector with KafkaHelper with Mdc
balanceAmount = "50",
balanceCurrency = "EUR",
owners = "Susan" :: " Frank" :: Nil,
generateViews = "Public" :: "Accountant" :: "Auditor" :: Nil,
viewsToGenerate = "Public" :: "Accountant" :: "Auditor" :: Nil,
bankRoutingScheme = "iban",
bankRoutingAddress = "bankRoutingAddress",
branchRoutingScheme = "branchRoutingScheme",
@ -847,7 +847,7 @@ object KafkaMappedConnector_vJun2017 extends Connector with KafkaHelper with Mdc
balanceAmount = "50",
balanceCurrency = "EUR",
owners = "Susan" :: " Frank" :: Nil,
generateViews = "Public" :: "Accountant" :: "Auditor" :: Nil,
viewsToGenerate = "Public" :: "Accountant" :: "Auditor" :: Nil,
bankRoutingScheme = "iban",
bankRoutingAddress = "bankRoutingAddress",
branchRoutingScheme = "branchRoutingScheme",
@ -1772,7 +1772,7 @@ object KafkaMappedConnector_vJun2017 extends Connector with KafkaHelper with Mdc
balanceAmount = "50",
balanceCurrency = "EUR",
owners = "Susan" :: " Frank" :: Nil,
generateViews = "Public" :: "Accountant" :: "Auditor" :: Nil,
viewsToGenerate = "Public" :: "Accountant" :: "Auditor" :: Nil,
bankRoutingScheme = "iban",
bankRoutingAddress = "bankRoutingAddress",
branchRoutingScheme = "branchRoutingScheme",

View File

@ -33,6 +33,7 @@ package code.model.dataAccess
import java.util.UUID
import code.accountholder.AccountHolders
import code.api.util.APIUtil.{isThereAnOAuthHeader, isValidStrongPassword, _}
import code.api.util.{APIUtil, ErrorMessages}
import code.api.{DirectLogin, OAuthHandshake}
@ -45,9 +46,10 @@ import net.liftweb.util._
import scala.xml.{NodeSeq, Text}
import code.loginattempts.LoginAttempt
import code.model.User
import code.model._
import code.users.Users
import code.util.Helper
import code.views.Views
/**
@ -819,7 +821,36 @@ import net.liftweb.util.Helpers._
} yield v
}
}
/**
* Update accounts, views, accountholders when sign up new remote user
* This method will be called in AuthUser, so keep it here.
*/
def updateUserAccountViews2(user: ResourceUser): Unit = {
//these accounts will be got from remote, over Kafka
val accounts = Connector.connector.vend.getBankAccounts(user).get
debug(s"-->AuthUser.updateUserAccountViews.accounts : ${accounts} ")
//As to performance issue : this for loop run: (number of account * number of views) times
// each round will run 2+3+2 = 7 sql queries(3 writing + 4 reading )
for {
account <- accounts // many accounts
viewId <- account.viewsToGenerate // many views
bankId <- Full(BankId(account.bankId))
accountId <- Full(AccountId(account.accountId))
bankAccountUID <- Full(BankAccountUID(bankId, accountId))
//As to performance issue : following contains 2 SQL queries: ViewImpl.find + ViewImpl.create
view <- Views.views.vend.getOrCreateAccountView(bankAccountUID, viewId)
viewBankAccountUID <-Full(ViewUID(view.viewId,view.bankId, view.accountId))
} yield {
//As to performance issue : following contains 3 SQL queries: ViewImpl.find + ViewPrivileges.count(By + ViewPrivileges.create
Views.views.vend.getOrCreateViewPrivilege(bankAccountUID, viewBankAccountUID, user)
//As to performance issue : following contains 2 SQL queries: MapperAccountHolders.find + MapperAccountHolders.create
AccountHolders.accountHolders.vend.getOrCreateAccountHolder(user,bankAccountUID)
}
}
/**
* Find the authUser by author user name(authUser and resourceUser are the same).
* Only search for the local database.

View File

@ -21,8 +21,9 @@ object RemotedataViews extends ObpActorInit with Views {
def addPermission(viewUID: ViewUID, user: User): Box[View] =
extractFutureToBox(actor ? cc.addPermission(viewUID, user))
def getOrCreateViewPrivilege(account: BankAccountUID,viewUID: ViewUID, user: User)=
extractFutureToBox(actor ? cc.getOrCreateViewPrivilege(account: BankAccountUID,viewUID: ViewUID, user: User))
def getOrCreateViewPrivilege(bankAccountUID: BankAccountUID, viewBankAccountUID: ViewUID, user: User): Box[View] =
extractFutureToBox(actor ? cc.getOrCreateViewPrivilege(bankAccountUID: BankAccountUID,
viewBankAccountUID: ViewUID, user: User))
def revokePermission(viewUID : ViewUID, user : User) : Box[Boolean] =
extractFutureToBox(actor ? cc.revokePermission(viewUID, user))
@ -91,8 +92,8 @@ object RemotedataViews extends ObpActorInit with Views {
def getOwners(view: View) : Set[User] =
extractFuture(actor ? cc.getOwners(view))
def getOrCreateAccountView(account: BankAccountUID, viewName: String): Box[View] =
extractFutureToBox(actor ? cc.getOrCreateAccountView(account: BankAccountUID, viewName: String))
def getOrCreateAccountView(bankAccountUID: BankAccountUID, viewId: String): Box[View] =
extractFutureToBox(actor ? cc.getOrCreateAccountView(bankAccountUID: BankAccountUID, viewId: String))
def getOrCreateOwnerView(bankId: BankId, accountId: AccountId, description: String) : Box[View] =
extractFutureToBox(actor ? cc.getOrCreateOwnerView(bankId, accountId, description))

View File

@ -24,7 +24,7 @@ class RemotedataViewsActor extends Actor with ObpActorHelper with MdcLoggable {
case cc.getOrCreateViewPrivilege(account: BankAccountUID,viewUID: ViewUID, user: User) =>
logger.debug("getOrCreateViewPrivilege(" + account +"," +viewUID +"," + user +")")
sender ! extractResult(mapper.getOrCreateViewPrivilege(account: BankAccountUID,viewUID: ViewUID, user: User))
sender ! extractResult(mapper.getOrCreateViewPrivilege(account: BankAccountUID, viewUID: ViewUID, user: User))
case cc.permission(account : BankAccountUID, user: User) =>
logger.debug("permission(" + account +"," + user +")")

View File

@ -65,14 +65,14 @@ object MapperViews extends Views with MdcLoggable {
* Note: This method is a little different with addPermission,
* it will check the view is belong to the account or not firstly.
*/
def getOrCreateViewPrivilege(account: BankAccountUID,viewUID: ViewUID, user: User): Box[View] = {
if(account.accountId.value == viewUID.accountId.value){
val newView = Views.views.vend.addPermission(viewUID, user)
logger.debug(s"-->CreateViewPrivilege: update the View ${viewUID} for resourceuser ${user} and account ${viewUID.accountId.value} ")
def getOrCreateViewPrivilege(bankAccountUID: BankAccountUID, viewBankAccountUID: ViewUID, user: User): Box[View] = {
if(bankAccountUID.accountId.value == viewBankAccountUID.accountId.value){
val newView = Views.views.vend.addPermission(viewBankAccountUID, user)
logger.debug(s"-->CreateViewPrivilege: update the View ${viewBankAccountUID } for resourceuser ${user} and account ${viewBankAccountUID.accountId.value} ")
newView
}
else{
logger.debug(s"-->CreateViewPrivilege: update the View.account.id(${viewUID.accountId})is not the same as account.id(${account.accountId})")
logger.debug(s"-->CreateViewPrivilege: update the View.account.id(${viewBankAccountUID.accountId})is not the same as account.id(${bankAccountUID.accountId})")
Empty
}
}
@ -427,23 +427,23 @@ object MapperViews extends Views with MdcLoggable {
}
/**
* @param account the IncomingAccount from Kafka
* @param viewName This field should be selected one from Owner/Public/Accountant/Auditor, only support
* these four values.
* @param bankAccountUID the IncomingAccount from Kafka
* @param viewId This field should be selected one from Owner/Public/Accountant/Auditor, only support
* these four values.
* @return This will insert a View (e.g. the owner view) for an Account (BankAccount), and return the view
* Note:
* updateUserAccountViews would call createAccountView once per View specified in the IncomingAccount from Kafka.
* We should cache this function because the available views on an account will change rarely.
*
*/
def getOrCreateAccountView(account: BankAccountUID, viewName: String): Box[View] = {
def getOrCreateAccountView(bankAccountUID: BankAccountUID, viewId: String): Box[View] = {
val bankId = account.bankId
val accountId = account.accountId
val ownerView = "Owner".equals(viewName)
val publicView = "Public".equals(viewName)
val accountantsView = "Accountant".equals(viewName)
val auditorsView = "Auditor".equals(viewName)
val bankId = bankAccountUID.bankId
val accountId = bankAccountUID.accountId
val ownerView = "Owner".equals(viewId)
val publicView = "Public".equals(viewId)
val accountantsView = "Accountant".equals(viewId)
val auditorsView = "Auditor".equals(viewId)
val newView =
if (ownerView)
@ -456,7 +456,7 @@ object MapperViews extends Views with MdcLoggable {
Views.views.vend.getOrCreateAuditorsView(bankId, accountId, "Auditors View")
else Empty
logger.debug(s"-->getOrCreateAccountView.${viewName} : ${newView} ")
logger.debug(s"-->getOrCreateAccountView.${viewId } : ${newView} ")
newView
}

View File

@ -22,7 +22,7 @@ trait Views {
def permissions(account : BankAccountUID) : List[Permission]
def permission(account : BankAccountUID, user: User) : Box[Permission]
def getOrCreateViewPrivilege(account: BankAccountUID,viewUID: ViewUID, user: User): Box[View]
def getOrCreateViewPrivilege(bankAccountUID: BankAccountUID, viewBankAccountUID: ViewUID, user: User): Box[View]
def addPermission(viewUID : ViewUID, user : User) : Box[View]
def addPermissions(views : List[ViewUID], user : User) : Box[List[View]]
def revokePermission(viewUID : ViewUID, user : User) : Box[Boolean]
@ -45,7 +45,7 @@ trait Views {
def getNonPublicBankAccounts(user : User) : List[BankAccountUID]
def getNonPublicBankAccounts(user : User, bankId : BankId) : List[BankAccountUID]
def getOrCreateAccountView(account: BankAccountUID, viewName: String): Box[View]
def getOrCreateAccountView(bankAccountUID: BankAccountUID, viewId: String): Box[View]
def getOrCreateOwnerView(bankId: BankId, accountId: AccountId, description: String) : Box[View]
def getOrCreatePublicView(bankId: BankId, accountId: AccountId, description: String) : Box[View]
def getOrCreateAccountantsView(bankId: BankId, accountId: AccountId, description: String) : Box[View]