mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-05 04:56:02 +01:00
Fix validation rules for scalatra-forms
This commit is contained in:
@@ -139,7 +139,7 @@ trait AccountControllerBase extends AccountManagementControllerBase {
|
|||||||
private def accountWebhookEvents = new ValueType[Set[WebHook.Event]]{
|
private def accountWebhookEvents = new ValueType[Set[WebHook.Event]]{
|
||||||
def convert(name: String, params: Map[String, Seq[String]], messages: Messages): Set[WebHook.Event] = {
|
def convert(name: String, params: Map[String, Seq[String]], messages: Messages): Set[WebHook.Event] = {
|
||||||
WebHook.Event.values.flatMap { t =>
|
WebHook.Event.values.flatMap { t =>
|
||||||
params.get(name + "." + t.name).map(_ => t)
|
params.optionValue(name + "." + t.name).map(_ => t)
|
||||||
}.toSet
|
}.toSet
|
||||||
}
|
}
|
||||||
def validate(name: String, params: Map[String, Seq[String]], messages: Messages): Seq[(String, String)] =
|
def validate(name: String, params: Map[String, Seq[String]], messages: Messages): Seq[(String, String)] =
|
||||||
@@ -636,10 +636,14 @@ trait AccountControllerBase extends AccountManagementControllerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private def uniqueRepository: Constraint = new Constraint(){
|
private def uniqueRepository: Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] =
|
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] = {
|
||||||
params.get("owner").flatMap { userName =>
|
for {
|
||||||
getRepositoryNamesOfUser(userName.head).find(_ == value).map(_ => "Repository already exists.")
|
userName <- params.optionValue("owner")
|
||||||
|
_ <- getRepositoryNamesOfUser(userName).find(_ == value)
|
||||||
|
} yield {
|
||||||
|
"Repository already exists."
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def members: Constraint = new Constraint(){
|
private def members: Constraint = new Constraint(){
|
||||||
|
|||||||
@@ -314,10 +314,11 @@ trait AccountManagementControllerBase extends ControllerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected def uniqueMailAddress(paramName: String = ""): Constraint = new Constraint(){
|
protected def uniqueMailAddress(paramName: String = ""): Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] =
|
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] = {
|
||||||
getAccountByMailAddress(value, true)
|
getAccountByMailAddress(value, true)
|
||||||
.filter { x => if(paramName.isEmpty) true else Some(x.userName) != params.get(paramName) }
|
.filter { x => if(paramName.isEmpty) true else Some(x.userName) != params.optionValue(paramName) }
|
||||||
.map { _ => "Mail address is already registered." }
|
.map { _ => "Mail address is already registered." }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val allReservedNames = Set("git", "admin", "upload", "api", "assets", "plugin-assets", "signin", "signout", "register", "activities.atom", "sidebar-collapse", "groups", "new")
|
val allReservedNames = Set("git", "admin", "upload", "api", "assets", "plugin-assets", "signin", "signout", "register", "activities.atom", "sidebar-collapse", "groups", "new")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import gitbucket.core.issues.labels.html
|
|||||||
import gitbucket.core.service.{RepositoryService, AccountService, IssuesService, LabelsService}
|
import gitbucket.core.service.{RepositoryService, AccountService, IssuesService, LabelsService}
|
||||||
import gitbucket.core.util.{ReferrerAuthenticator, WritableUsersAuthenticator}
|
import gitbucket.core.util.{ReferrerAuthenticator, WritableUsersAuthenticator}
|
||||||
import gitbucket.core.util.Implicits._
|
import gitbucket.core.util.Implicits._
|
||||||
|
import gitbucket.core.util.SyntaxSugars._
|
||||||
import org.scalatra.forms._
|
import org.scalatra.forms._
|
||||||
import org.scalatra.i18n.Messages
|
import org.scalatra.i18n.Messages
|
||||||
import org.scalatra.Ok
|
import org.scalatra.Ok
|
||||||
@@ -83,10 +84,10 @@ trait LabelsControllerBase extends ControllerBase {
|
|||||||
|
|
||||||
private def uniqueLabelName: Constraint = new Constraint(){
|
private def uniqueLabelName: Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] = {
|
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] = {
|
||||||
val owner = params("owner").head
|
val owner = params.value("owner")
|
||||||
val repository = params("repository").head
|
val repository = params.value("repository")
|
||||||
params.get("labelId").map { labelId =>
|
params.optionValue("labelId").map { labelId =>
|
||||||
getLabel(owner, repository, value).filter(_.labelId != labelId.head.toInt).map(_ => "Name has already been taken.")
|
getLabel(owner, repository, value).filter(_.labelId != labelId.toInt).map(_ => "Name has already been taken.")
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
getLabel(owner, repository, value).map(_ => "Name has already been taken.")
|
getLabel(owner, repository, value).map(_ => "Name has already been taken.")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import gitbucket.core.issues.priorities.html
|
|||||||
import gitbucket.core.service.{RepositoryService, AccountService, IssuesService, PrioritiesService}
|
import gitbucket.core.service.{RepositoryService, AccountService, IssuesService, PrioritiesService}
|
||||||
import gitbucket.core.util.{ReferrerAuthenticator, WritableUsersAuthenticator}
|
import gitbucket.core.util.{ReferrerAuthenticator, WritableUsersAuthenticator}
|
||||||
import gitbucket.core.util.Implicits._
|
import gitbucket.core.util.Implicits._
|
||||||
|
import gitbucket.core.util.SyntaxSugars._
|
||||||
import org.scalatra.forms._
|
import org.scalatra.forms._
|
||||||
import org.scalatra.i18n.Messages
|
import org.scalatra.i18n.Messages
|
||||||
import org.scalatra.Ok
|
import org.scalatra.Ok
|
||||||
@@ -99,10 +100,10 @@ trait PrioritiesControllerBase extends ControllerBase {
|
|||||||
|
|
||||||
private def uniquePriorityName: Constraint = new Constraint(){
|
private def uniquePriorityName: Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] = {
|
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] = {
|
||||||
val owner = params("owner").head
|
val owner = params.value("owner")
|
||||||
val repository = params("repository").head
|
val repository = params.value("repository")
|
||||||
params.get("priorityId").map { priorityId =>
|
params.optionValue("priorityId").map { priorityId =>
|
||||||
getPriority(owner, repository, value).filter(_.priorityId != priorityId.head.toInt).map(_ => "Name has already been taken.")
|
getPriority(owner, repository, value).filter(_.priorityId != priorityId.toInt).map(_ => "Name has already been taken.")
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
getPriority(owner, repository, value).map(_ => "Name has already been taken.")
|
getPriority(owner, repository, value).map(_ => "Name has already been taken.")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -460,12 +460,15 @@ trait RepositorySettingsControllerBase extends ControllerBase {
|
|||||||
* Duplicate check for the rename repository name.
|
* Duplicate check for the rename repository name.
|
||||||
*/
|
*/
|
||||||
private def renameRepositoryName: Constraint = new Constraint(){
|
private def renameRepositoryName: Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] =
|
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] = {
|
||||||
params.get("repository").filter(_ != value).flatMap { _ =>
|
for {
|
||||||
params.get("owner").flatMap { userName =>
|
repoName <- params.optionValue("repository") if repoName != value
|
||||||
getRepositoryNamesOfUser(userName.head).find(_ == value).map(_ => "Repository already exists.")
|
userName <- params.optionValue("owner")
|
||||||
}
|
_ <- getRepositoryNamesOfUser(userName).find(_ == value)
|
||||||
|
} yield {
|
||||||
|
"Repository already exists."
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ trait WikiControllerBase extends ControllerBase {
|
|||||||
|
|
||||||
private def unique: Constraint = new Constraint(){
|
private def unique: Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] =
|
override def validate(name: String, value: String, params: Map[String, Seq[String]], messages: Messages): Option[String] =
|
||||||
getWikiPageList(params("owner").head, params("repository").head).find(_ == value).map(_ => "Page already exists.")
|
getWikiPageList(params.value("owner"), params.value("repository")).find(_ == value).map(_ => "Page already exists.")
|
||||||
}
|
}
|
||||||
|
|
||||||
private def pagename: Constraint = new Constraint(){
|
private def pagename: Constraint = new Constraint(){
|
||||||
|
|||||||
@@ -53,4 +53,14 @@ object SyntaxSugars {
|
|||||||
def unapply[A, B](t: (A, B)): Option[(A, B)] = Some(t)
|
def unapply[A, B](t: (A, B)): Option[(A, B)] = Some(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides easier and explicit ways to access to a head value of `Map[String, Seq[String]]`.
|
||||||
|
* This is intended to use in implementations of scalatra-forms's `Constraint` or `ValueType`.
|
||||||
|
*/
|
||||||
|
implicit class HeadValueAccessibleMap(map: Map[String, Seq[String]]){
|
||||||
|
def value(key: String): String = map(key).head
|
||||||
|
def optionValue(key: String): Option[String] = map.get(key).flatMap(_.headOption)
|
||||||
|
def values(key: String): Seq[String] = map.get(key).getOrElse(Seq.empty)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user