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

/src/main/java/ru/tehkode/permissions/PermissionBackend.java

https://gitlab.com/Slind/PermissionsEx
Java | 425 lines | 181 code | 67 blank | 177 comment | 26 complexity | 4f05f4feb4e969999f98d208e4e78f00 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;
  20. import java.io.IOException;
  21. import java.io.OutputStreamWriter;
  22. import java.lang.reflect.Constructor;
  23. import java.util.*;
  24. import java.util.logging.Logger;
  25. import org.bukkit.Bukkit;
  26. import org.bukkit.World;
  27. import org.bukkit.configuration.Configuration;
  28. import org.bukkit.entity.Player;
  29. import ru.tehkode.permissions.bukkit.PermissionsEx;
  30. import ru.tehkode.permissions.exceptions.PermissionBackendException;
  31. /**
  32. * @author t3hk0d3
  33. */
  34. public abstract class PermissionBackend {
  35. protected final static String defaultBackend = "file";
  36. protected PermissionManager manager;
  37. protected Configuration config;
  38. protected boolean createUserRecords = false;
  39. protected PermissionBackend(PermissionManager manager, Configuration config) {
  40. this.manager = manager;
  41. this.config = config;
  42. this.createUserRecords = config.getBoolean("permissions.createUserRecords", this.createUserRecords);
  43. }
  44. /**
  45. * Backend initialization should be done here
  46. */
  47. public abstract void initialize() throws PermissionBackendException;
  48. /**
  49. * Returns new PermissionUser object for specified player name
  50. *
  51. * @param name Player name
  52. * @return PermissionUser for specified player, or null on error.
  53. */
  54. public abstract PermissionUser getUser(String name);
  55. /**
  56. * Returns new PermissionGroup object for specified group name
  57. *
  58. * @param name Group name
  59. * @return PermissionGroup object, or null on error
  60. */
  61. public abstract PermissionGroup getGroup(String name);
  62. /*
  63. * Creates new group with specified name, or returns PermissionGroup object,
  64. * if there is such group already exists.
  65. *
  66. * @param name Group name
  67. * @returns PermissionGroup instance for specified group
  68. */
  69. public PermissionGroup createGroup(String name) {
  70. return this.manager.getGroup(name);
  71. }
  72. /**
  73. * Removes the specified group
  74. *
  75. * @param groupName Name of the group which should be removed
  76. * @return true if group was removed, false if group has child groups
  77. */
  78. public boolean removeGroup(String groupName) {
  79. if (this.getGroups(groupName).length > 0) {
  80. return false;
  81. }
  82. for (PermissionUser user : this.getUsers(groupName)) {
  83. user.removeGroup(groupName);
  84. }
  85. this.manager.getGroup(groupName).remove();
  86. return true;
  87. }
  88. /**
  89. * Returns default group, a group that is assigned to a user without a group set
  90. *
  91. * @return Default group instance
  92. */
  93. public abstract PermissionGroup getDefaultGroup(String worldName);
  94. /**
  95. * Set group as default group
  96. *
  97. * @param group
  98. */
  99. public abstract void setDefaultGroup(PermissionGroup group, String worldName);
  100. /**
  101. * Returns an array of world names of specified world name
  102. *
  103. * @param world world name
  104. * @return Array of parent worlds. If there is no parent world return empty array
  105. */
  106. public abstract String[] getWorldInheritance(String world);
  107. /**
  108. * Set world inheritance parents for specified world
  109. *
  110. * @param world world name which inheritance should be set
  111. * @param parentWorlds array of parent world names
  112. */
  113. public abstract void setWorldInheritance(String world, String[] parentWorlds);
  114. /**
  115. * Return all registered groups
  116. *
  117. * @return
  118. */
  119. public abstract PermissionGroup[] getGroups();
  120. /**
  121. * Return child groups of specified group
  122. *
  123. * @param groupName
  124. * @return empty array if group has no children, empty or not exist
  125. */
  126. public PermissionGroup[] getGroups(String groupName) {
  127. return this.getGroups(groupName, null);
  128. }
  129. public PermissionGroup[] getGroups(String groupName, String worldName) {
  130. return this.getGroups(groupName, worldName, false);
  131. }
  132. /**
  133. * Return child groups of specified group.
  134. *
  135. * @param groupName
  136. * @param inheritance - If true a full list of descendants will be returned
  137. * @return empty array if group has no children, empty or not exist
  138. */
  139. public PermissionGroup[] getGroups(String groupName, boolean inheritance) {
  140. Set<PermissionGroup> groups = new HashSet<PermissionGroup>();
  141. for (World world : Bukkit.getServer().getWorlds()) {
  142. groups.addAll(Arrays.asList(getGroups(groupName, world.getName(), inheritance)));
  143. }
  144. // Common space users
  145. groups.addAll(Arrays.asList(getGroups(groupName, null, inheritance)));
  146. return groups.toArray(new PermissionGroup[0]);
  147. }
  148. public PermissionGroup[] getGroups(String groupName, String worldName, boolean inheritance) {
  149. List<PermissionGroup> groups = new LinkedList<PermissionGroup>();
  150. for (PermissionGroup group : this.getGroups()) {
  151. if (!groups.contains(group) && group.isChildOf(groupName, worldName, inheritance)) {
  152. groups.add(group);
  153. }
  154. }
  155. return groups.toArray(new PermissionGroup[0]);
  156. }
  157. /**
  158. * Return all registered and online users
  159. *
  160. * @return
  161. */
  162. public PermissionUser[] getUsers() {
  163. Set<PermissionUser> users = new HashSet<PermissionUser>();
  164. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
  165. users.add(this.manager.getUser(player));
  166. }
  167. users.addAll(Arrays.asList(this.getRegisteredUsers()));
  168. return users.toArray(new PermissionUser[0]);
  169. }
  170. /**
  171. * Return all registered users
  172. *
  173. * @return
  174. */
  175. public abstract PermissionUser[] getRegisteredUsers();
  176. public Collection<String> getRegisteredUserNames() {
  177. PermissionUser[] users = getRegisteredUsers();
  178. List<String> ret = new ArrayList<String>(users.length);
  179. for (PermissionUser user : users) {
  180. ret.add(user.getName());
  181. }
  182. return Collections.unmodifiableCollection(ret);
  183. }
  184. public Collection<String> getRegisteredGroupNames() {
  185. PermissionGroup[] groups = getGroups();
  186. List<String> ret = new ArrayList<String>(groups.length);
  187. for (PermissionGroup group : groups) {
  188. ret.add(group.getName());
  189. }
  190. return Collections.unmodifiableCollection(ret);
  191. }
  192. /**
  193. * Return users of specified group.
  194. *
  195. * @param groupName
  196. * @return null if there is no such group
  197. */
  198. public PermissionUser[] getUsers(String groupName) {
  199. return getUsers(groupName, false);
  200. }
  201. public PermissionUser[] getUsers(String groupName, String worldName) {
  202. return getUsers(groupName, worldName, false);
  203. }
  204. /**
  205. * Return users of specified group (and child groups)
  206. *
  207. * @param groupName
  208. * @param inheritance - If true return users list of descendant groups too
  209. * @return
  210. */
  211. public PermissionUser[] getUsers(String groupName, boolean inheritance) {
  212. Set<PermissionUser> users = new HashSet<PermissionUser>();
  213. for (PermissionUser user : this.getUsers()) {
  214. if (user.inGroup(groupName, inheritance)) {
  215. users.add(user);
  216. }
  217. }
  218. return users.toArray(new PermissionUser[0]);
  219. }
  220. public PermissionUser[] getUsers(String groupName, String worldName, boolean inheritance) {
  221. Set<PermissionUser> users = new HashSet<PermissionUser>();
  222. for (PermissionUser user : this.getUsers()) {
  223. if (user.inGroup(groupName, worldName, inheritance)) {
  224. users.add(user);
  225. }
  226. }
  227. return users.toArray(new PermissionUser[0]);
  228. }
  229. /**
  230. * Reload backend (reread permissions file, reconnect to database, etc)
  231. */
  232. public abstract void reload() throws PermissionBackendException;
  233. /**
  234. * Dump data to native backend format
  235. *
  236. * @param writer Writer where dumped data should be written to
  237. * @throws IOException
  238. */
  239. public abstract void dumpData(OutputStreamWriter writer) throws IOException;
  240. /**
  241. * Array of backend aliases
  242. */
  243. protected static Map<String, Class<? extends PermissionBackend>> registedAliases = new HashMap<String, Class<? extends PermissionBackend>>();
  244. /**
  245. * Return class name for alias
  246. *
  247. * @param alias
  248. * @return Class name if found or alias if there is no such class name present
  249. */
  250. public static String getBackendClassName(String alias) {
  251. if (registedAliases.containsKey(alias)) {
  252. return registedAliases.get(alias).getName();
  253. }
  254. return alias;
  255. }
  256. /**
  257. * Returns Class object for specified alias, if there is no alias registered
  258. * then try to find it using Class.forName(alias)
  259. *
  260. * @param alias
  261. * @return
  262. * @throws ClassNotFoundException
  263. */
  264. public static Class<? extends PermissionBackend> getBackendClass(String alias) throws ClassNotFoundException {
  265. if (!registedAliases.containsKey(alias)) {
  266. return (Class<? extends PermissionBackend>) Class.forName(alias);
  267. }
  268. return registedAliases.get(alias);
  269. }
  270. /**
  271. * Register new alias for specified backend class
  272. *
  273. * @param alias
  274. * @param backendClass
  275. */
  276. public static void registerBackendAlias(String alias, Class<? extends PermissionBackend> backendClass) {
  277. if (!PermissionBackend.class.isAssignableFrom(backendClass)) {
  278. throw new RuntimeException("Provided class should be subclass of PermissionBackend");
  279. }
  280. registedAliases.put(alias, backendClass);
  281. Logger.getLogger("Minecraft").info("[PermissionsEx] " + alias + " backend registered!");
  282. }
  283. /**
  284. * Return alias for specified backend class
  285. * If there is no such class registered the fullname of this class would
  286. * be returned using backendClass.getName();
  287. *
  288. * @param backendClass
  289. * @return alias or class fullname when not found using backendClass.getName()
  290. */
  291. public static String getBackendAlias(Class<? extends PermissionBackend> backendClass) {
  292. if (registedAliases.containsValue(backendClass)) {
  293. for (String alias : registedAliases.keySet()) { // Is there better way to find key by value?
  294. if (registedAliases.get(alias).equals(backendClass)) {
  295. return alias;
  296. }
  297. }
  298. }
  299. return backendClass.getName();
  300. }
  301. /**
  302. * Returns new backend class instance for specified backendName
  303. *
  304. * @param backendName Class name or alias of backend
  305. * @param config Configuration object to access backend settings
  306. * @return new instance of PermissionBackend object
  307. */
  308. public static PermissionBackend getBackend(String backendName, Configuration config) {
  309. return getBackend(backendName, PermissionsEx.getPermissionManager(), config, defaultBackend);
  310. }
  311. /**
  312. * Returns new Backend class instance for specified backendName
  313. *
  314. * @param backendName Class name or alias of backend
  315. * @param manager PermissionManager object
  316. * @param config Configuration object to access backend settings
  317. * @return new instance of PermissionBackend object
  318. */
  319. public static PermissionBackend getBackend(String backendName, PermissionManager manager, Configuration config) {
  320. return getBackend(backendName, manager, config, defaultBackend);
  321. }
  322. /**
  323. * Returns new Backend class instance for specified backendName
  324. *
  325. * @param backendName Class name or alias of backend
  326. * @param manager PermissionManager object
  327. * @param config Configuration object to access backend settings
  328. * @param fallBackBackend name of backend that should be used if specified backend was not found or failed to initialize
  329. * @return new instance of PermissionBackend object
  330. */
  331. public static PermissionBackend getBackend(String backendName, PermissionManager manager, Configuration config, String fallBackBackend) {
  332. if (backendName == null || backendName.isEmpty()) {
  333. backendName = defaultBackend;
  334. }
  335. String className = getBackendClassName(backendName);
  336. try {
  337. Class<? extends PermissionBackend> backendClass = getBackendClass(backendName);
  338. Logger.getLogger("Minecraft").info("[PermissionsEx] Initializing " + backendName + " backend");
  339. Constructor<? extends PermissionBackend> constructor = backendClass.getConstructor(PermissionManager.class, Configuration.class);
  340. return (PermissionBackend) constructor.newInstance(manager, config);
  341. } catch (ClassNotFoundException e) {
  342. Logger.getLogger("Minecraft").warning("[PermissionsEx] Specified backend \"" + backendName + "\" are not found.");
  343. if (fallBackBackend == null) {
  344. throw new RuntimeException(e);
  345. }
  346. if (!className.equals(getBackendClassName(fallBackBackend))) {
  347. return getBackend(fallBackBackend, manager, config, null);
  348. } else {
  349. throw new RuntimeException(e);
  350. }
  351. } catch (Exception e) {
  352. throw new RuntimeException(e);
  353. }
  354. }
  355. public boolean isCreateUserRecords() {
  356. return this.createUserRecords;
  357. }
  358. }