feature #7: Add routes for comment article

This commit is contained in:
2019-08-27 22:24:38 +02:00
parent ff1e34c616
commit 1fb0e39038
12 changed files with 364 additions and 10 deletions

View File

@@ -13,7 +13,7 @@ class ArticleVoter: Voter {
}
override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean {
return action is Action && subject is Article?
return (action is Action || action is CommentVoter.Action) && subject is Article?
}
override fun vote(action: ActionI, call: ApplicationCall, subject: Any?): Vote {
@@ -26,6 +26,14 @@ class ArticleVoter: Voter {
return Vote.GRANTED
}
if (action == CommentVoter.Action.CREATE) {
return Vote.GRANTED
}
if (action == CommentVoter.Action.VIEW) {
return Vote.GRANTED
}
if (action == Action.DELETE && user is User && subject is Article && subject.createdBy?.userId == user.id) {
return Vote.GRANTED
}

View File

@@ -0,0 +1,38 @@
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 && subject is Comment<*>?
}
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) {
return Vote.GRANTED
}
if (action == Action.UPDATE && user != null && subject is Comment<*> && user.id == subject.createdBy?.userId) {
return Vote.GRANTED
}
if (action == Action.DELETE) {
return Vote.DENIED
}
return Vote.ABSTAIN
}
}