diff --git a/src/main/scala/app/ControllerBase.scala b/src/main/scala/app/ControllerBase.scala index 1cc5f13..c1189ee 100644 --- a/src/main/scala/app/ControllerBase.scala +++ b/src/main/scala/app/ControllerBase.scala @@ -34,6 +34,17 @@ url.substring(0, url.length - request.getRequestURI.length) } + protected def identifier: Constraint = new Constraint(){ + def validate(name: String, value: String): Option[String] = + if(!value.matches("^[a-zA-Z0-9\\-_]+$")){ + Some("%s contains invalid character.".format(name)) + } else if(value.startsWith("_") || value.startsWith("-")){ + Some("%s starts with invalid character.".format(name)) + } else { + None + } + } + } case class Context(path: String, loginAccount: Option[Account]) \ No newline at end of file diff --git a/src/main/scala/app/CreateRepositoryController.scala b/src/main/scala/app/CreateRepositoryController.scala index 17d4d50..0430077 100644 --- a/src/main/scala/app/CreateRepositoryController.scala +++ b/src/main/scala/app/CreateRepositoryController.scala @@ -21,7 +21,7 @@ case class RepositoryCreationForm(name: String, description: Option[String]) val form = mapping( - "name" -> trim(label("Repository name", text(required, maxlength(40), repository))), + "name" -> trim(label("Repository name", text(required, maxlength(40), identifier, unique))), "description" -> trim(label("Description" , optional(text()))) )(RepositoryCreationForm.apply) @@ -81,20 +81,11 @@ }) /** - * Constraint for the repository name. + * Duplicate check for the repository name. */ - def repository: Constraint = new Constraint(){ - def validate(name: String, value: String): Option[String] = { - if(!value.matches("^[a-zA-Z0-9\\-_]+$")){ - Some("Repository name contains invalid character.") - } else if(value.startsWith("_") || value.startsWith("-")){ - Some("Repository name starts with invalid character.") - } else if(getRepositoryNamesOfUser(context.loginAccount.get.userName).contains(value)){ - Some("Repository already exists.") - } else { - None - } - } + private def unique: Constraint = new Constraint(){ + def validate(name: String, value: String): Option[String] = + getRepositoryNamesOfUser(context.loginAccount.get.userName).find(_ == value).map(_ => "Repository already exists.") } } \ No newline at end of file diff --git a/src/main/scala/app/SettingsController.scala b/src/main/scala/app/SettingsController.scala index 459c883..88637fc 100644 --- a/src/main/scala/app/SettingsController.scala +++ b/src/main/scala/app/SettingsController.scala @@ -130,7 +130,7 @@ /** * Provides Constraint to validate the collaborator name. */ - def collaborator: Constraint = new Constraint(){ + private def collaborator: Constraint = new Constraint(){ def validate(name: String, value: String): Option[String] = { getAccountByUserName(value) match { case None => Some("User does not exist.") diff --git a/src/main/scala/app/UsersController.scala b/src/main/scala/app/UsersController.scala index cd5d0df..0e4f732 100644 --- a/src/main/scala/app/UsersController.scala +++ b/src/main/scala/app/UsersController.scala @@ -12,7 +12,7 @@ case class UserForm(userName: String, password: String, mailAddress: String, isAdmin: Boolean, url: Option[String]) val newForm = mapping( - "userName" -> trim(label("Username" , text(required, maxlength(100), username, unique))), + "userName" -> trim(label("Username" , text(required, maxlength(100), identifier, unique))), "password" -> trim(label("Password" , text(required, maxlength(100)))), "mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))), "isAdmin" -> trim(label("User Type" , boolean())), @@ -20,7 +20,7 @@ )(UserForm.apply) val editForm = mapping( - "userName" -> trim(label("Username" , text(required, maxlength(100), username))), + "userName" -> trim(label("Username" , text(required, maxlength(100), identifier))), "password" -> trim(label("Password" , text(required, maxlength(100)))), "mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))), "isAdmin" -> trim(label("User Type" , boolean())), @@ -68,17 +68,6 @@ redirect("/admin/users") }) - private def username: Constraint = new Constraint(){ - def validate(name: String, value: String): Option[String] = - if(!value.matches("^[a-zA-Z0-9\\-_]+$")){ - Some("Username contains invalid character.") - } else if(value.startsWith("_") || value.startsWith("-")){ - Some("Username starts with invalid character.") - } else { - None - } - } - private def unique: Constraint = new Constraint(){ def validate(name: String, value: String): Option[String] = getAccountByUserName(value).map { _ => "User already exists." } diff --git a/src/main/scala/app/WikiController.scala b/src/main/scala/app/WikiController.scala index ecc9305..cb709e5 100644 --- a/src/main/scala/app/WikiController.scala +++ b/src/main/scala/app/WikiController.scala @@ -14,14 +14,14 @@ case class WikiPageEditForm(pageName: String, content: String, message: Option[String], currentPageName: String) val newForm = mapping( - "pageName" -> trim(label("Page name" , text(required, maxlength(40), pageName, unique))), + "pageName" -> trim(label("Page name" , text(required, maxlength(40), identifier, unique))), "content" -> trim(label("Content" , text(required))), "message" -> trim(label("Message" , optional(text()))), "currentPageName" -> trim(label("Current page name" , text())) )(WikiPageEditForm.apply) val editForm = mapping( - "pageName" -> trim(label("Page name" , text(required, maxlength(40), pageName))), + "pageName" -> trim(label("Page name" , text(required, maxlength(40), identifier))), "content" -> trim(label("Content" , text(required))), "message" -> trim(label("Message" , optional(text()))), "currentPageName" -> trim(label("Current page name" , text(required))) @@ -176,22 +176,7 @@ } }) - /** - * Constraint for the wiki page name. - */ - def pageName: Constraint = new Constraint(){ - def validate(name: String, value: String): Option[String] = { - if(!value.matches("^[a-zA-Z0-9\\-_]+$")){ - Some("Page name contains invalid character.") - } else if(value.startsWith("_") || value.startsWith("-")){ - Some("Page name starts with invalid character.") - } else { - None - } - } - } - - def isWritable(owner: String, repository: String): Boolean = { + private def isWritable(owner: String, repository: String): Boolean = { context.loginAccount match { case Some(a) if(a.isAdmin) => true case Some(a) if(a.userName == owner) => true @@ -199,15 +184,10 @@ case _ => false } } - - def unique: Constraint = new Constraint(){ - def validate(name: String, value: String): Option[String] = { - if(getWikiPageList(params("owner"), params("repository")).contains(value)){ - Some("Page already exists.") - } else { - None - } - } + + private def unique: Constraint = new Constraint(){ + def validate(name: String, value: String): Option[String] = + getWikiPageList(params("owner"), params("repository")).find(_ == value).map(_ => "Page already exists.") } } \ No newline at end of file