PageRenderTime 443ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/ru/tehkode/permissions/backends/FileBackend.java

https://github.com/NuclearW/PermissionsEx
Java | 356 lines | 325 code | 8 blank | 23 comment | 3 complexity | adab11e4f5b285f154121103492bf9d9 MD5 | raw file
  1. /*
  2. * PermissionsEx - Permissions plugin for Bukkit
  3. * Copyright (C) 2011 t3hk0d3 http://www.tehkode.ru
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package ru.tehkode.permissions.backends;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.OutputStreamWriter;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.LinkedList;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.bukkit.Bukkit;
  30. import org.bukkit.World;
  31. import org.yaml.snakeyaml.DumperOptions;
  32. import org.yaml.snakeyaml.Yaml;
  33. import org.yaml.snakeyaml.constructor.SafeConstructor;
  34. import org.yaml.snakeyaml.representer.Representer;
  35. import ru.tehkode.permissions.PermissionBackend;
  36. import ru.tehkode.permissions.PermissionGroup;
  37. import ru.tehkode.permissions.PermissionManager;
  38. import ru.tehkode.permissions.PermissionUser;
  39. import ru.tehkode.permissions.config.Configuration;
  40. import ru.tehkode.permissions.config.ConfigurationNode;
  41. import ru.tehkode.permissions.backends.file.FileGroup;
  42. import ru.tehkode.permissions.backends.file.FileUser;
  43. /**
  44. *
  45. * @author code
  46. */
  47. public class FileBackend extends PermissionBackend {
  48. public Configuration permissions;
  49. public FileBackend(PermissionManager manager, Configuration config) {
  50. super(manager, config);
  51. }
  52. @Override
  53. public void initialize() {
  54. String permissionFilename = config.getString("permissions.backends.file.file");
  55. // Default settings
  56. if (permissionFilename == null) {
  57. permissionFilename = "permissions.yml";
  58. config.setProperty("permissions.backends.file.file", "permissions.yml");
  59. config.save();
  60. }
  61. String baseDir = config.getString("permissions.basedir");
  62. if (baseDir.contains("\\") && !"\\".equals(File.separator)) {
  63. baseDir = baseDir.replace("\\", File.separator);
  64. }
  65. File baseDirectory = new File(baseDir);
  66. if (!baseDirectory.exists()) {
  67. baseDirectory.mkdirs();
  68. }
  69. File permissionFile = new File(baseDir, permissionFilename);
  70. permissions = new Configuration(permissionFile);
  71. if (!permissionFile.exists()) {
  72. try {
  73. permissionFile.createNewFile();
  74. // Load default permissions
  75. permissions.setProperty("groups.default.default", true);
  76. List<String> defaultPermissions = new LinkedList<String>();
  77. // Specify here default permissions
  78. defaultPermissions.add("modifyworld.*");
  79. permissions.setProperty("groups.default.permissions", defaultPermissions);
  80. permissions.save();
  81. } catch (IOException e) {
  82. throw new RuntimeException(e);
  83. }
  84. }
  85. permissions.load();
  86. }
  87. @Override
  88. public String[] getWorldInheritance(String world) {
  89. if (world != null && !world.isEmpty()) {
  90. return this.permissions.getStringList("worlds." + world + ".inheritance", new LinkedList<String>()).toArray(new String[0]);
  91. }
  92. return new String[0];
  93. }
  94. @Override
  95. public void setWorldInheritance(String world, String[] parentWorlds) {
  96. if (world == null && world.isEmpty()) {
  97. return;
  98. }
  99. this.permissions.setProperty("worlds." + world + ".inheritance", Arrays.asList(parentWorlds));
  100. }
  101. @Override
  102. public PermissionUser getUser(String userName) {
  103. return new FileUser(userName, manager, this);
  104. }
  105. @Override
  106. public PermissionGroup getGroup(String groupName) {
  107. return new FileGroup(groupName, manager, this);
  108. }
  109. @Override
  110. public PermissionGroup getDefaultGroup(String worldName) {
  111. Map<String, ConfigurationNode> groupsMap = this.permissions.getNodesMap("groups");
  112. if (groupsMap == null || groupsMap.isEmpty()) {
  113. throw new RuntimeException("No groups defined. Check your permissions file.");
  114. }
  115. String defaultGroupProperty = "default";
  116. if (worldName != null) {
  117. defaultGroupProperty = "worlds." + worldName + "." + defaultGroupProperty;
  118. }
  119. for (Map.Entry<String, ConfigurationNode> entry : groupsMap.entrySet()) {
  120. if (entry.getValue().getBoolean(defaultGroupProperty, false)) {
  121. return this.manager.getGroup(entry.getKey());
  122. }
  123. }
  124. if (worldName == null) {
  125. throw new RuntimeException("Default user group are not defined. Please select one using \"default: true\" property");
  126. }
  127. return null;
  128. }
  129. @Override
  130. public void setDefaultGroup(PermissionGroup group, String worldName) {
  131. Map<String, ConfigurationNode> groupsMap = this.permissions.getNodesMap("groups");
  132. String defaultGroupProperty = "default";
  133. if (worldName != null) {
  134. defaultGroupProperty = "worlds." + worldName + "." + defaultGroupProperty;
  135. }
  136. for (Map.Entry<String, ConfigurationNode> entry : groupsMap.entrySet()) {
  137. if (!entry.getKey().equalsIgnoreCase(group.getName())
  138. && entry.getValue().getBoolean(defaultGroupProperty, false)) {
  139. entry.getValue().removeProperty(defaultGroupProperty);
  140. }
  141. if (entry.getKey().equalsIgnoreCase(group.getName())) {
  142. entry.getValue().setProperty(defaultGroupProperty, true);
  143. }
  144. }
  145. }
  146. @Override
  147. public PermissionGroup[] getGroups() {
  148. List<PermissionGroup> groups = new LinkedList<PermissionGroup>();
  149. Map<String, ConfigurationNode> groupsMap = this.permissions.getNodesMap("groups");
  150. for (Map.Entry<String, ConfigurationNode> entry : groupsMap.entrySet()) {
  151. groups.add(this.manager.getGroup(entry.getKey()));
  152. }
  153. Collections.sort(groups);
  154. return groups.toArray(new PermissionGroup[0]);
  155. }
  156. @Override
  157. public PermissionUser[] getRegisteredUsers() {
  158. List<PermissionUser> users = new LinkedList<PermissionUser>();
  159. Map<String, ConfigurationNode> userMap = this.permissions.getNodesMap("users");
  160. if (userMap != null) {
  161. for (Map.Entry<String, ConfigurationNode> entry : userMap.entrySet()) {
  162. users.add(this.manager.getUser(entry.getKey()));
  163. }
  164. }
  165. return users.toArray(new PermissionUser[]{});
  166. }
  167. @Override
  168. public void reload() {
  169. this.permissions.load();
  170. }
  171. public static Map<String, String> collectOptions(Map<String, Object> root) {
  172. return collectOptions(root, "", new HashMap<String, String>());
  173. }
  174. protected static Map<String, String> collectOptions(Map<String, Object> root, String baseKey, Map<String, String> collector) {
  175. for (Map.Entry<String, Object> entry : root.entrySet()) {
  176. String newKey = entry.getKey();
  177. if (baseKey != null && !baseKey.isEmpty()) {
  178. newKey = baseKey + "." + newKey;
  179. }
  180. if (entry.getValue() instanceof Map) {
  181. Map<String, Object> map = (Map<String, Object>) entry.getValue();
  182. collectOptions(map, newKey, collector);
  183. } else if (entry.getValue() instanceof ConfigurationNode) {
  184. collectOptions(((ConfigurationNode) entry.getValue()).getRoot(), newKey, collector);
  185. } else {
  186. collector.put(newKey, (String) entry.getValue());
  187. }
  188. }
  189. return collector;
  190. }
  191. @Override
  192. public void dumpData(OutputStreamWriter writer) throws IOException {
  193. DumperOptions options = new DumperOptions();
  194. options.setIndent(4);
  195. options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  196. Yaml yaml = new Yaml(new SafeConstructor(), new Representer(), options);
  197. ConfigurationNode root = new ConfigurationNode();
  198. // Users setup
  199. for (PermissionUser user : this.manager.getUsers()) {
  200. // Inheritance
  201. if (user.getGroupsNames().length > 0) {
  202. root.setProperty("users." + user.getName() + ".group", Arrays.asList(user.getGroupsNames()));
  203. }
  204. // Prefix
  205. if (user.getOwnPrefix() != null && !user.getOwnPrefix().isEmpty()) {
  206. root.setProperty("users." + user.getName() + ".prefix", user.getOwnPrefix());
  207. }
  208. //Suffix
  209. if (user.getOwnSuffix() != null && !user.getOwnSuffix().isEmpty()) {
  210. root.setProperty("users." + user.getName() + ".suffix", user.getOwnSuffix());
  211. }
  212. // Permissions
  213. for (Map.Entry<String, String[]> entry : user.getAllPermissions().entrySet()) {
  214. String nodePath = "users." + user.getName();
  215. if (!entry.getKey().isEmpty()) {
  216. nodePath += ".worlds." + entry.getKey();
  217. }
  218. nodePath += ".permissions";
  219. if (entry.getValue().length > 0) {
  220. root.setProperty(nodePath, Arrays.asList(entry.getValue()));
  221. }
  222. }
  223. // Options
  224. for (Map.Entry<String, Map<String, String>> entry : user.getAllOptions().entrySet()) {
  225. String nodePath = "users." + user.getName();
  226. if (!entry.getKey().isEmpty()) {
  227. nodePath += "worlds." + entry.getKey();
  228. }
  229. nodePath += ".options";
  230. if (entry.getValue().size() > 0) {
  231. root.setProperty(nodePath, entry.getValue());
  232. }
  233. }
  234. }
  235. PermissionGroup defaultGroup = this.manager.getDefaultGroup();
  236. // Groups
  237. for (PermissionGroup group : this.manager.getGroups()) {
  238. // Inheritance
  239. if (group.getParentGroupsNames().length > 0) {
  240. root.setProperty("groups." + group.getName() + ".inheritance", Arrays.asList(group.getParentGroupsNames()));
  241. }
  242. // Prefix
  243. if (group.getOwnPrefix() != null && !group.getOwnPrefix().isEmpty()) {
  244. root.setProperty("groups." + group.getName() + ".prefix", group.getOwnPrefix());
  245. }
  246. //Suffix
  247. if (group.getOwnSuffix() != null && !group.getOwnSuffix().isEmpty()) {
  248. root.setProperty("groups." + group.getName() + ".suffix", group.getOwnSuffix());
  249. }
  250. if (group.equals(defaultGroup)) {
  251. root.setProperty("groups." + group.getName() + ".default", true);
  252. }
  253. // Permissions
  254. for (Map.Entry<String, String[]> entry : group.getAllPermissions().entrySet()) {
  255. String nodePath = "groups." + group.getName();
  256. if (!entry.getKey().isEmpty()) {
  257. nodePath += ".worlds." + entry.getKey();
  258. }
  259. nodePath += ".permissions";
  260. if (entry.getValue().length > 0) {
  261. root.setProperty(nodePath, Arrays.asList(entry.getValue()));
  262. }
  263. }
  264. // Options
  265. for (Map.Entry<String, Map<String, String>> entry : group.getAllOptions().entrySet()) {
  266. String nodePath = "groups." + group.getName();
  267. if (!entry.getKey().isEmpty()) {
  268. nodePath += "worlds." + entry.getKey();
  269. }
  270. nodePath += ".options";
  271. if (entry.getValue().size() > 0) {
  272. root.setProperty(nodePath, entry.getValue());
  273. }
  274. }
  275. }
  276. // World inheritance
  277. for (World world : Bukkit.getServer().getWorlds()) {
  278. String[] parentWorlds = manager.getWorldInheritance(world.getName());
  279. if (parentWorlds.length == 0) {
  280. continue;
  281. }
  282. root.setProperty("worlds." + world.getName() + ".inheritance", Arrays.asList(parentWorlds));
  283. }
  284. // Write data to writer
  285. yaml.dump(root.getRoot(), writer);
  286. }
  287. }