All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lightningkite.lightningdb.ModelPermissions.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.lightningdb

import kotlin.reflect.KProperty1

/**
 * Defines permissions for accessing a model in a database.
 * Default constructor is 'whitelist' mode.
 */
data class ModelPermissions(
    /**
     * The user may only create an item if it matches this condition.
     */
    val create: Condition = Condition.Never(),
    /**
     * The user may only read models that match this condition.
     */
    val read: Condition = Condition.Never(),
    /**
     * The user may only read models masked as defined here.
     */
    val readMask: Mask = Mask(listOf()),
    /**
     * The user may only update models that match this condition.
     */
    val update: Condition = Condition.Never(),
    /**
     * Restrictions on what the user is allowed to update.
     */
    val updateRestrictions: UpdateRestrictions = UpdateRestrictions(listOf()),
    /**
     * The user may only delete models that match this condition.
     */
    val delete: Condition = Condition.Never(),
    val maxQueryTimeMs: Long = 1_000L
) {
    companion object {
        /**
         * A full whitelist permission set.
         */
        fun  allowAll(): ModelPermissions = ModelPermissions(
            create = Condition.Always(),
            read = Condition.Always(),
            update = Condition.Always(),
            delete = Condition.Always(),
        )
    }
    /**
     * @return a condition defining under what circumstances the given [modification] is permitted in.
     */
    fun allowed(modification: Modification): Condition = updateRestrictions(modification) and update

    /**
     * Masks a single instance of the model.
     */
    fun mask(model: Model): Model = readMask(model)

    /**
     * Masks a single instance of the model.
     */
    fun mask(model: Partial): Partial = readMask(model)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy