<commit_before>package models

import scala.util.control.Exception.catching
import play.api.i18n.Messages
import macros._
import dbrary._
import site._

/** The possible levels of permission used for site access, user delegation, and volume permissions.
  * Must match the corresponding postgres "permission" type. */
object Permission extends PGEnum("permission") {
  val NONE, VIEW, DOWNLOAD, CONTRIBUTE, ADMIN = Value
  // aliases or equivalent permissions (do not use val here)
  /** Alias for ADMIN. Volume ADMINs are considered OWNers. */
  def OWN = ADMIN
  /** Alias for CONTRIBUTE. */
  def EDIT = CONTRIBUTE
  /** Alias for CONTRIBUTE. Grants full access to data, bypassing consent permissions. */
  def FULL = CONTRIBUTE
  /** Alias for DOWNLOAD. DOWNLOAD permissions grant access to shared data, while non-data only requires VIEW. */
  def DATA = DOWNLOAD

  override implicit val sqlType : SQLType[Value] =
    SQLType.transform[Option[String], Value]("permission", classOf[Value])(
      _.fold[Option[Value]](Some(NONE))(s => catching(classOf[NoSuchElementException]).opt(withName(s))),
      p => Some(p.toString))

  def check(has : Value, need : Value)(implicit site : Site) : Boolean =
    has >= need || site.superuser

  /** The effective permission for data objects with the given attributes.
    * Note that this is also implemented (and used) in the SQL function data_permission, which must be kept consistent. */
  def data(p : Value, consent : Consent.Value, classification : Classification.Value)(implicit site_ : Site) : HasPermission = new HasPermission {
    val site = site_
    val getPermission =
      if (p >= FULL)
        p
      else if (p >= VIEW)
        if (Classification.download(p, consent).fold(false)(classification >= _))
          DOWNLOAD
        else
          VIEW
      else
        NONE
  }
}

trait HasPermission extends PerSite {
  def getPermission : Permission.Value
  final def checkPermission(need : Permission.Value) : Boolean =
    Permission.check(getPermission, need)(site)
}

/** The possible levels of participant consent governing [Classification.IDENTIFIED] data.
  * Must match the corresponding postgres "consent" type, except for the NONE value which represents NULL (missing) as this is a common possibility. */
object Consent extends PGEnum("consent") {
  val NONE, PRIVATE, SHARED, EXCERPTS, PUBLIC = Value
  def description(v : Value) = Messages("consent." + v.toString)
  implicit val truth : Truth[Value] = Truth[Value](_ != NONE)
  override implicit val sqlType : SQLType[Value] =
    SQLType.transform[Option[String], Value]("consent", classOf[Value])(
      _.fold[Option[Value]](Some(NONE))(s => catching(classOf[NoSuchElementException]).opt(withName(s))),
      Maybe(_).opt.map(_.toString))
}

/** The possible types of data sensitivity according to the presence of identifying user data.
  * Must match the corresponding postgres "consent" type. */
object Classification extends PGEnum("classification") {
  val IDENTIFIED, EXCERPT, DEIDENTIFIED, ANALYSIS, PRODUCT, MATERIAL = Value
  def RESTRICTED = IDENTIFIED
  /** ANALYSIS and above are non-data and so unrestricted. */
  def UNRESTRICTED = ANALYSIS

  /** The most restricted data classification level that the current user may access under the given consent level.
    * Actual access to data will additionally depend on volume permissions not checked here. */
  def access(consent : Consent.Value)(implicit site : Site) : Value = {
    val c = consent
    val a = site.access
    if (// a >= Permission.ADMIN ||
        c >= Consent.PUBLIC ||
        c >= Consent.SHARED && a >= Permission.DOWNLOAD)
      IDENTIFIED
    else if (c >= Consent.EXCERPTS)
      EXCERPT
    else
      DEIDENTIFIED
  }

  /** The most restricted data classification level that the current user may access under the given permission and consent level. */
  def download(p : Permission.Value, consent : Consent.Value)(implicit site : Site) : Option[Value] = {
    if (p >= Permission.FULL)
      Some(Classification.values.min)
    else if (p >= Permission.DOWNLOAD)
      Some(Classification.access(consent))
    else if (p >= Permission.VIEW)
      Some(Classification.UNRESTRICTED)
    else
      None
  }
}
<commit_msg>Grant superusers full download privs<commit_after>package models

import scala.util.control.Exception.catching
import play.api.i18n.Messages
import macros._
import dbrary._
import site._

/** The possible levels of permission used for site access, user delegation, and volume permissions.
  * Must match the corresponding postgres "permission" type. */
object Permission extends PGEnum("permission") {
  val NONE, VIEW, DOWNLOAD, CONTRIBUTE, ADMIN = Value
  // aliases or equivalent permissions (do not use val here)
  /** Alias for ADMIN. Volume ADMINs are considered OWNers. */
  def OWN = ADMIN
  /** Alias for CONTRIBUTE. */
  def EDIT = CONTRIBUTE
  /** Alias for CONTRIBUTE. Grants full access to data, bypassing consent permissions. */
  def FULL = CONTRIBUTE
  /** Alias for DOWNLOAD. DOWNLOAD permissions grant access to shared data, while non-data only requires VIEW. */
  def DATA = DOWNLOAD

  override implicit val sqlType : SQLType[Value] =
    SQLType.transform[Option[String], Value]("permission", classOf[Value])(
      _.fold[Option[Value]](Some(NONE))(s => catching(classOf[NoSuchElementException]).opt(withName(s))),
      p => Some(p.toString))

  def check(has : Value, need : Value)(implicit site : Site) : Boolean =
    has >= need || site.superuser

  /** The effective permission for data objects with the given attributes.
    * Note that this is also implemented (and used) in the SQL function data_permission, which must be kept consistent. */
  def data(p : Value, consent : Consent.Value, classification : Classification.Value)(implicit site_ : Site) : HasPermission = new HasPermission {
    val site = site_
    val getPermission =
      if (p >= FULL)
        p
      else if (p >= VIEW)
        if (Classification.download(p, consent).fold(false)(classification >= _))
          DOWNLOAD
        else
          VIEW
      else
        NONE
  }
}

trait HasPermission extends PerSite {
  def getPermission : Permission.Value
  final def checkPermission(need : Permission.Value) : Boolean =
    Permission.check(getPermission, need)(site)
}

/** The possible levels of participant consent governing [Classification.IDENTIFIED] data.
  * Must match the corresponding postgres "consent" type, except for the NONE value which represents NULL (missing) as this is a common possibility. */
object Consent extends PGEnum("consent") {
  val NONE, PRIVATE, SHARED, EXCERPTS, PUBLIC = Value
  def description(v : Value) = Messages("consent." + v.toString)
  implicit val truth : Truth[Value] = Truth[Value](_ != NONE)
  override implicit val sqlType : SQLType[Value] =
    SQLType.transform[Option[String], Value]("consent", classOf[Value])(
      _.fold[Option[Value]](Some(NONE))(s => catching(classOf[NoSuchElementException]).opt(withName(s))),
      Maybe(_).opt.map(_.toString))
}

/** The possible types of data sensitivity according to the presence of identifying user data.
  * Must match the corresponding postgres "consent" type. */
object Classification extends PGEnum("classification") {
  val IDENTIFIED, EXCERPT, DEIDENTIFIED, ANALYSIS, PRODUCT, MATERIAL = Value
  def RESTRICTED = IDENTIFIED
  /** ANALYSIS and above are non-data and so unrestricted. */
  def UNRESTRICTED = ANALYSIS

  /** The most restricted data classification level that the current user may access under the given consent level.
    * Actual access to data will additionally depend on volume permissions not checked here. */
  def access(consent : Consent.Value)(implicit site : Site) : Value = {
    val c = consent
    val a = site.access
    if (// a >= Permission.ADMIN ||
        c >= Consent.PUBLIC ||
        c >= Consent.SHARED && a >= Permission.DOWNLOAD)
      IDENTIFIED
    else if (c >= Consent.EXCERPTS)
      EXCERPT
    else
      DEIDENTIFIED
  }

  /** The most restricted data classification level that the current user may access under the given permission and consent level. */
  def download(p : Permission.Value, consent : Consent.Value)(implicit site : Site) : Option[Value] = {
    if (p >= Permission.FULL || site.superuser)
      Some(Classification.values.min)
    else if (p >= Permission.DOWNLOAD)
      Some(Classification.access(consent))
    else if (p >= Permission.VIEW)
      Some(Classification.UNRESTRICTED)
    else
      None
  }
}

{'fraction_non_alphanumeric': 0.10076246334310851, 'fraction_numerical': 0.000469208211143695, 'mean_word_length': 3.6387377584330793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7219071', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2381, 'n_words': 831}