From 4c61b9281db6571f73cb49232bbc348b76dec6b3 Mon Sep 17 00:00:00 2001 From: Jacob Gillespie Date: Sun, 13 Dec 2015 18:43:16 -0600 Subject: [PATCH] Add whitelist capabilities --- README.md | 5 +++ pom.xml | 4 -- .../discoursegroupsync/Configuration.kt | 14 +++++- .../discoursegroupsync/DiscourseGroupSync.kt | 5 ++- .../gg/obsidian/discoursegroupsync/Group.kt | 7 ++- .../obsidian/discoursegroupsync/UserManger.kt | 44 ++++++++++++++++--- src/main/resources/config.yml | 3 ++ 7 files changed, 68 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6accd8e..2724e97 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ DiscourseGroupSync has several options that can be configured in the `config.yml # The URL of your Discourse installation (without the trailing slash) discourse-url: http://forum.example.com +# Message to display if kicking due to whitelist +kick-message: You are not on the whitelist + # A mapping between Discourse groups (by integer ID) and Minecraft groups (by name) groups: @@ -40,6 +43,8 @@ groups: `discourse` keys are the IDs of your chosen Discourse groups. A negative number means the absence of the group, so `discourse: -20` would target users who were not a member of group `20`. `0` is a special group meaning users who are not a member of any Discourse groups. +Set a `whitelist: true` field on any of the groups to enable them as a whitelist filter. + ## Features * Synchronizes Minecraft groups with Discourse groups on player join diff --git a/pom.xml b/pom.xml index 7cff417..82844a0 100644 --- a/pom.xml +++ b/pom.xml @@ -31,10 +31,6 @@ vault-repo http://nexus.theyeticave.net/content/repositories/pub_releases - - jitpack.io - https://jitpack.io - diff --git a/src/main/kotlin/gg/obsidian/discoursegroupsync/Configuration.kt b/src/main/kotlin/gg/obsidian/discoursegroupsync/Configuration.kt index 6f8bf90..9101cd3 100644 --- a/src/main/kotlin/gg/obsidian/discoursegroupsync/Configuration.kt +++ b/src/main/kotlin/gg/obsidian/discoursegroupsync/Configuration.kt @@ -5,18 +5,21 @@ import java.util.* class Configuration(val plugin: DiscourseGroupSync) { var DISCOURSE_URL = "" + var KICK_MESSAGE = "" var GROUPS: HashSet = HashSet() fun load() { plugin.reloadConfig() DISCOURSE_URL = plugin.getConfig().getString("discourse-url") + KICK_MESSAGE = plugin.getConfig().getString("kick-message", "You are not on the whitelist") for (rawDefinition in plugin.getConfig().getMapList("groups")) { val definition = rawDefinition as Map var discourseGroup: Int? = null var minecraftGroup: String? = null var remove = false + var whitelist = false if (definition.contains("discourse")) { discourseGroup = definition["discourse"] as Int @@ -30,9 +33,18 @@ class Configuration(val plugin: DiscourseGroupSync) { remove = definition["remove"] as Boolean } + if (definition.contains("whitelist")) { + whitelist = definition["whitelist"] as Boolean + } + if (discourseGroup == null || minecraftGroup == null) continue - val group = Group(discourseGroup, minecraftGroup, remove) + val group = Group( + discourseGroup = discourseGroup, + minecraftGroup = minecraftGroup, + remove = remove, + whitelist = whitelist + ) GROUPS.add(group) } } diff --git a/src/main/kotlin/gg/obsidian/discoursegroupsync/DiscourseGroupSync.kt b/src/main/kotlin/gg/obsidian/discoursegroupsync/DiscourseGroupSync.kt index 40171fa..5b23832 100644 --- a/src/main/kotlin/gg/obsidian/discoursegroupsync/DiscourseGroupSync.kt +++ b/src/main/kotlin/gg/obsidian/discoursegroupsync/DiscourseGroupSync.kt @@ -34,7 +34,10 @@ class DiscourseGroupSync : JavaPlugin(), Listener { @EventHandler fun onJoin(e: PlayerJoinEvent) { - userManager.syncGroups(e.player) + val canJoin = userManager.onJoin(e.player) + if (!canJoin) { + e.player.kickPlayer(config.KICK_MESSAGE) + } } fun setupPermissions(): Boolean { diff --git a/src/main/kotlin/gg/obsidian/discoursegroupsync/Group.kt b/src/main/kotlin/gg/obsidian/discoursegroupsync/Group.kt index 7b5b204..288c485 100644 --- a/src/main/kotlin/gg/obsidian/discoursegroupsync/Group.kt +++ b/src/main/kotlin/gg/obsidian/discoursegroupsync/Group.kt @@ -1,3 +1,8 @@ package gg.obsidian.discoursegroupsync -data class Group(val discourseGroup: Int, val minecraftGroup: String, val remove: Boolean = false) +data class Group( + val discourseGroup: Int, + val minecraftGroup: String, + val remove: Boolean = false, + val whitelist: Boolean = false +) diff --git a/src/main/kotlin/gg/obsidian/discoursegroupsync/UserManger.kt b/src/main/kotlin/gg/obsidian/discoursegroupsync/UserManger.kt index b190007..5b9db28 100644 --- a/src/main/kotlin/gg/obsidian/discoursegroupsync/UserManger.kt +++ b/src/main/kotlin/gg/obsidian/discoursegroupsync/UserManger.kt @@ -12,7 +12,17 @@ class UserManager(val plugin: DiscourseGroupSync) { val httpClient = OkHttpClient() - fun getDiscourseUser(username: String): User { + fun onJoin(player: Player): Boolean { + val user = getDiscourseUser(player) + + syncGroups(player, user) + return checkWhitelist(user) + } + + fun getDiscourseUser(player: Player): User? { + val username = UUIDHelper.uuidToUsername(player.uniqueId) + if (username == "") return null + val url = plugin.config.DISCOURSE_URL + "/users/" + username + ".json" val request = Request.Builder().url(url).get().build(); val response = httpClient.newCall(request).execute() @@ -41,12 +51,32 @@ class UserManager(val plugin: DiscourseGroupSync) { return User(username = username, discourseGroups = discourseGroups) } - fun syncGroups(player: Player) { - val username = UUIDHelper.uuidToUsername(player.uniqueId) - if (username == "") return + fun checkWhitelist(user: User?): Boolean { + if (user == null) { + for (group in plugin.config.GROUPS) { + if (group.whitelist == true) + return false + } + return true + } - val user = getDiscourseUser(username) + var canJoin = true + + for (group in plugin.config.GROUPS) { + if (group.whitelist != true) continue + + if (user.discourseGroups.contains(group.discourseGroup)) { + return true + } else { + canJoin = false + } + } + + return canJoin + } + fun syncGroups(player: Player, user: User?) { + if (user == null) return val groupsToAdd = HashSet() val groupsToRemove = HashSet() @@ -82,12 +112,12 @@ class UserManager(val plugin: DiscourseGroupSync) { } for (group in groupsToAdd) { - plugin.logger.info("Adding $username to group $group") + plugin.logger.info("Adding ${user.username} to group $group") plugin.permissions?.playerAddGroup(player, group) } for (group in groupsToRemove) { - plugin.logger.info("Removing $username from group $group") + plugin.logger.info("Removing ${user.username} from group $group") plugin.permissions?.playerRemoveGroup(player, group) } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index dace8d5..c92a1bc 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,6 +1,9 @@ # The URL of your Discourse installation (without the trailing slash) discourse-url: http://forum.example.com +# Message to display if kicking due to whitelist +kick-message: You are not on the whitelist + # A mapping between Discourse groups (by integer ID) and Minecraft groups (by name) groups: