feature/Add connector method checkExternalUserExists part 3

This commit is contained in:
Marko Milić 2020-08-11 09:43:07 +02:00
parent 56cd54c545
commit 807bbfb133

View File

@ -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
}
}