@@ -1,5 +1,7 @@
|
||||
package fr.dcproject.security.voter
|
||||
|
||||
import fr.dcproject.entity.ArticleAuthI
|
||||
import fr.dcproject.entity.ArticleI
|
||||
import fr.dcproject.entity.ArticleSimpleI
|
||||
import fr.dcproject.entity.UserI
|
||||
import io.ktor.application.ApplicationCall
|
||||
@@ -16,53 +18,52 @@ class ArticleVoter : Voter {
|
||||
|
||||
override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean {
|
||||
return (action is Action || action is CommentVoter.Action || action is VoteVoter.Action)
|
||||
.and(subject is List<*> || subject is ArticleSimpleI? || subject is VoteEntity<*> || subject is CommentEntity<*>)
|
||||
.and(subject is ArticleI? || subject is VoteEntity<*> || subject is CommentEntity<*>)
|
||||
}
|
||||
|
||||
override fun vote(action: ActionI, call: ApplicationCall, subject: Any?): Vote {
|
||||
val user = call.user
|
||||
if (action == Action.CREATE && user is UserI) {
|
||||
return Vote.GRANTED
|
||||
}
|
||||
|
||||
if (action == Action.VIEW) {
|
||||
if (subject is ArticleSimpleI) {
|
||||
return if (subject.isDeleted()) Vote.DENIED
|
||||
else Vote.GRANTED
|
||||
}
|
||||
if (subject is List<*>) {
|
||||
subject.forEach {
|
||||
if (it !is ArticleSimpleI || it.isDeleted()) {
|
||||
return Vote.DENIED
|
||||
}
|
||||
}
|
||||
return Vote.GRANTED
|
||||
}
|
||||
return Vote.DENIED
|
||||
}
|
||||
|
||||
if (action == Action.CREATE && user is UserI) return Vote.GRANTED
|
||||
if (action == Action.VIEW) return view(subject, user)
|
||||
if (action == Action.DELETE) return delete(subject, user)
|
||||
if (action == Action.UPDATE) return update(subject, user)
|
||||
if (action is CommentVoter.Action) return voteForComment(action)
|
||||
if (action is VoteVoter.Action) return voteForVote(action, subject)
|
||||
|
||||
if (subject is ArticleSimpleI) {
|
||||
if (action == Action.DELETE && user is UserI && subject.createdBy.user.id == user.id) {
|
||||
return Vote.GRANTED
|
||||
}
|
||||
|
||||
if (action == Action.UPDATE && user is UserI && subject.createdBy.user.id == user.id) {
|
||||
return Vote.GRANTED
|
||||
}
|
||||
|
||||
return Vote.DENIED
|
||||
}
|
||||
|
||||
if (action is Action) {
|
||||
return Vote.DENIED
|
||||
}
|
||||
if (action is Action) return Vote.DENIED
|
||||
|
||||
return Vote.ABSTAIN
|
||||
}
|
||||
|
||||
private fun view(subject: Any?, user: UserI?): Vote {
|
||||
checkClass(ArticleAuthI::class, subject)
|
||||
if (subject is ArticleAuthI<*>) {
|
||||
return if (subject.isDeleted()) Vote.DENIED
|
||||
else if (subject.draft && (user == null || subject.createdBy.user.id != user.id)) Vote.DENIED
|
||||
else Vote.GRANTED
|
||||
}
|
||||
return Vote.DENIED
|
||||
}
|
||||
|
||||
private fun delete(subject: Any?, user: UserI?): Vote {
|
||||
checkClass(ArticleAuthI::class, subject)
|
||||
if (subject is ArticleAuthI<*>) {
|
||||
if (user is UserI && subject.createdBy.user.id == user.id) {
|
||||
return Vote.GRANTED
|
||||
}
|
||||
}
|
||||
return Vote.DENIED
|
||||
}
|
||||
|
||||
private fun update(subject: Any?, user: UserI?): Vote {
|
||||
checkClass(ArticleAuthI::class, subject)
|
||||
if (subject is ArticleAuthI<*>) {
|
||||
if (user is UserI && subject.createdBy.user.id == user.id) {
|
||||
return Vote.GRANTED
|
||||
}
|
||||
}
|
||||
return Vote.DENIED
|
||||
}
|
||||
|
||||
private fun voteForVote(action: VoteVoter.Action, subject: Any?): Vote {
|
||||
if (action == VoteVoter.Action.CREATE && subject is VoteEntity<*>) {
|
||||
val target = subject.target
|
||||
|
||||
Reference in New Issue
Block a user