Refactor Voter

This commit is contained in:
2019-08-30 15:15:29 +02:00
parent d1999d84ca
commit 5d78f8d4c6
2 changed files with 39 additions and 19 deletions

View File

@@ -28,13 +28,8 @@ class ArticleVoter: Voter {
return Vote.GRANTED return Vote.GRANTED
} }
if (action == CommentVoter.Action.CREATE) { if (action is CommentVoter.Action) return voteForComment(action)
return Vote.GRANTED if (action is VoteVoter.Action) return voteForVote(action, subject)
}
if (action == CommentVoter.Action.VIEW) {
return Vote.GRANTED
}
if (subject is ArticleEntity) { if (subject is ArticleEntity) {
if (action == Action.DELETE && user is User && subject.createdBy?.userId == user.id) { if (action == Action.DELETE && user is User && subject.createdBy?.userId == user.id) {
@@ -48,6 +43,14 @@ class ArticleVoter: Voter {
return Vote.DENIED return Vote.DENIED
} }
if (action is Action) {
return Vote.DENIED
}
return Vote.ABSTAIN
}
private fun voteForVote(action: VoteVoter.Action, subject: Any?): Vote {
if (action == VoteVoter.Action.CREATE && subject is VoteEntity<*>) { if (action == VoteVoter.Action.CREATE && subject is VoteEntity<*>) {
val target = subject.target val target = subject.target
if (target !is ArticleEntity) { if (target !is ArticleEntity) {
@@ -57,9 +60,16 @@ class ArticleVoter: Voter {
return Vote.DENIED return Vote.DENIED
} }
} }
return Vote.ABSTAIN
}
if (action is Action) { private fun voteForComment(action: CommentVoter.Action): Vote {
return Vote.DENIED if (action == CommentVoter.Action.CREATE) {
return Vote.GRANTED
}
if (action == CommentVoter.Action.VIEW) {
return Vote.GRANTED
} }
return Vote.ABSTAIN return Vote.ABSTAIN

View File

@@ -27,14 +27,6 @@ class ConstitutionVoter: Voter {
return Vote.GRANTED 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 ConstitutionEntity && subject.createdBy?.userId == user.id) { if (action == Action.DELETE && user is User && subject is ConstitutionEntity && subject.createdBy?.userId == user.id) {
return Vote.GRANTED return Vote.GRANTED
} }
@@ -43,6 +35,17 @@ class ConstitutionVoter: Voter {
return Vote.GRANTED return Vote.GRANTED
} }
if (action is CommentVoter.Action) return voteForComment(action)
if (action is VoteVoter.Action) return voteForVote(action, subject)
if (action is Action) {
return Vote.DENIED
}
return Vote.ABSTAIN
}
private fun voteForVote(action: VoteVoter.Action, subject: Any?): Vote {
if (action == VoteVoter.Action.CREATE && subject is VoteEntity<*>) { if (action == VoteVoter.Action.CREATE && subject is VoteEntity<*>) {
val target = subject.target val target = subject.target
if (target !is ConstitutionEntity) { if (target !is ConstitutionEntity) {
@@ -52,9 +55,16 @@ class ConstitutionVoter: Voter {
return Vote.DENIED return Vote.DENIED
} }
} }
return Vote.ABSTAIN
}
if (action is Action) { private fun voteForComment(action: CommentVoter.Action): Vote {
return Vote.DENIED if (action == CommentVoter.Action.CREATE) {
return Vote.GRANTED
}
if (action == CommentVoter.Action.VIEW) {
return Vote.GRANTED
} }
return Vote.ABSTAIN return Vote.ABSTAIN