mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
Validation Framework completed mostly.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import util.Directory._
|
import util.Directory._
|
||||||
|
import util.Validations._
|
||||||
import org.scalatra._
|
import org.scalatra._
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
@@ -23,11 +24,8 @@ class CreateRepositoryServlet extends ServletBase {
|
|||||||
* Create new repository.
|
* Create new repository.
|
||||||
*/
|
*/
|
||||||
post("/") {
|
post("/") {
|
||||||
withValidation(validate, params){
|
withValidation(form, params){ form =>
|
||||||
val repositoryName = params("name")
|
val gitdir = getRepositoryDir(LoginUser, form.name)
|
||||||
val description = params("description")
|
|
||||||
|
|
||||||
val gitdir = getRepositoryDir(LoginUser, repositoryName)
|
|
||||||
val repository = new RepositoryBuilder().setGitDir(gitdir).setBare.build
|
val repository = new RepositoryBuilder().setGitDir(gitdir).setBare.build
|
||||||
|
|
||||||
repository.create
|
repository.create
|
||||||
@@ -36,16 +34,16 @@ class CreateRepositoryServlet extends ServletBase {
|
|||||||
config.setBoolean("http", null, "receivepack", true)
|
config.setBoolean("http", null, "receivepack", true)
|
||||||
config.save
|
config.save
|
||||||
|
|
||||||
val tmpdir = getInitRepositoryDir(LoginUser, repositoryName)
|
val tmpdir = getInitRepositoryDir(LoginUser, form.name)
|
||||||
try {
|
try {
|
||||||
// Clone the repository
|
// Clone the repository
|
||||||
Git.cloneRepository.setURI(gitdir.toURI.toString).setDirectory(tmpdir).call
|
Git.cloneRepository.setURI(gitdir.toURI.toString).setDirectory(tmpdir).call
|
||||||
|
|
||||||
// Create README.md
|
// Create README.md
|
||||||
FileUtils.writeStringToFile(new File(tmpdir, "README.md"), if(description.nonEmpty){
|
FileUtils.writeStringToFile(new File(tmpdir, "README.md"), if(form.description.nonEmpty){
|
||||||
repositoryName + "\n===============\n\n" + description
|
form.name + "\n===============\n\n" + form.description
|
||||||
} else {
|
} else {
|
||||||
repositoryName + "\n===============\n"
|
form.name + "\n===============\n"
|
||||||
}, "UTF-8")
|
}, "UTF-8")
|
||||||
|
|
||||||
val git = Git.open(tmpdir)
|
val git = Git.open(tmpdir)
|
||||||
@@ -58,25 +56,32 @@ class CreateRepositoryServlet extends ServletBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// redirect to the repository
|
// redirect to the repository
|
||||||
redirect("/%s/%s".format(LoginUser, repositoryName))
|
redirect("/%s/%s".format(LoginUser, form.name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get("/validate") {
|
get("/validate") {
|
||||||
contentType = "application/json"
|
contentType = "application/json"
|
||||||
validate(params).toJSON
|
form.validateAsJSON(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
def validate(params: Map[String, String]): ValidationResult = {
|
val form = Form(
|
||||||
val name = params("name")
|
"name" -> trim(label("Repository name", text(required, maxlength(40), repository))),
|
||||||
if(name.isEmpty){
|
"description" -> trim(label("Description" , text()))
|
||||||
ValidationResult(false, Map("name" -> "Repository name is required."))
|
)(RepositoryCreationForm.apply)
|
||||||
} else if(!name.matches("^[a-z0-6\\-_]+$")){
|
|
||||||
ValidationResult(false, Map("name" -> "Repository name contans invalid character."))
|
def repository: Constraint = new Constraint(){
|
||||||
} else if(getRepositories(LoginUser).contains(name)){
|
def validate(name: String, value: String): Option[String] = {
|
||||||
ValidationResult(false, Map("name" -> "Repository already exists."))
|
if(!value.matches("^[a-z0-9\\-_]+$")){
|
||||||
|
Some("Repository name contains invalid character.")
|
||||||
|
} else if(getRepositories(LoginUser).contains(value)){
|
||||||
|
Some("Repository already exists.")
|
||||||
} else {
|
} else {
|
||||||
ValidationResult(true, Map.empty)
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case class RepositoryCreationForm(name: String, description: String)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -15,22 +15,4 @@ abstract class ServletBase extends ScalatraServlet with JacksonJsonSupport {
|
|||||||
// TODO get from session
|
// TODO get from session
|
||||||
val LoginUser = System.getProperty("user.name")
|
val LoginUser = System.getProperty("user.name")
|
||||||
|
|
||||||
protected def withValidation(validator: Map[String, String] => ValidationResult, params: Map[String, String])(action: => Any): Any = {
|
|
||||||
validator(params).valid match {
|
|
||||||
case true => action
|
|
||||||
case false => throw new RuntimeException("Invalid Request") // TODO show error page?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case class ValidationResult(valid: Boolean, errors: Map[String, String]){
|
|
||||||
def toJSON(): JObject = {
|
|
||||||
JObject(
|
|
||||||
"valid" -> JBool(valid),
|
|
||||||
"errors" -> JObject(errors.map { case (key, value) =>
|
|
||||||
JField(key, JString(value))
|
|
||||||
}.toList)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
118
src/main/scala/util/Validations.scala
Normal file
118
src/main/scala/util/Validations.scala
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import org.json4s._
|
||||||
|
import org.json4s.jackson._
|
||||||
|
|
||||||
|
object Validations {
|
||||||
|
|
||||||
|
def withValidation[T](form: Form[T], params: Map[String, String])(action: T => Any): Any = {
|
||||||
|
form.validate(params).isEmpty match {
|
||||||
|
case true => action(form.create(params))
|
||||||
|
case false => throw new RuntimeException("Invalid Request") // TODO show error page?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def Form[T, P1](f1: (String, ValueType[P1]))(factory: (P1) => T): Form[T] = new Form[T]{
|
||||||
|
def fields = Seq(f1)
|
||||||
|
def create(params: Map[String, String]) = factory(f1._2.convert(params(f1._1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
def Form[T, P1, P2](f1: (String, ValueType[P1]), f2: (String, ValueType[P2]))(factory: (P1, P2) => T): Form[T] = new Form[T]{
|
||||||
|
def fields = Seq(f1, f2)
|
||||||
|
def create(params: Map[String, String]) = factory(f1._2.convert(params(f1._1)), f2._2.convert(params(f2._1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
def Form[T, P1, P2, P3](f1: (String, ValueType[P1]), f2: (String, ValueType[P2]), f3: (String, ValueType[P3]))(factory: (P1, P2, P3) => T): Form[T] = new Form[T]{
|
||||||
|
def fields = Seq(f1, f2)
|
||||||
|
def create(params: Map[String, String]) = factory(f1._2.convert(params(f1._1)), f2._2.convert(params(f2._1)), f3._2.convert(params(f3._1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class Form[T] {
|
||||||
|
|
||||||
|
def fields: Seq[(String, ValueType[_])]
|
||||||
|
|
||||||
|
def create(params: Map[String, String]): T
|
||||||
|
|
||||||
|
def validate(params: Map[String, String]): Map[String, String] = {
|
||||||
|
fields.map { case (name, valueType) =>
|
||||||
|
valueType.validate(name, params(name)) match {
|
||||||
|
case Some(message) => Some(name, message)
|
||||||
|
case None => None
|
||||||
|
}
|
||||||
|
}.flatten.toMap
|
||||||
|
}
|
||||||
|
|
||||||
|
def validateAsJSON(params: Map[String, String]): JObject = {
|
||||||
|
JObject(validate(params).map { case (key, value) =>
|
||||||
|
JField(key, JString(value))
|
||||||
|
}.toList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Constraints
|
||||||
|
|
||||||
|
trait Constraint {
|
||||||
|
def validate(name: String, value: String): Option[String]
|
||||||
|
}
|
||||||
|
|
||||||
|
def required: Constraint = new Constraint(){
|
||||||
|
def validate(name: String, value: String): Option[String] =
|
||||||
|
if(value.isEmpty) Some("%s is required.".format(name)) else None
|
||||||
|
}
|
||||||
|
|
||||||
|
def required(message: String): Constraint = new Constraint(){
|
||||||
|
def validate(name: String, value: String): Option[String] =
|
||||||
|
if(value.isEmpty) Some(message) else None
|
||||||
|
}
|
||||||
|
|
||||||
|
def maxlength(length: Int): Constraint = new Constraint(){
|
||||||
|
def validate(name: String, value: String): Option[String] =
|
||||||
|
if(value.length > length) Some("%s cannot be longer than %d characters.".format(name, length)) else None
|
||||||
|
}
|
||||||
|
|
||||||
|
def minlength(length: Int): Constraint = new Constraint(){
|
||||||
|
def validate(name: String, value: String): Option[String] =
|
||||||
|
if(value.length < length) Some("%s cannot be shorter than %d characters".format(name, length)) else None
|
||||||
|
}
|
||||||
|
|
||||||
|
def pattern(pattern: String, message: String = ""): Constraint = new Constraint {
|
||||||
|
def validate(name: String, value: String): Option[String] =
|
||||||
|
if(!value.matches("^" + pattern + "$")){
|
||||||
|
if(message.isEmpty) Some("%s must be '%s'.".format(name, pattern)) else Some(message)
|
||||||
|
} else None
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// ValueTypes
|
||||||
|
|
||||||
|
abstract class ValueType[T](constraints: Constraint*) {
|
||||||
|
|
||||||
|
def convert(value: String): T
|
||||||
|
|
||||||
|
def validate(name: String, value: String): Option[String] = {
|
||||||
|
constraints.map(_.validate(name, value)).flatten.headOption
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def text(constraints: Constraint*): ValueType[String] = new ValueType[String](constraints: _*){
|
||||||
|
def convert(value: String): String = value
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// ValueType wrappers to provide additional features.
|
||||||
|
|
||||||
|
def trim[T](valueType: ValueType[T]): ValueType[T] = new ValueType[T](){
|
||||||
|
def convert(value: String): T = valueType.convert(value.trim)
|
||||||
|
override def validate(name: String, value: String): Option[String] = valueType.validate(name, value.trim)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
def label[T](label: String, valueType: ValueType[T]): ValueType[T] = new ValueType[T](){
|
||||||
|
def convert(value: String): T = valueType.convert(value.trim)
|
||||||
|
override def validate(name: String, value: String): Option[String] = valueType.validate(label, value.trim)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,11 +16,11 @@ function validate(e){
|
|||||||
// clear all error messages
|
// clear all error messages
|
||||||
$('.error-message').text('');
|
$('.error-message').text('');
|
||||||
|
|
||||||
if(data.valid){
|
if($.isEmptyObject(data)){
|
||||||
form.data('validated', true);
|
form.data('validated', true);
|
||||||
form.submit();
|
form.submit();
|
||||||
} else {
|
} else {
|
||||||
$.each(data.errors, function(key, value){
|
$.each(data, function(key, value){
|
||||||
$('#error-' + key).text(value);
|
$('#error-' + key).text(value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user