From f7835e18c373b9fb8bbb1f0023ada1b345d4c4d1 Mon Sep 17 00:00:00 2001 From: Jacob Gillespie Date: Sun, 29 Nov 2015 15:01:22 -0600 Subject: [PATCH] Add group extension --- .../obsidianpermissions/Configuration.kt | 9 +++++- .../obsidian/obsidianpermissions/Manager.kt | 28 ++++++++++++++++++- .../obsidianpermissions/models/Group.kt | 3 +- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/gg/obsidian/obsidianpermissions/Configuration.kt b/src/main/kotlin/gg/obsidian/obsidianpermissions/Configuration.kt index 3d13c74..9214c51 100644 --- a/src/main/kotlin/gg/obsidian/obsidianpermissions/Configuration.kt +++ b/src/main/kotlin/gg/obsidian/obsidianpermissions/Configuration.kt @@ -19,6 +19,7 @@ class Configuration(val plugin: Plugin) { var displayName: String? = null var rank = 10; val permissions = HashSet() + val extends = HashSet() if (definition.contains("name")) { name = definition["name"] as String @@ -37,13 +38,19 @@ class Configuration(val plugin: Plugin) { permissions.addAll(hashmap) } + if (definition.contains("extends")) { + val hashmap = definition["extends"] as ArrayList + extends.addAll(hashmap) + } + if (name == null) continue val group = Group( name = name, rank = rank, permissions = permissions, - displayNameTemplate = displayName + displayNameTemplate = displayName, + extends = extends ) GROUPS.add(group) } diff --git a/src/main/kotlin/gg/obsidian/obsidianpermissions/Manager.kt b/src/main/kotlin/gg/obsidian/obsidianpermissions/Manager.kt index 8ea501d..74303d3 100644 --- a/src/main/kotlin/gg/obsidian/obsidianpermissions/Manager.kt +++ b/src/main/kotlin/gg/obsidian/obsidianpermissions/Manager.kt @@ -120,14 +120,40 @@ class Manager(val plugin: Plugin) { private fun getCombinedPermissionsFor(player: Player): Map { val finalMap = HashMap() val groups = plugin.groupMembershipTable.getPlayerGroups(player) + val appliedGroups = HashSet() + for (group in groups) { - for (permission in group.permissions) { + val permissions = resolveGroupPermissions(group, appliedGroups) ?: continue + for (permission in permissions) { finalMap.put(permission, true) } + appliedGroups.add(group) } return finalMap } + private fun resolveGroupPermissions( + group: Group, + appliedGroups: MutableSet = HashSet() + ): Set? { + if (appliedGroups.contains(group)) return null + + if (group.extends.size == 0) return group.permissions + + val permissions = HashSet() + permissions.addAll(group.permissions) + appliedGroups.add(group) + + for (groupName in group.extends) { + val subGroup = getGroup(groupName) ?: continue + val subPermissions = resolveGroupPermissions(subGroup, appliedGroups) ?: continue + permissions.addAll(subPermissions) + appliedGroups.add(group) + } + + return permissions + } + private fun getDisplayNameTemplate(player: Player): String { val groups = getPlayerGroups(player) for (group in groups) { diff --git a/src/main/kotlin/gg/obsidian/obsidianpermissions/models/Group.kt b/src/main/kotlin/gg/obsidian/obsidianpermissions/models/Group.kt index 6179284..e8b0acd 100644 --- a/src/main/kotlin/gg/obsidian/obsidianpermissions/models/Group.kt +++ b/src/main/kotlin/gg/obsidian/obsidianpermissions/models/Group.kt @@ -4,5 +4,6 @@ data class Group( val name: String, val rank: Int, val permissions: Set, - val displayNameTemplate: String? = null + val displayNameTemplate: String? = null, + val extends: Set ) {}