<commit_before>package controllers

import com.scalableminds.webknossos.datastore.controllers.ValidationHelpers
import com.scalableminds.util.mvc.ExtendedController
import com.scalableminds.util.tools.{Converter, Fox}
import com.typesafe.scalalogging.LazyLogging
import models.basics.Implicits
import models.binary.DataSet
import models.user.User
import net.liftweb.common.{Box, Failure, Full, ParamFailure}
import oxalis.security._
import oxalis.view.ProvidesSessionData
import play.api.i18n.{I18nSupport, Messages, MessagesApi}
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json._
import play.api.mvc.{Request, Result, Controller => PlayController}
import play.twirl.api.Html
import oxalis.security.WebknossosSilhouette.{SecuredAction, SecuredRequest, UserAwareAction, UserAwareRequest}
import reactivemongo.bson.BSONObjectID


trait Controller extends PlayController
  with ExtendedController
  with ProvidesSessionData
  with models.basics.Implicits
  with ValidationHelpers
  with I18nSupport
  with LazyLogging {

  implicit def AuthenticatedRequest2Request[T](r: SecuredRequest[T]): Request[T] =
    r.request

  def ensureTeamAdministration(user: User, team: BSONObjectID) =
    user.isTeamManagerOfBLOCKING(team) ?~> Messages("team.admin.notAllowed")

  def allowedToAdministrate(admin: User, dataSet: DataSet) =
    dataSet.isEditableBy(Some(admin)) ?~> Messages("notAllowed")

  case class Filter[A, T](name: String, predicate: (A, T) => Boolean, default: Option[String] = None)(implicit converter: Converter[String, A]) {
    def applyOn(list: List[T])(implicit request: Request[_]): List[T] = {
      request.getQueryString(name).orElse(default).flatMap(converter.convert) match {
        case Some(attr) => list.filter(predicate(attr, _))
        case _          => list
      }
    }
  }

  case class FilterColl[T](filters: Seq[Filter[_, T]]) {
    def applyOn(list: List[T])(implicit request: Request[_]): List[T] = {
      filters.foldLeft(list) {
        case (l, filter) => filter.applyOn(l)
      }
    }
  }

  def UsingFilters[T, R](filters: Filter[_, T]*)(block: FilterColl[T] => R): R = {
    block(FilterColl(filters))
  }

  def jsonErrorWrites(errors: JsError): JsObject =
    Json.obj(
      "errors" -> errors.errors.map(error =>
        error._2.foldLeft(Json.obj("field" -> error._1.toJsonString)) {
          case (js, e) => js ++ Json.obj("error" -> Messages(e.message))<commit_msg>in ensureTeamAdministration, use nonblocking version of isTeamManager<commit_after>package controllers

import com.scalableminds.webknossos.datastore.controllers.ValidationHelpers
import com.scalableminds.util.mvc.ExtendedController
import com.scalableminds.util.tools.{Converter, Fox}
import com.typesafe.scalalogging.LazyLogging
import models.basics.Implicits
import models.binary.DataSet
import models.user.User
import net.liftweb.common.{Box, Failure, Full, ParamFailure}
import oxalis.security._
import oxalis.view.ProvidesSessionData
import play.api.i18n.{I18nSupport, Messages, MessagesApi}
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json._
import play.api.mvc.{Request, Result, Controller => PlayController}
import play.twirl.api.Html
import oxalis.security.WebknossosSilhouette.{SecuredAction, SecuredRequest, UserAwareAction, UserAwareRequest}
import reactivemongo.bson.BSONObjectID


trait Controller extends PlayController
  with ExtendedController
  with ProvidesSessionData
  with models.basics.Implicits
  with ValidationHelpers
  with I18nSupport
  with LazyLogging {

  implicit def AuthenticatedRequest2Request[T](r: SecuredRequest[T]): Request[T] =
    r.request

  def ensureTeamAdministration(user: User, team: BSONObjectID) =
    user.assertTeamManagerOrAdminOf(team) ?~> Messages("team.admin.notAllowed")

  def allowedToAdministrate(admin: User, dataSet: DataSet) =
    dataSet.isEditableBy(Some(admin)) ?~> Messages("notAllowed")

  case class Filter[A, T](name: String, predicate: (A, T) => Boolean, default: Option[String] = None)(implicit converter: Converter[String, A]) {
    def applyOn(list: List[T])(implicit request: Request[_]): List[T] = {
      request.getQueryString(name).orElse(default).flatMap(converter.convert) match {
        case Some(attr) => list.filter(predicate(attr, _))
        case _          => list
      }
    }
  }

  case class FilterColl[T](filters: Seq[Filter[_, T]]) {
    def applyOn(list: List[T])(implicit request: Request[_]): List[T] = {
      filters.foldLeft(list) {
        case (l, filter) => filter.applyOn(l)
      }
    }
  }

  def UsingFilters[T, R](filters: Filter[_, T]*)(block: FilterColl[T] => R): R = {
    block(FilterColl(filters))
  }

  def jsonErrorWrites(errors: JsError): JsObject =
    Json.obj(
      "errors" -> errors.errors.map(error =>
        error._2.foldLeft(Json.obj("field" -> error._1.toJsonString)) {
          case (js, e) => js ++ Json.obj("error" -> Messages(e.message))
{'fraction_non_alphanumeric': 0.1250757728834108, 'fraction_numerical': 0.003637098403717923, 'mean_word_length': 4.709342560553633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '2113824', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1597, 'n_words': 309}