<commit_before>package app

import service._
import jp.sf.amateras.scalatra.forms._

class SettingsController extends SettingsControllerBase with RepositoryService with AccountService


trait SettingsControllerBase extends ControllerBase { self: RepositoryService with AccountService =>

  case class CollaboratorForm(userName: String)

  val form = mapping(
    "userName" -> trim(label("Username", text(required, existUser)))
  )(CollaboratorForm.apply)

  get("/:owner/:repository/settings") {
    val owner      = params("owner")
    val repository = params("repository")
    redirect("/%s/%s/settings/options".format(owner, repository))
  }
  
  get("/:owner/:repository/settings/options") {
    val owner      = params("owner")
    val repository = params("repository")
    
    settings.html.options(getRepository(owner, repository, servletContext).get)
  }
  
  get("/:owner/:repository/settings/collaborators") {
    val owner      = params("owner")
    val repository = params("repository")
    
    settings.html.collaborators(getCollaborators(owner, repository), getRepository(owner, repository, servletContext).get)
  }

  post("/:owner/:repository/settings/collaborators/_add", form) { form =>
    val owner      = params("owner")
    val repository = params("repository")
    addCollaborator(owner, repository, form.userName)
    redirect("/%s/%s/settings/collaborators".format(owner, repository))
  }

  def existUser: Constraint = new Constraint(){
    def validate(name: String, value: String): Option[String] = {
      getAccountByUserName(value) match {
        case None => Some("User does not exist.")
        case Some(x) if(x.userName == context.loginAccount.get.userName) => Some("User can access this repository already.")
        case Some(x) => None
      }
    }
  }

}<commit_msg>Add validation to adding collaborator.<commit_after>package app

import service._
import jp.sf.amateras.scalatra.forms._

class SettingsController extends SettingsControllerBase with RepositoryService with AccountService


trait SettingsControllerBase extends ControllerBase { self: RepositoryService with AccountService =>

  case class CollaboratorForm(userName: String)

  val form = mapping(
    "userName" -> trim(label("Username", text(required, collaborator)))
  )(CollaboratorForm.apply)

  get("/:owner/:repository/settings") {
    val owner      = params("owner")
    val repository = params("repository")
    redirect("/%s/%s/settings/options".format(owner, repository))
  }
  
  get("/:owner/:repository/settings/options") {
    val owner      = params("owner")
    val repository = params("repository")
    
    settings.html.options(getRepository(owner, repository, servletContext).get)
  }
  
  get("/:owner/:repository/settings/collaborators") {
    val owner      = params("owner")
    val repository = params("repository")
    
    settings.html.collaborators(getCollaborators(owner, repository), getRepository(owner, repository, servletContext).get)
  }

  post("/:owner/:repository/settings/collaborators/_add", form) { form =>
    val owner      = params("owner")
    val repository = params("repository")
    addCollaborator(owner, repository, form.userName)
    redirect("/%s/%s/settings/collaborators".format(owner, repository))
  }

  def collaborator: Constraint = new Constraint(){
    def validate(name: String, value: String): Option[String] = {
      getAccountByUserName(value) match {
        case None => Some("User does not exist.")
        case Some(x) if(x.userName == context.loginAccount.get.userName) => Some("User can access this repository already.")
        case Some(x) => {
          val paths = request.getRequestURI.split("/")
          if(getCollaborators(paths(1), paths(2)).contains(x.userName)){
            Some("User can access this repository already.")
          } else {
            None
          }
        }
      }
    }
  }

}
{'fraction_non_alphanumeric': 0.127599486521181, 'fraction_numerical': 0.0005134788189987163, 'mean_word_length': 3.7167070217917675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '664068', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1069, 'n_words': 238}