Skip to content

Commit

Permalink
Add whitelist capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Dec 14, 2015
1 parent 227c00f commit 4c61b92
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<id>vault-repo</id>
<url>http://nexus.theyeticave.net/content/repositories/pub_releases</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/gg/obsidian/discoursegroupsync/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import java.util.*
class Configuration(val plugin: DiscourseGroupSync) {

var DISCOURSE_URL = ""
var KICK_MESSAGE = ""
var GROUPS: HashSet<Group> = HashSet<Group>()

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<String, Any>
var discourseGroup: Int? = null
var minecraftGroup: String? = null
var remove = false
var whitelist = false

if (definition.contains("discourse")) {
discourseGroup = definition["discourse"] as Int
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/gg/obsidian/discoursegroupsync/Group.kt
Original file line number Diff line number Diff line change
@@ -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
)
44 changes: 37 additions & 7 deletions src/main/kotlin/gg/obsidian/discoursegroupsync/UserManger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<String>()
val groupsToRemove = HashSet<String>()

Expand Down Expand Up @@ -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)
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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:

Expand Down

0 comments on commit 4c61b92

Please sign in to comment.