<commit_before>/*
* Copyright (C) 2011-2017 scalable minds UG (haftungsbeschränkt) & Co. KG. <http://scm.io>
*/
package com.scalableminds.braingames.datastore.controllers

import java.io.FileInputStream

import com.google.protobuf.CodedInputStream
import com.scalableminds.braingames.datastore.services.{AccessTokenService, UserAccessRequest}
import com.scalableminds.util.mvc.ExtendedController
import com.trueaccord.scalapb.json.JsonFormat
import com.trueaccord.scalapb.{GeneratedMessage, GeneratedMessageCompanion, Message}
import com.typesafe.scalalogging.LazyLogging
import net.liftweb.common.Box
import net.liftweb.util.Helpers.tryo
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json.{JsError, Reads}
import play.api.mvc.Results.BadRequest
import play.api.mvc.{Controller => PlayController, _}
import play.api.{Mode, Play}

import scala.concurrent.Future

trait Controller
  extends PlayController
    with ExtendedController
    with RemoteOriginHelpers
    with ValidationHelpers
    with LazyLogging

trait TokenSecuredController extends Controller {

  def accessTokenService: AccessTokenService

  case class TokenSecuredAction(accessRequest: UserAccessRequest) extends ActionBuilder[Request] {

    val debugModeEnabled = Play.current.configuration.getBoolean("datastore.debugMode").getOrElse(false)

    private def hasUserAccess[A](implicit request: Request[A]): Future[Boolean] = {
      // TODO RocksDB if (debugModeEnabled && Play.mode(Play.current) != Mode.Prod) {
      //  return Future.successful(true)
      //}

      request.getQueryString("token").map { token =>
        accessTokenService.hasUserAccess(token, accessRequest)
      }.getOrElse(Future.successful(false))
    }

    def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]): Future[Result] = {
      hasUserAccess(request).flatMap {
        case true =>
          block(request)
        case false =>
          Future.successful(Forbidden("Invalid access token."))
      }
    }
  }
}

trait RemoteOriginHelpers {

  def AllowRemoteOrigin(f: => Future[Result]): Future[Result] =
    f.map(addHeadersToResult)

  def AllowRemoteOrigin(f: => Result): Result =
    addHeadersToResult(f)

  def addHeadersToResult(result: Result): Result =
    result.withHeaders("Access-Control-Allow-Origin" -> "*", "Access-Control-Max-Age" -> "600")

  case class AllowRemoteOrigin[A](action: Action[A]) extends Action[A] {
<commit_msg>improved error message<commit_after>/*
* Copyright (C) 2011-2017 scalable minds UG (haftungsbeschränkt) & Co. KG. <http://scm.io>
*/
package com.scalableminds.braingames.datastore.controllers

import java.io.FileInputStream

import com.google.protobuf.CodedInputStream
import com.scalableminds.braingames.datastore.services.{AccessTokenService, UserAccessRequest}
import com.scalableminds.util.mvc.ExtendedController
import com.trueaccord.scalapb.{GeneratedMessage, GeneratedMessageCompanion, Message}
import com.typesafe.scalalogging.LazyLogging
import net.liftweb.common.Box
import net.liftweb.util.Helpers.tryo
import play.api.Play
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json.{JsError, Reads}
import play.api.mvc.Results.BadRequest
import play.api.mvc.{Controller => PlayController, _}

import scala.concurrent.Future

trait Controller
  extends PlayController
    with ExtendedController
    with RemoteOriginHelpers
    with ValidationHelpers
    with LazyLogging

trait TokenSecuredController extends Controller {

  def accessTokenService: AccessTokenService

  case class TokenSecuredAction(accessRequest: UserAccessRequest) extends ActionBuilder[Request] {

    val debugModeEnabled = Play.current.configuration.getBoolean("datastore.debugMode").getOrElse(false)

    private def hasUserAccess[A](implicit request: Request[A]): Future[Boolean] = {
      // TODO RocksDB if (debugModeEnabled && Play.mode(Play.current) != Mode.Prod) {
      //  return Future.successful(true)
      //}

      request.getQueryString("token").map { token =>
        accessTokenService.hasUserAccess(token, accessRequest)
      }.getOrElse(Future.successful(false))
    }

    def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]): Future[Result] = {
      hasUserAccess(request).flatMap {
        case true =>
          block(request)
        case false =>
          Future.successful(Forbidden("No access token."))
      }
    }
  }
}

trait RemoteOriginHelpers {

  def AllowRemoteOrigin(f: => Future[Result]): Future[Result] =
    f.map(addHeadersToResult)

  def AllowRemoteOrigin(f: => Result): Result =
    addHeadersToResult(f)

  def addHeadersToResult(result: Result): Result =
    result.withHeaders("Access-Control-Allow-Origin" -> "*", "Access-Control-Max-Age" -> "600")

  case class AllowRemoteOrigin[A](action: Action[A]) extends Action[A] {

{'fraction_non_alphanumeric': 0.11241265926839293, 'fraction_numerical': 0.004521167283189478, 'mean_word_length': 4.55593607305936, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '534533', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1444, 'n_words': 273}