From 807bbfb1332d2243fa8273a252e70131292ed4a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Tue, 11 Aug 2020 09:43:07 +0200 Subject: [PATCH] feature/Add connector method checkExternalUserExists part 3 --- .../code/model/dataAccess/AuthUser.scala | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) 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 2f771b6e0..285f9ca36 100644 --- a/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala +++ b/obp-api/src/main/scala/code/model/dataAccess/AuthUser.scala @@ -31,7 +31,7 @@ import code.accountholders.AccountHolders import code.api.util.APIUtil.{hasAnOAuthHeader, isValidStrongPassword, _} import code.api.util.ErrorMessages._ import code.api.util._ -import code.api.{DirectLogin, GatewayLogin, OAuthHandshake} +import code.api.{APIFailure, DirectLogin, GatewayLogin, OAuthHandshake} import code.bankconnectors.Connector import code.loginattempts.LoginAttempt import code.users.Users @@ -123,25 +123,28 @@ class AuthUser extends MegaProtoUser[AuthUser] with MdcLoggable { override def validations = isEmpty(Helper.i18n("Please.enter.your.username")) _ :: valUnique(Helper.i18n("unique.username")) _ :: valUniqueExternally(Helper.i18n("unique.username")) _ :: - super.validations.distinct + super.validations override val fieldId = Some(Text("txtUsername")) /** * Make sure that the field is unique in the CBS */ - def valUniqueExternally(msg: => String)(value: String): List[FieldError] ={ + def valUniqueExternally(msg: => String)(uniqueUsername: String): List[FieldError] ={ if (APIUtil.getPropsAsBoolValue("connector.user.authentication", false)) { - Connector.connector.vend.checkExternalUserExists(value, None).map(_.name) match { - case Full(name) => - value match { - case username if username == name => List(FieldError(this, Text(msg))) // issue 179 - case _ => Nil + Connector.connector.vend.checkExternalUserExists(uniqueUsername, None).map(_.sub) match { + case Full(returnedUsername) => // Get the username via connector + if(uniqueUsername == returnedUsername) { // Username is NOT unique + List(FieldError(this, Text(msg))) // provide the error message + } else { + Nil // All good. Allow username creation } - case ParamFailure(message,_,_,_) if message.contains("NO DATA") => Nil - case _ => List(FieldError(this, Text(msg))) // issue 179 + case ParamFailure(message,_,_,APIFailure(errorMessage, errorCode)) if errorMessage.contains("NO DATA") => // Cannot get the username via connector + Nil // All good. Allow username creation + case _ => // Any other case we provide error message + List(FieldError(this, Text(msg))) } } else { - Nil + Nil // All good. Allow username creation } }