PageRenderTime 22ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/worldedit-bukkit/src/main/java/com/sk89q/wepif/PermissionsResolverManager.java

https://gitlab.com/Skull3x/WorldEdit
Java | 307 lines | 248 code | 40 blank | 19 comment | 35 complexity | a8c938c27aa9adca777b87a25feea47c MD5 | raw file
  1. /*
  2. * WorldEdit, a Minecraft world manipulation toolkit
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldEdit team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.wepif;
  20. import com.sk89q.util.yaml.YAMLFormat;
  21. import com.sk89q.util.yaml.YAMLProcessor;
  22. import org.bukkit.OfflinePlayer;
  23. import org.bukkit.Server;
  24. import org.bukkit.event.EventHandler;
  25. import org.bukkit.event.server.PluginDisableEvent;
  26. import org.bukkit.event.server.PluginEnableEvent;
  27. import org.bukkit.plugin.Plugin;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.lang.reflect.Method;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.Iterator;
  34. import java.util.List;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. public class PermissionsResolverManager implements PermissionsResolver {
  38. private static final String CONFIG_HEADER = "#\r\n" +
  39. "# WEPIF Configuration File\r\n" +
  40. "#\r\n" +
  41. "# This file handles permissions configuration for every plugin using WEPIF\r\n" +
  42. "#\r\n" +
  43. "# About editing this file:\r\n" +
  44. "# - DO NOT USE TABS. You MUST use spaces or Bukkit will complain. If\r\n" +
  45. "# you use an editor like Notepad++ (recommended for Windows users), you\r\n" +
  46. "# must configure it to \"replace tabs with spaces.\" In Notepad++, this can\r\n" +
  47. "# be changed in Settings > Preferences > Language Menu.\r\n" +
  48. "# - Don't get rid of the indents. They are indented so some entries are\r\n" +
  49. "# in categories (like \"enforce-single-session\" is in the \"protection\"\r\n" +
  50. "# category.\r\n" +
  51. "# - If you want to check the format of this file before putting it\r\n" +
  52. "# into WEPIF, paste it into http://yaml-online-parser.appspot.com/\r\n" +
  53. "# and see if it gives \"ERROR:\".\r\n" +
  54. "# - Lines starting with # are comments and so they are ignored.\r\n" +
  55. "#\r\n" +
  56. "# About Configuration Permissions\r\n" +
  57. "# - See http://wiki.sk89q.com/wiki/WorldEdit/Permissions/Bukkit\r\n" +
  58. "# - Now with multiworld support (see example)\r\n" +
  59. "\r\n";
  60. private static PermissionsResolverManager instance;
  61. public static void initialize(Plugin plugin) {
  62. if (!isInitialized()) {
  63. instance = new PermissionsResolverManager(plugin);
  64. }
  65. }
  66. public static boolean isInitialized() {
  67. return instance != null;
  68. }
  69. public static PermissionsResolverManager getInstance() {
  70. if (!isInitialized()) {
  71. throw new WEPIFRuntimeException("WEPIF has not yet been initialized!");
  72. }
  73. return instance;
  74. }
  75. private Server server;
  76. private PermissionsResolver permissionResolver;
  77. private YAMLProcessor config;
  78. private Logger logger = Logger.getLogger(getClass().getCanonicalName());
  79. private List<Class<? extends PermissionsResolver>> enabledResolvers = new ArrayList<Class<? extends PermissionsResolver>>();
  80. @SuppressWarnings("unchecked")
  81. protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[] {
  82. PluginPermissionsResolver.class,
  83. PermissionsExResolver.class,
  84. bPermissionsResolver.class,
  85. GroupManagerResolver.class,
  86. NijiPermissionsResolver.class,
  87. DinnerPermsResolver.class,
  88. FlatFilePermissionsResolver.class
  89. };
  90. protected PermissionsResolverManager(Plugin plugin) {
  91. this.server = plugin.getServer();
  92. (new ServerListener()).register(plugin); // Register the events
  93. loadConfig(new File("wepif.yml"));
  94. findResolver();
  95. }
  96. public void findResolver() {
  97. for (Class<? extends PermissionsResolver> resolverClass : enabledResolvers) {
  98. try {
  99. Method factoryMethod = resolverClass.getMethod("factory", Server.class, YAMLProcessor.class);
  100. this.permissionResolver = (PermissionsResolver) factoryMethod.invoke(null, this.server, this.config);
  101. if (this.permissionResolver != null) {
  102. break;
  103. }
  104. } catch (Throwable e) {
  105. logger.log(Level.WARNING, "Error in factory method for " + resolverClass.getSimpleName(), e);
  106. continue;
  107. }
  108. }
  109. if (permissionResolver == null) {
  110. permissionResolver = new ConfigurationPermissionsResolver(config);
  111. }
  112. permissionResolver.load();
  113. logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
  114. }
  115. public void setPluginPermissionsResolver(Plugin plugin) {
  116. if (!(plugin instanceof PermissionsProvider)) {
  117. return;
  118. }
  119. permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
  120. logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
  121. }
  122. @Override
  123. public void load() {
  124. findResolver();
  125. }
  126. @Override
  127. public boolean hasPermission(String name, String permission) {
  128. return permissionResolver.hasPermission(name, permission);
  129. }
  130. @Override
  131. public boolean hasPermission(String worldName, String name, String permission) {
  132. return permissionResolver.hasPermission(worldName, name, permission);
  133. }
  134. @Override
  135. public boolean inGroup(String player, String group) {
  136. return permissionResolver.inGroup(player, group);
  137. }
  138. @Override
  139. public String[] getGroups(String player) {
  140. return permissionResolver.getGroups(player);
  141. }
  142. @Override
  143. public boolean hasPermission(OfflinePlayer player, String permission) {
  144. return permissionResolver.hasPermission(player, permission);
  145. }
  146. @Override
  147. public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
  148. return permissionResolver.hasPermission(worldName, player, permission);
  149. }
  150. @Override
  151. public boolean inGroup(OfflinePlayer player, String group) {
  152. return permissionResolver.inGroup(player, group);
  153. }
  154. @Override
  155. public String[] getGroups(OfflinePlayer player) {
  156. return permissionResolver.getGroups(player);
  157. }
  158. @Override
  159. public String getDetectionMessage() {
  160. return "Using WEPIF for permissions";
  161. }
  162. private boolean loadConfig(File file) {
  163. boolean isUpdated = false;
  164. if (!file.exists()) {
  165. try {
  166. file.createNewFile();
  167. } catch (IOException e) {
  168. logger.log(Level.WARNING, "Failed to create new configuration file", e);
  169. }
  170. }
  171. config = new YAMLProcessor(file, false, YAMLFormat.EXTENDED);
  172. try {
  173. config.load();
  174. } catch (IOException e) {
  175. logger.log(Level.WARNING, "Error loading WEPIF configuration", e);
  176. }
  177. List<String> keys = config.getKeys(null);
  178. config.setHeader(CONFIG_HEADER);
  179. if (!keys.contains("ignore-nijiperms-bridges")) {
  180. config.setProperty("ignore-nijiperms-bridges", true);
  181. isUpdated = true;
  182. }
  183. if (!keys.contains("resolvers")) {
  184. //List<String> resolverKeys = config.getKeys("resolvers");
  185. List<String> resolvers = new ArrayList<String>();
  186. for (Class<?> clazz : availableResolvers) {
  187. resolvers.add(clazz.getSimpleName());
  188. }
  189. enabledResolvers.addAll(Arrays.asList(availableResolvers));
  190. config.setProperty("resolvers.enabled", resolvers);
  191. isUpdated = true;
  192. } else {
  193. List<String> disabledResolvers = config.getStringList("resolvers.disabled", new ArrayList<String>());
  194. List<String> stagedEnabled = config.getStringList("resolvers.enabled", null);
  195. for (Iterator<String> i = stagedEnabled.iterator(); i.hasNext();) {
  196. String nextName = i.next();
  197. Class<?> next = null;
  198. try {
  199. next = Class.forName(getClass().getPackage().getName() + "." + nextName);
  200. } catch (ClassNotFoundException e) {}
  201. if (next == null || !PermissionsResolver.class.isAssignableFrom(next)) {
  202. logger.warning("WEPIF: Invalid or unknown class found in enabled resolvers: "
  203. + nextName + ". Moving to disabled resolvers list.");
  204. i.remove();
  205. disabledResolvers.add(nextName);
  206. isUpdated = true;
  207. continue;
  208. }
  209. enabledResolvers.add(next.asSubclass(PermissionsResolver.class));
  210. }
  211. for (Class<?> clazz : availableResolvers) {
  212. if (!stagedEnabled.contains(clazz.getSimpleName()) &&
  213. !disabledResolvers.contains(clazz.getSimpleName())) {
  214. disabledResolvers.add(clazz.getSimpleName());
  215. logger.info("New permissions resolver: "
  216. + clazz.getSimpleName() + " detected. " +
  217. "Added to disabled resolvers list.");
  218. isUpdated = true;
  219. }
  220. }
  221. config.setProperty("resolvers.disabled", disabledResolvers);
  222. config.setProperty("resolvers.enabled", stagedEnabled);
  223. }
  224. if (keys.contains("dinner-perms") || keys.contains("dinnerperms")) {
  225. config.removeProperty("dinner-perms");
  226. config.removeProperty("dinnerperms");
  227. isUpdated = true;
  228. }
  229. if (!keys.contains("permissions")) {
  230. ConfigurationPermissionsResolver.generateDefaultPerms(
  231. config.addNode("permissions"));
  232. isUpdated = true;
  233. }
  234. if (isUpdated) {
  235. logger.info("WEPIF: Updated config file");
  236. config.save();
  237. }
  238. return isUpdated;
  239. }
  240. public static class MissingPluginException extends Exception {
  241. }
  242. class ServerListener implements org.bukkit.event.Listener {
  243. @EventHandler
  244. public void onPluginEnable(PluginEnableEvent event) {
  245. Plugin plugin = event.getPlugin();
  246. String name = plugin.getDescription().getName();
  247. if (plugin instanceof PermissionsProvider) {
  248. setPluginPermissionsResolver(plugin);
  249. } else if ("permissions".equalsIgnoreCase(name) || "permissionsex".equalsIgnoreCase(name)
  250. || "bpermissions".equalsIgnoreCase(name) || "groupmanager".equalsIgnoreCase(name)) {
  251. load();
  252. }
  253. }
  254. @EventHandler
  255. public void onPluginDisable(PluginDisableEvent event) {
  256. String name = event.getPlugin().getDescription().getName();
  257. if (event.getPlugin() instanceof PermissionsProvider
  258. || "permissions".equalsIgnoreCase(name) || "permissionsex".equalsIgnoreCase(name)
  259. || "bpermissions".equalsIgnoreCase(name) || "groupmanager".equalsIgnoreCase(name)) {
  260. load();
  261. }
  262. }
  263. void register(Plugin plugin) {
  264. plugin.getServer().getPluginManager().registerEvents(this, plugin);
  265. }
  266. }
  267. }