Skip to content

Commit

Permalink
Add group extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Nov 29, 2015
1 parent c1c9ee8 commit f7835e1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Configuration(val plugin: Plugin) {
var displayName: String? = null
var rank = 10;
val permissions = HashSet<String>()
val extends = HashSet<String>()

if (definition.contains("name")) {
name = definition["name"] as String
Expand All @@ -37,13 +38,19 @@ class Configuration(val plugin: Plugin) {
permissions.addAll(hashmap)
}

if (definition.contains("extends")) {
val hashmap = definition["extends"] as ArrayList<String>
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)
}
Expand Down
28 changes: 27 additions & 1 deletion src/main/kotlin/gg/obsidian/obsidianpermissions/Manager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,40 @@ class Manager(val plugin: Plugin) {
private fun getCombinedPermissionsFor(player: Player): Map<String, Boolean> {
val finalMap = HashMap<String, Boolean>()
val groups = plugin.groupMembershipTable.getPlayerGroups(player)
val appliedGroups = HashSet<Group>()

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<Group> = HashSet<Group>()
): Set<String>? {
if (appliedGroups.contains(group)) return null

if (group.extends.size == 0) return group.permissions

val permissions = HashSet<String>()
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ data class Group(
val name: String,
val rank: Int,
val permissions: Set<String>,
val displayNameTemplate: String? = null
val displayNameTemplate: String? = null,
val extends: Set<String>
) {}

0 comments on commit f7835e1

Please sign in to comment.