56 lines
1.5 KiB
Kotlin
56 lines
1.5 KiB
Kotlin
package fr.dcproject.security.voter
|
|
|
|
import fr.dcproject.entity.Comment
|
|
import io.ktor.application.ApplicationCall
|
|
|
|
class CommentVoter : Voter {
|
|
enum class Action : ActionI {
|
|
CREATE,
|
|
UPDATE,
|
|
VIEW,
|
|
DELETE
|
|
}
|
|
|
|
override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean {
|
|
return (action is Action)
|
|
.and(subject is Comment<*>? || subject is List<*>)
|
|
}
|
|
|
|
override fun vote(action: ActionI, call: ApplicationCall, subject: Any?): Vote {
|
|
val user = call.user
|
|
if (action == Action.CREATE && user != null) {
|
|
return Vote.GRANTED
|
|
}
|
|
|
|
if (action == Action.VIEW) {
|
|
if (subject is Comment<*>) {
|
|
return if (subject.isDeleted()) Vote.DENIED
|
|
else Vote.GRANTED
|
|
}
|
|
if (subject is List<*>) {
|
|
subject.forEach {
|
|
if (it !is Comment<*> || it.isDeleted()) {
|
|
return Vote.DENIED
|
|
}
|
|
}
|
|
return Vote.GRANTED
|
|
}
|
|
return Vote.DENIED
|
|
}
|
|
|
|
if (action == Action.UPDATE && user != null && subject is Comment<*> && user.id == subject.createdBy.user.id) {
|
|
return Vote.GRANTED
|
|
}
|
|
|
|
if (action == Action.DELETE) {
|
|
return Vote.DENIED
|
|
}
|
|
|
|
if (action is Action) {
|
|
return Vote.DENIED
|
|
}
|
|
|
|
return Vote.ABSTAIN
|
|
}
|
|
}
|