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

com.lightningkite.ktordb.SecuredFieldCollection.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.ktordb

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlin.reflect.KProperty1
import kotlin.reflect.KType

open class SecuredFieldCollection(
    override val wraps: FieldCollection,
    val rules: SecurityRules,
) : AbstractSignalFieldCollection() {
    override suspend fun find(
        condition: Condition,
        orderBy: List>,
        skip: Int,
        limit: Int,
        maxQueryMs: Long
    ): Flow {
        return wraps.find(
            condition = if (orderBy.isNotEmpty()) orderBy.map { rules.sortAllowed(it) }
                .reduce { left, right -> left and right } and rules.read(condition)
            else rules.read(condition),
            orderBy = orderBy,
            skip = skip,
            limit = limit,
            maxQueryMs = maxQueryMs.coerceAtMost(rules.maxQueryTimeMs())
        )
            .map { rules.mask(it) }
    }

    override suspend fun insertImpl(models: List): List {
        return wraps.insertMany(models.map { rules.create(it) }).let { it.map { rules.mask(it) } }
    }

    override suspend fun replaceOneImpl(condition: Condition, model: Model): EntryChange {
        val f = rules.replace(model)
        return wraps.replaceOne(f.first and condition, f.second).let {
            EntryChange(
                it.old?.let { rules.mask(it) },
                it.new?.let { rules.mask(it) },
            )
        }
    }

    override suspend fun replaceOneIgnoringResultImpl(condition: Condition, model: Model): Boolean {
        val f = rules.replace(model)
        return wraps.replaceOneIgnoringResult(f.first and condition, f.second)
    }

    override suspend fun upsertOneImpl(
        condition: Condition,
        modification: Modification,
        model: Model
    ): EntryChange {
        val f = rules.replace(model)
        return wraps.upsertOne(f.first and condition, modification, f.second).let {
            EntryChange(
                it.old?.let { rules.mask(it) },
                it.new?.let { rules.mask(it) },
            )
        }
    }

    override suspend fun upsertOneIgnoringResultImpl(
        condition: Condition,
        modification: Modification,
        model: Model
    ): Boolean {
        val f = rules.replace(model)
        return wraps.upsertOneIgnoringResult(f.first and condition, modification, f.second)
    }

    override suspend fun updateOneImpl(
        condition: Condition,
        modification: Modification
    ): EntryChange {
        val e = rules.edit(condition, modification)
        return wraps.updateOne(condition and e.first, e.second)
    }

    override suspend fun updateOneIgnoringResultImpl(
        condition: Condition,
        modification: Modification
    ): Boolean {
        val e = rules.edit(condition, modification)
        return wraps.updateOneIgnoringResult(condition and e.first, e.second)
    }

    override suspend fun updateManyImpl(
        condition: Condition,
        modification: Modification
    ): CollectionChanges {
        val e = rules.edit(condition, modification)
        return wraps.updateMany(
            condition and e.first,
            e.second
        )
    }

    override suspend fun updateManyIgnoringResultImpl(
        condition: Condition,
        modification: Modification
    ): Int {
        val e = rules.edit(condition, modification)
        return wraps.updateManyIgnoringResult(
            condition and e.first,
            e.second
        )
    }

    override suspend fun deleteOneImpl(condition: Condition): Model? {
        return wraps.deleteOne(rules.delete(condition))
    }

    override suspend fun deleteOneIgnoringOldImpl(condition: Condition): Boolean {
        return wraps.deleteOneIgnoringOld(rules.delete(condition))
    }

    override suspend fun deleteManyImpl(condition: Condition): List {
        return wraps.deleteMany(rules.delete(condition))
    }

    override suspend fun deleteManyIgnoringOldImpl(condition: Condition): Int {
        return wraps.deleteManyIgnoringOld(condition)
    }

    override suspend fun watch(
        condition: Condition
    ): Flow> = wraps.watch(condition and rules.read(condition))
        .mapNotNull {
            val old = it.old?.let { rules.mask(it) }
            val new = it.new?.let { rules.mask(it) }
            EntryChange(old, new)
        }

    override suspend fun count(condition: Condition): Int = wraps.count(condition and rules.read(condition))

    override suspend fun  groupCount(
        condition: Condition,
        groupBy: KProperty1
    ): Map {
        return wraps.groupCount(condition and rules.read(condition) and rules.sortAllowed(SortPart(groupBy)), groupBy)
    }

    override suspend fun  aggregate(
        aggregate: Aggregate,
        condition: Condition,
        property: KProperty1
    ): Double? = wraps.aggregate(aggregate, condition and rules.read(condition), property)

    override suspend fun  groupAggregate(
        aggregate: Aggregate,
        condition: Condition,
        groupBy: KProperty1,
        property: KProperty1
    ): Map = wraps.groupAggregate(
        aggregate,
        condition and rules.read(condition) and rules.sortAllowed(SortPart(groupBy)),
        groupBy,
        property
    )
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy