Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webapi): unique username/email check on change user #1561

Merged
merged 19 commits into from Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
98b8ba1
test(webapi): add unique username/email check to change user
LukasStoeckli Dec 17, 2019
4cb2450
Merge branch 'develop' into wip/fix-change-user
LukasStoeckli Dec 23, 2019
0b4cdee
fix(webapi): fix unique username and email check
LukasStoeckli Dec 23, 2019
1c8899e
fix(webapi): remove mean blocking awaits
LukasStoeckli Jan 7, 2020
4571343
fix(webapi): remove unnecessary comparison in change user
LukasStoeckli Jan 7, 2020
2901f7f
refactor(webapi): remove unused import
LukasStoeckli Jan 7, 2020
70a8759
Merge branch 'develop' into wip/fix-change-user
LukasStoeckli Jan 10, 2020
7c147bf
Merge branch 'develop' into wip/fix-change-user
subotic Feb 7, 2020
0985159
fix(webapi):check if requested email and username equal current values
LukasStoeckli Apr 27, 2020
ac7d4f7
merge develop into wip/fix-change-user
LukasStoeckli Apr 28, 2020
44e068c
fix(webapi): replace '""' with 'None' for values that don't exist
LukasStoeckli Apr 29, 2020
e8a4a8a
Merge branch 'develop' into wip/fix-change-user
Apr 30, 2020
2bcb8c9
Merge branch 'develop' into wip/fix-change-user
May 6, 2020
3e19738
Merge branch 'develop' into wip/fix-change-user
Jun 4, 2020
ec1f67c
refactor(UsersResponderADM): Avoid using Option.get.
Jun 4, 2020
908bafd
Merge branch 'develop' into wip/fix-change-user
Jun 9, 2020
3ce6c3f
test: Fix tests by making error messages consistent.
Jun 24, 2020
f83c5ac
Merge branch 'develop' into wip/fix-change-user
Jun 24, 2020
431b6e0
Merge branch 'develop' into wip/fix-change-user
Jun 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -265,32 +265,15 @@ class UsersResponderADM(responderData: ResponderData) extends Responder(responde
}

// check if we want to change the email
_ = if (changeUserRequest.email.isDefined) {
benjamingeer marked this conversation as resolved.
Show resolved Hide resolved
stringFormatter.validateEmailAndThrow(changeUserRequest.email.get, throw BadRequestException(s"The email: '${changeUserRequest.email.get}' is invalid"))
currentUserInformation.map { user =>
// check if current email differs from the one in the change request
if (!user.email.equals(changeUserRequest.email.get)) {
// check if new email is free
userByEmailExists(changeUserRequest.email.get).map(result =>
if (result) throw DuplicateValueException("A user with this email exists already. Cannot change")
)
}
}
emailTaken: Boolean <- userByEmailExists(changeUserRequest.email.getOrElse(""), currentUserInformation.get.email)
_ = if (emailTaken) {
throw DuplicateValueException(s"User with the email: '${changeUserRequest.email.get}' already exists")
}

// check if we want to change the username
_ = if (changeUserRequest.username.isDefined) {
stringFormatter.validateUsername(changeUserRequest.username.get, throw BadRequestException(s"The username: '${changeUserRequest.username.get}' contains invalid characters"))
currentUserInformation.map { user =>
// check if the current username differs from the one in the change request
if (!user.username.equals(changeUserRequest.username.get)) {
// check if new username is free
userByUsernameExists(changeUserRequest.email.get).map(result =>
if (result) throw DuplicateValueException("A user with this username exists already. Cannot change")
)
}

}
usernameTaken: Boolean <- userByUsernameExists(changeUserRequest.username.getOrElse(""), currentUserInformation.get.username)
_ = if (usernameTaken) {
throw DuplicateValueException(s"User with the username: '${changeUserRequest.username.get}' already exists")
}

userUpdatePayload = UserUpdatePayloadADM(
Expand Down Expand Up @@ -1420,34 +1403,50 @@ class UsersResponderADM(responderData: ResponderData) extends Responder(responde
* Helper method for checking if an username is already registered.
*
* @param username the username of the user.
* @param current the current username of the user.
* @return a [[Boolean]].
*/
private def userByUsernameExists(username: String): Future[Boolean] = {
for {
askString <- Future(queries.sparql.admin.txt.checkUserExistsByUsername(username = username).toString)
// _ = log.debug("userExists - query: {}", askString)

checkUserExistsResponse <- (storeManager ? SparqlAskRequest(askString)).mapTo[SparqlAskResponse]
result = checkUserExistsResponse.result

} yield result
private def userByUsernameExists(username: String, current: String = ""): Future[Boolean] = {
LukasStoeckli marked this conversation as resolved.
Show resolved Hide resolved
username match {
case "" => FastFuture.successful(false)
case `current` => FastFuture.successful(true)
case _ => {
stringFormatter.validateUsername(username, throw BadRequestException(s"The username: '${username}' contains invalid characters"))
for {
askString <- Future(queries.sparql.admin.txt.checkUserExistsByUsername(username = username).toString)
// _ = log.debug("userExists - query: {}", askString)

checkUserExistsResponse <- (storeManager ? SparqlAskRequest(askString)).mapTo[SparqlAskResponse]
result = checkUserExistsResponse.result

} yield result
}
}
}

/**
* Helper method for checking if an email is already registered.
*
* @param email the email of the user.
* @param current the current email of the user.
* @return a [[Boolean]].
*/
private def userByEmailExists(email: String): Future[Boolean] = {
for {
askString <- Future(queries.sparql.admin.txt.checkUserExistsByEmail(email = email).toString)
// _ = log.debug("userExists - query: {}", askString)

checkUserExistsResponse <- (storeManager ? SparqlAskRequest(askString)).mapTo[SparqlAskResponse]
result = checkUserExistsResponse.result

} yield result
private def userByEmailExists(email: String, current: String = ""): Future[Boolean] = {
LukasStoeckli marked this conversation as resolved.
Show resolved Hide resolved
email match {
case "" => FastFuture.successful(false)
case `current` => FastFuture.successful(true)
case _ => {
stringFormatter.validateEmailAndThrow(email, throw BadRequestException(s"The email: '${email}' is invalid"))
for {
askString <- Future(queries.sparql.admin.txt.checkUserExistsByEmail(email = email).toString)
// _ = log.debug("userExists - query: {}", askString)

checkUserExistsResponse <- (storeManager ? SparqlAskRequest(askString)).mapTo[SparqlAskResponse]
result = checkUserExistsResponse.result

} yield result
}
}
}

/**
Expand Down
Expand Up @@ -388,6 +388,30 @@ class UsersResponderADMSpec extends CoreSpec(UsersResponderADMSpec.config) with
response3.user.familyName should equal (SharedTestDataADM.normalUser.familyName)

}

"return a 'DuplicateValueException' if the supplied 'username' is not unique" in {
responderManager ! UserChangeBasicUserInformationRequestADM(
userIri = SharedTestDataADM.normalUser.id,
changeUserRequest = ChangeUserApiRequestADM(
username = Some("root")
),
SharedTestDataADM.superUser,
UUID.randomUUID
)
expectMsg(Failure(DuplicateValueException(s"User with the username: 'root' already exists")))
}

"return a 'DuplicateValueException' if the supplied 'email' is not unique" in {
responderManager ! UserChangeBasicUserInformationRequestADM(
userIri = SharedTestDataADM.normalUser.id,
changeUserRequest = ChangeUserApiRequestADM(
email = Some("root@example.com")
),
SharedTestDataADM.superUser,
UUID.randomUUID
)
expectMsg(Failure(DuplicateValueException(s"User with the email: 'root@example.com' already exists")))
}

"return 'BadRequest' if the new 'username' contains invalid characters (@)" in {

Expand Down