Refactoring of updateOpinions (route/repo/query)
Can nw be set multiple opinion on sigle query fix OpinionVoter on CREATE
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package fr.dcproject.security.voter
|
||||
|
||||
import fr.dcproject.entity.ArticleAuthI
|
||||
import fr.dcproject.entity.Opinion
|
||||
import io.ktor.application.ApplicationCall
|
||||
|
||||
@@ -12,13 +13,17 @@ class OpinionVoter : Voter {
|
||||
|
||||
override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean {
|
||||
return (action is Action)
|
||||
.and(subject is Opinion<*>?)
|
||||
.and(subject is Opinion<*>? || subject is ArticleAuthI<*>)
|
||||
}
|
||||
|
||||
override fun vote(action: ActionI, call: ApplicationCall, subject: Any?): Vote {
|
||||
val user = call.user
|
||||
if (action == Action.CREATE) {
|
||||
return if (user != null) Vote.GRANTED else Vote.DENIED
|
||||
return if (user != null && (
|
||||
(subject is ArticleAuthI<*> && !subject.isDeleted()) ||
|
||||
(subject is Opinion<*> && subject.createdBy.user.id == user.id)
|
||||
)) Vote.GRANTED
|
||||
else Vote.DENIED
|
||||
}
|
||||
|
||||
if (action == Action.VIEW) {
|
||||
|
||||
@@ -38,14 +38,15 @@ enum class Vote {
|
||||
|
||||
private val votersAttributeKey = AttributeKey<List<Voter>>("voters")
|
||||
|
||||
fun ApplicationCall.assertCan(action: ActionI, subject: Any? = null) {
|
||||
if (!can(action, subject)) {
|
||||
fun ApplicationCall.assertCan(action: ActionI, subject: Any? = null, agreeIfNullOrEmpty: Boolean = true) {
|
||||
val isNullOrEmpty = (subject == null || (subject is Collection<*> && subject.isNullOrEmpty()))
|
||||
if (!can(action, subject) && !agreeIfNullOrEmpty && isNullOrEmpty) {
|
||||
throw UnauthorizedException(action)
|
||||
}
|
||||
}
|
||||
|
||||
fun PipelineContext<Unit, ApplicationCall>.assertCan(action: ActionI, subject: Any? = null) =
|
||||
context.assertCan(action, subject)
|
||||
fun PipelineContext<Unit, ApplicationCall>.assertCan(action: ActionI, subject: Any? = null, agreeIfNullOrEmpty: Boolean = true) =
|
||||
context.assertCan(action, subject, agreeIfNullOrEmpty)
|
||||
|
||||
fun PipelineContext<Unit, ApplicationCall>.can(action: ActionI, subject: Any? = null) =
|
||||
context.can(action, subject)
|
||||
|
||||
Reference in New Issue
Block a user