2011-09-19 06:24:21 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-09-27 07:32:46 +00:00
|
|
|
*/
|
2011-09-19 06:24:21 +00:00
|
|
|
|
2011-12-04 17:56:35 +00:00
|
|
|
package com.sk89q.wepif;
|
2011-08-02 04:02:15 +00:00
|
|
|
|
2011-10-23 05:22:50 +00:00
|
|
|
import com.sk89q.util.yaml.YAMLProcessor;
|
2011-12-04 17:56:35 +00:00
|
|
|
import org.bukkit.OfflinePlayer;
|
2011-08-02 04:02:15 +00:00
|
|
|
import org.bukkit.Server;
|
2011-12-04 17:56:35 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2011-10-04 05:05:32 +00:00
|
|
|
import org.bukkit.permissions.Permissible;
|
|
|
|
import org.bukkit.permissions.Permission;
|
2011-08-02 04:02:15 +00:00
|
|
|
import org.bukkit.permissions.PermissionAttachmentInfo;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class DinnerPermsResolver implements PermissionsResolver {
|
|
|
|
|
|
|
|
private static final String GROUP_PREFIX = "group.";
|
|
|
|
private final Server server;
|
|
|
|
|
|
|
|
public DinnerPermsResolver(Server server) {
|
|
|
|
this.server = server;
|
|
|
|
}
|
2011-09-27 07:32:46 +00:00
|
|
|
|
2011-10-23 05:22:50 +00:00
|
|
|
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
2011-09-27 03:40:18 +00:00
|
|
|
return new DinnerPermsResolver(server);
|
2011-09-27 07:32:46 +00:00
|
|
|
}
|
|
|
|
|
2011-08-02 04:02:15 +00:00
|
|
|
public void load() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermission(String name, String permission) {
|
2011-12-04 17:56:35 +00:00
|
|
|
return hasPermission(server.getOfflinePlayer(name), permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermission(String worldName, String name, String permission) {
|
|
|
|
return hasPermission(worldName, server.getOfflinePlayer(name), permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean inGroup(String name, String group) {
|
|
|
|
return inGroup(server.getOfflinePlayer(name), group);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] getGroups(String name) {
|
|
|
|
return getGroups(server.getOfflinePlayer(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermission(OfflinePlayer player, String permission) {
|
|
|
|
Permissible perms = getPermissible(player);
|
2011-10-04 05:05:32 +00:00
|
|
|
if (perms == null) {
|
2011-12-04 17:56:35 +00:00
|
|
|
return false; // Permissions are only registered for objects with a Permissible
|
2011-09-24 19:24:10 +00:00
|
|
|
}
|
2011-10-04 05:05:32 +00:00
|
|
|
switch (internalHasPermission(perms, permission)) {
|
2011-11-23 01:29:48 +00:00
|
|
|
case -1:
|
|
|
|
return false;
|
|
|
|
case 1:
|
|
|
|
return true;
|
2011-12-04 17:56:35 +00:00
|
|
|
}
|
|
|
|
int dotPos = permission.lastIndexOf(".");
|
|
|
|
while (dotPos > -1) {
|
|
|
|
switch (internalHasPermission(perms, permission.substring(0, dotPos + 1) + "*")) {
|
|
|
|
case -1:
|
|
|
|
return false;
|
|
|
|
case 1:
|
|
|
|
return true;
|
2011-09-24 19:24:10 +00:00
|
|
|
}
|
2011-09-04 23:21:49 +00:00
|
|
|
dotPos = permission.lastIndexOf(".", dotPos - 1);
|
2011-08-02 04:02:15 +00:00
|
|
|
}
|
2011-10-04 05:05:32 +00:00
|
|
|
return internalHasPermission(perms, "*") == 1;
|
2011-08-02 04:02:15 +00:00
|
|
|
}
|
|
|
|
|
2011-12-04 17:56:35 +00:00
|
|
|
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
|
|
|
return hasPermission(player, permission); // no per-world ability to check permissions in dinnerperms
|
2011-08-02 04:02:15 +00:00
|
|
|
}
|
|
|
|
|
2011-12-04 17:56:35 +00:00
|
|
|
public boolean inGroup(OfflinePlayer player, String group) {
|
2011-12-09 15:19:26 +00:00
|
|
|
final Permissible perms = getPermissible(player);
|
2011-10-04 05:05:32 +00:00
|
|
|
if (perms == null) {
|
2011-08-02 04:02:15 +00:00
|
|
|
return false;
|
2011-09-24 19:24:10 +00:00
|
|
|
}
|
2011-12-09 15:19:26 +00:00
|
|
|
|
|
|
|
final String perm = GROUP_PREFIX + group;
|
|
|
|
return perms.isPermissionSet(perm) && perms.hasPermission(perm);
|
2011-08-02 04:02:15 +00:00
|
|
|
}
|
|
|
|
|
2011-12-04 17:56:35 +00:00
|
|
|
public String[] getGroups(OfflinePlayer player) {
|
|
|
|
Permissible perms = getPermissible(player);
|
2011-10-04 05:05:32 +00:00
|
|
|
if (perms == null) {
|
2011-08-02 04:02:15 +00:00
|
|
|
return new String[0];
|
2011-09-24 19:24:10 +00:00
|
|
|
}
|
2011-08-02 04:02:15 +00:00
|
|
|
List<String> groupNames = new ArrayList<String>();
|
2011-10-04 05:05:32 +00:00
|
|
|
for (PermissionAttachmentInfo permAttach : perms.getEffectivePermissions()) {
|
2011-08-02 04:02:15 +00:00
|
|
|
String perm = permAttach.getPermission();
|
2011-09-24 19:24:10 +00:00
|
|
|
if (!(perm.startsWith(GROUP_PREFIX) && permAttach.getValue())) {
|
2011-08-02 04:02:15 +00:00
|
|
|
continue;
|
2011-09-24 19:24:10 +00:00
|
|
|
}
|
2011-08-16 21:05:33 +00:00
|
|
|
groupNames.add(perm.substring(GROUP_PREFIX.length(), perm.length()));
|
2011-08-02 04:02:15 +00:00
|
|
|
}
|
2011-09-24 19:24:10 +00:00
|
|
|
return groupNames.toArray(new String[groupNames.size()]);
|
2011-08-02 04:02:15 +00:00
|
|
|
}
|
2011-12-04 17:56:35 +00:00
|
|
|
|
|
|
|
public Permissible getPermissible(OfflinePlayer offline) {
|
|
|
|
if (offline == null) return null;
|
|
|
|
Permissible perm = null;
|
|
|
|
if (offline instanceof Permissible) {
|
|
|
|
perm = (Permissible) offline;
|
|
|
|
} else {
|
|
|
|
Player player = offline.getPlayer();
|
|
|
|
if (player != null) perm = player;
|
|
|
|
}
|
|
|
|
return perm;
|
|
|
|
}
|
2011-09-27 03:40:18 +00:00
|
|
|
|
2011-10-04 05:05:32 +00:00
|
|
|
/**
|
|
|
|
* Checks the permission from dinnerperms
|
|
|
|
* @param perms Permissible to check for
|
|
|
|
* @param permission The permission to check
|
|
|
|
* @return -1 if the permission is explicitly denied, 1 if the permission is allowed,
|
|
|
|
* 0 if the permission is denied by a default.
|
|
|
|
*/
|
|
|
|
public int internalHasPermission(Permissible perms, String permission) {
|
|
|
|
if (perms.isPermissionSet(permission)) {
|
|
|
|
return perms.hasPermission(permission) ? 1 : -1;
|
|
|
|
} else {
|
|
|
|
Permission perm = server.getPluginManager().getPermission(permission);
|
|
|
|
if (perm != null) {
|
|
|
|
return perm.getDefault().getValue(perms.isOp()) ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-09-27 03:40:18 +00:00
|
|
|
public String getDetectionMessage() {
|
|
|
|
return "Using the Bukkit Permissions API.";
|
|
|
|
}
|
2011-08-02 04:02:15 +00:00
|
|
|
}
|