PageRenderTime 152ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Skull3x/WorldEdit
Java | 248 lines | 178 code | 50 blank | 20 comment | 40 complexity | 06ede97e16c1421af0500f0d84a4dd25 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.YAMLProcessor;
  21. import org.bukkit.OfflinePlayer;
  22. import org.bukkit.Server;
  23. import java.io.BufferedReader;
  24. import java.io.File;
  25. import java.io.FileReader;
  26. import java.io.IOException;
  27. import java.util.Arrays;
  28. import java.util.HashMap;
  29. import java.util.HashSet;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;
  34. public class FlatFilePermissionsResolver implements PermissionsResolver {
  35. private static final Logger log = Logger.getLogger(FlatFilePermissionsResolver.class.getCanonicalName());
  36. private Map<String, Set<String>> userPermissionsCache;
  37. private Set<String> defaultPermissionsCache;
  38. private Map<String, Set<String>> userGroups;
  39. private final File groupFile;
  40. private final File userFile;
  41. public static PermissionsResolver factory(Server server, YAMLProcessor config) {
  42. File groups = new File("perms_groups.txt");
  43. File users = new File("perms_users.txt");
  44. if (!groups.exists() || !users.exists()) {
  45. return null;
  46. }
  47. return new FlatFilePermissionsResolver(groups, users);
  48. }
  49. public FlatFilePermissionsResolver() {
  50. this(new File("perms_groups.txt"), new File("perms_users.txt"));
  51. }
  52. public FlatFilePermissionsResolver(File groupFile, File userFile) {
  53. this.groupFile = groupFile;
  54. this.userFile = userFile;
  55. }
  56. @Deprecated
  57. public static boolean filesExists() {
  58. return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists();
  59. }
  60. public Map<String, Set<String>> loadGroupPermissions() {
  61. Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
  62. BufferedReader buff = null;
  63. try {
  64. FileReader input = new FileReader(this.groupFile);
  65. buff = new BufferedReader(input);
  66. String line;
  67. while ((line = buff.readLine()) != null) {
  68. line = line.trim();
  69. // Blank line
  70. if (line.isEmpty()) {
  71. continue;
  72. } else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
  73. continue;
  74. }
  75. String[] parts = line.split(":");
  76. String key = parts[0];
  77. if (parts.length > 1) {
  78. String[] perms = parts[1].split(",");
  79. Set<String> groupPerms = new HashSet<String>(Arrays.asList(perms));
  80. userGroupPermissions.put(key, groupPerms);
  81. }
  82. }
  83. } catch (IOException e) {
  84. log.log(Level.WARNING, "Failed to load permissions", e);
  85. } finally {
  86. try {
  87. if (buff != null) {
  88. buff.close();
  89. }
  90. } catch (IOException ignored) {
  91. }
  92. }
  93. return userGroupPermissions;
  94. }
  95. @Override
  96. public void load() {
  97. userGroups = new HashMap<String, Set<String>>();
  98. userPermissionsCache = new HashMap<String, Set<String>>();
  99. defaultPermissionsCache = new HashSet<String>();
  100. Map<String, Set<String>> userGroupPermissions = loadGroupPermissions();
  101. if (userGroupPermissions.containsKey("default")) {
  102. defaultPermissionsCache = userGroupPermissions.get("default");
  103. }
  104. BufferedReader buff = null;
  105. try {
  106. FileReader input = new FileReader(this.userFile);
  107. buff = new BufferedReader(input);
  108. String line;
  109. while ((line = buff.readLine()) != null) {
  110. Set<String> permsCache = new HashSet<String>();
  111. line = line.trim();
  112. // Blank line
  113. if (line.isEmpty()) {
  114. continue;
  115. } else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
  116. continue;
  117. }
  118. String[] parts = line.split(":");
  119. String key = parts[0];
  120. if (parts.length > 1) {
  121. String[] groups = (parts[1] + ",default").split(",");
  122. String[] perms = parts.length > 2 ? parts[2].split(",") : new String[0];
  123. permsCache.addAll(Arrays.asList(perms));
  124. for (String group : groups) {
  125. Set<String> groupPerms = userGroupPermissions.get(group);
  126. if (groupPerms != null) {
  127. permsCache.addAll(groupPerms);
  128. }
  129. }
  130. userPermissionsCache.put(key.toLowerCase(), permsCache);
  131. userGroups.put(key.toLowerCase(), new HashSet<String>(Arrays.asList(groups)));
  132. }
  133. }
  134. } catch (IOException e) {
  135. log.log(Level.WARNING, "Failed to load permissions", e);
  136. } finally {
  137. try {
  138. if (buff != null) {
  139. buff.close();
  140. }
  141. } catch (IOException ignored) {
  142. }
  143. }
  144. }
  145. @Override
  146. public boolean hasPermission(String player, String permission) {
  147. int dotPos = permission.lastIndexOf(".");
  148. if (dotPos > -1) {
  149. if (hasPermission(player, permission.substring(0, dotPos))) {
  150. return true;
  151. }
  152. }
  153. Set<String> perms = userPermissionsCache.get(player.toLowerCase());
  154. if (perms == null) {
  155. return defaultPermissionsCache.contains(permission)
  156. || defaultPermissionsCache.contains("*");
  157. }
  158. return perms.contains("*") || perms.contains(permission);
  159. }
  160. @Override
  161. public boolean hasPermission(String worldName, String player, String permission) {
  162. return hasPermission(player, "worlds." + worldName + "." + permission)
  163. || hasPermission(player, permission);
  164. }
  165. @Override
  166. public boolean inGroup(String player, String group) {
  167. Set<String> groups = userGroups.get(player.toLowerCase());
  168. return groups != null && groups.contains(group);
  169. }
  170. @Override
  171. public String[] getGroups(String player) {
  172. Set<String> groups = userGroups.get(player.toLowerCase());
  173. if (groups == null) {
  174. return new String[0];
  175. }
  176. return groups.toArray(new String[groups.size()]);
  177. }
  178. @Override
  179. public boolean hasPermission(OfflinePlayer player, String permission) {
  180. return hasPermission(player.getName(), permission);
  181. }
  182. @Override
  183. public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
  184. return hasPermission(worldName, player.getName(), permission);
  185. }
  186. @Override
  187. public boolean inGroup(OfflinePlayer player, String group) {
  188. return inGroup(player.getName(), group);
  189. }
  190. @Override
  191. public String[] getGroups(OfflinePlayer player) {
  192. return getGroups(player.getName());
  193. }
  194. @Override
  195. public String getDetectionMessage() {
  196. return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
  197. }
  198. }