Add security for follow

This commit is contained in:
2019-08-31 00:14:05 +02:00
parent 52dfaaf814
commit cb91c50e58
8 changed files with 75 additions and 21 deletions

View File

@@ -0,0 +1,53 @@
package fr.dcproject.security.voter
import io.ktor.application.ApplicationCall
import fr.dcproject.entity.Follow as FollowEntity
import fr.dcproject.entity.User as UserEntity
class FollowVoter: Voter {
enum class Action: ActionI {
CREATE,
DELETE,
VIEW
}
override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean {
return (action is Action) &&
(subject is List<*> || subject is FollowEntity<*>?)
}
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
}
if (action == Action.DELETE) {
return if (user != null) Vote.GRANTED
else Vote.DENIED
}
if (action == Action.VIEW) {
if (subject is FollowEntity<*>) {
return voteView(user, subject)
}
if (subject is List<*>) {
subject.forEach {
if (it !is FollowEntity<*> || voteView(user, it) == Vote.DENIED) {
return Vote.DENIED
}
}
return Vote.GRANTED
}
return Vote.DENIED
}
return Vote.ABSTAIN
}
private fun voteView(user: UserEntity?, subject: FollowEntity<*>): Vote {
return if ((user != null && subject.createdBy?.user?.id == user.id) || subject.createdBy?.followAnonymous == false) Vote.GRANTED
else Vote.DENIED
}
}