Skip to content

Commit

Permalink
fix: allow use of email as username
Browse files Browse the repository at this point in the history
Fix #942
  • Loading branch information
ptitFicus committed Feb 26, 2025
1 parent f32d65d commit 7437abc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
6 changes: 3 additions & 3 deletions app/fr/maif/izanami/models/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ object User {
implicit val rightWrite: Writes[Rights] = Json.writes[Rights]

implicit val userReads: Reads[User] = (
(__ \ "username").read[String].filter(name => NAME_REGEXP.pattern.matcher(name).matches()) and
(__ \ "email").read[String].filter(name => NAME_REGEXP.pattern.matcher(name).matches()) and
(__ \ "username").read[String].filter(name => USERNAME_REGEXP.pattern.matcher(name).matches()) and
(__ \ "email").read[String].filter(name => USERNAME_REGEXP.pattern.matcher(name).matches()) and
(__ \ "password").read[String].filter(name => PASSWORD_REGEXP.pattern.matcher(name).matches()) and
(__ \ "admin").readWithDefault[Boolean](false) and
(__ \ "defaultTenant").readNullable[String]
Expand Down Expand Up @@ -527,7 +527,7 @@ object User {
(__ \ "admin").readNullable[Boolean])((rights, admin) => UserRightsUpdateRequest(rights = rights, admin = admin))

implicit val userUpdateReads: Reads[UserInformationUpdateRequest] =
((__ \ "username").read[String].filter(name => NAME_REGEXP.pattern.matcher(name).matches()) and
((__ \ "username").read[String].filter(name => USERNAME_REGEXP.pattern.matcher(name).matches()) and
(__ \ "email").read[String].filter(Constraints.emailAddress.apply(_) == Valid) and
(__ \ "defaultTenant").readNullable[String] and
(__ \ "password").read[String])((name, email, defaultTenant, password) =>
Expand Down
1 change: 1 addition & 0 deletions app/fr/maif/izanami/models/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import scala.util.matching.Regex

package object models {
val NAME_REGEXP: Regex = "^[a-zA-Z0-9_-]+$".r
val USERNAME_REGEXP: Regex = "^[a-zA-Z0-9_\\-!#$%&'*+-/=?^`{|}~@.]+$".r
}
2 changes: 1 addition & 1 deletion app/fr/maif/izanami/web/UserController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class UserController(
val result =
for (
username <-
(request.body \ "username").asOpt[String].filter(name => NAME_REGEXP.pattern.matcher(name).matches());
(request.body \ "username").asOpt[String].filter(name => USERNAME_REGEXP.pattern.matcher(name).matches());
password <-
(request.body \ "password").asOpt[String].filter(name => PASSWORD_REGEXP.pattern.matcher(name).matches());
token <- (request.body \ "token").asOpt[String];
Expand Down
5 changes: 3 additions & 2 deletions izanami-frontend/src/utils/patterns.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const PASSWORD_REGEXP = /^[a-zA-Z0-9_\-+=;:,?!$%'"^@*<>&|#\/\\()\[\]{}]{8,100}$/;
export const USERNAME_REGEXP = /^[a-zA-Z0-9_-]+$/;
export const PASSWORD_REGEXP =
/^[a-zA-Z0-9_\-+=;:,?!$%'"^@*<>&|#\/\\()\[\]{}]{8,100}$/;
export const USERNAME_REGEXP = /^[a-zA-Z0-9_\-!#$%&'*+-/=?^`{|}~@.]+$/;
export const FEATURE_NAME_REGEXP = /^[ a-zA-Z0-9_\-:]+$/;
export const TENANT_NAME_REGEXP = /^[a-z0-9-]+$/;
export const KEY_NAME_REGEXP = /^[a-zA-Z0-9-_]+$/;
Expand Down
9 changes: 9 additions & 0 deletions test/fr/maif/izanami/api/UsersAPISpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ class UsersAPISpec extends BaseAPISpec {
(response.json.get \ "password").asOpt[String] mustBe None
}

"allow user creation with mail as username" in {
val situation = TestSituationBuilder().loggedInWithAdminRights().build()
val response = situation.createUser(user = "[email protected]", password = "12345678")

response.status mustBe CREATED
(response.json.get \ "username").as[String] mustEqual "[email protected]"
(response.json.get \ "password").asOpt[String] mustBe None
}

"prevent user creation is username or password is too long" in {
val situation = TestSituationBuilder().loggedInWithAdminRights().build()

Expand Down

0 comments on commit 7437abc

Please sign in to comment.