<commit_before>package controllers

import com.gilt.apidoc.models.{Organization, User}
import db.{MembershipsDao, UsersDao}
import play.api.mvc._
import play.api.mvc.Results.Unauthorized
import scala.concurrent.Future
import util.BasicAuthorization

private[controllers] case class UserAuth(

  /**
    * The user as identified by the api token
    */
  tokenUser: Option[User],

  /**
    * The user as identified by the UserGuidHeader
    */
  user: Option[User]
)


private[controllers] object RequestHelper {

  val UserGuidHeader = "X-User-Guid"
  val AuthorizationHeader = "Authorization"

  def userAuth(
    authorizationHeader: Option[String],
    userGuidHeader: Option[String]
  ): UserAuth = {

    val tokenUser: Option[User] = BasicAuthorization.get(authorizationHeader) match {
      case Some(auth: BasicAuthorization.Token) => {
        UsersDao.findByToken(auth.token)
      }
      case _ => None
    }

    val userFromHeader = userGuidHeader.flatMap(UsersDao.findByGuid(_))
    UserAuth(tokenUser, userFromHeader)

  }


}

class AnonymousRequest[A](val tokenUser: Option[User], val user: Option[User], request: Request[A]) extends WrappedRequest[A](request)

object AnonymousRequest extends ActionBuilder[AnonymousRequest] {

  def invokeBlock[A](request: Request[A], block: (AnonymousRequest[A]) => Future[Result]) = {
    val userAuth = RequestHelper.userAuth(
      request.headers.get(RequestHelper.AuthorizationHeader),
      request.headers.get(RequestHelper.UserGuidHeader)
    )

    block(
      new AnonymousRequest(
        tokenUser = userAuth.tokenUser,
        user = userAuth.user,
        request = request
      )
    )
  }

}

class AuthenticatedRequest[A](val tokenUser: User, val user: User, request: Request[A]) extends WrappedRequest[A](request) {

  def requireAdmin(org: Organization) {
    require(MembershipsDao.isUserAdmin(user, org), s"Action requires admin role. User[${user.guid}] is not an admin of Org[${org.key}]")
  }

  def requireMember(org: Organization) {
    require(MembershipsDao.isUserAdmin(user, org), s"Action requires admin role. User[${user.guid}] is not an admin of Org[${org.key}]")
  }

}

object Authenticated extends ActionBuilder[AuthenticatedRequest] {

  def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]) = {
    val userAuth = RequestHelper.userAuth(
      request.headers.get(RequestHelper.AuthorizationHeader),
      request.headers.get(RequestHelper.UserGuidHeader)
    )

    userAuth.user match {
      case None => {
        Future.successful(Unauthorized(s"Failed basic authorization or missing ${RequestHelper.UserGuidHeader} header"))
      }

      case Some(user) => {
        block(new AuthenticatedRequest(userAuth.tokenUser.get, user, request))
      }
    }
  }

}<commit_msg>Fix permissions check inconsistency when deleting a service<commit_after>package controllers

import com.gilt.apidoc.models.{Organization, User}
import db.{MembershipsDao, UsersDao}
import play.api.mvc._
import play.api.mvc.Results.Unauthorized
import scala.concurrent.Future
import util.BasicAuthorization

private[controllers] case class UserAuth(

  /**
    * The user as identified by the api token
    */
  tokenUser: Option[User],

  /**
    * The user as identified by the UserGuidHeader
    */
  user: Option[User]
)


private[controllers] object RequestHelper {

  val UserGuidHeader = "X-User-Guid"
  val AuthorizationHeader = "Authorization"

  def userAuth(
    authorizationHeader: Option[String],
    userGuidHeader: Option[String]
  ): UserAuth = {

    val tokenUser: Option[User] = BasicAuthorization.get(authorizationHeader) match {
      case Some(auth: BasicAuthorization.Token) => {
        UsersDao.findByToken(auth.token)
      }
      case _ => None
    }

    val userFromHeader = userGuidHeader.flatMap(UsersDao.findByGuid(_))
    UserAuth(tokenUser, userFromHeader)

  }


}

class AnonymousRequest[A](val tokenUser: Option[User], val user: Option[User], request: Request[A]) extends WrappedRequest[A](request)

object AnonymousRequest extends ActionBuilder[AnonymousRequest] {

  def invokeBlock[A](request: Request[A], block: (AnonymousRequest[A]) => Future[Result]) = {
    val userAuth = RequestHelper.userAuth(
      request.headers.get(RequestHelper.AuthorizationHeader),
      request.headers.get(RequestHelper.UserGuidHeader)
    )

    block(
      new AnonymousRequest(
        tokenUser = userAuth.tokenUser,
        user = userAuth.user,
        request = request
      )
    )
  }

}

class AuthenticatedRequest[A](val tokenUser: User, val user: User, request: Request[A]) extends WrappedRequest[A](request) {

  def requireAdmin(org: Organization) {
    require(MembershipsDao.isUserAdmin(user, org), s"Action requires admin role. User[${user.guid}] is not an admin of Org[${org.key}]")
  }

  def requireMember(org: Organization) {
    require(MembershipsDao.isUserMember(user, org), s"Action requires member role. User[${user.guid}] is not a member of Org[${org.key}]")
  }

}

object Authenticated extends ActionBuilder[AuthenticatedRequest] {

  def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]) = {
    val userAuth = RequestHelper.userAuth(
      request.headers.get(RequestHelper.AuthorizationHeader),
      request.headers.get(RequestHelper.UserGuidHeader)
    )

    userAuth.user match {
      case None => {
        Future.successful(Unauthorized(s"Failed basic authorization or missing ${RequestHelper.UserGuidHeader} header"))
      }

      case Some(user) => {
        block(new AuthenticatedRequest(userAuth.tokenUser.get, user, request))
      }
    }
  }

}
{'fraction_non_alphanumeric': 0.11150317572335922, 'fraction_numerical': 0.0, 'mean_word_length': 4.007950530035336, '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': '816557', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1664, 'n_words': 369}