com.lightningkite.lightningdb.ModelPermissions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
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)
}