PageRenderTime 114ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/NuclearW/PermissionsEx
Java | 454 lines | 218 code | 67 blank | 169 comment | 42 complexity | 5eafc6795dc7753d3f834671e5420fd5 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.util.HashMap;
  21. import java.util.Map;
  22. import java.util.Timer;
  23. import java.util.TimerTask;
  24. import java.util.logging.Logger;
  25. import org.bukkit.Bukkit;
  26. import org.bukkit.entity.Player;
  27. import ru.tehkode.permissions.config.Configuration;
  28. import ru.tehkode.permissions.events.PermissionEntityEvent;
  29. import ru.tehkode.permissions.events.PermissionEvent;
  30. import ru.tehkode.permissions.events.PermissionSystemEvent;
  31. /**
  32. *
  33. * @author code
  34. */
  35. public class PermissionManager {
  36. public final static int TRANSIENT_PERMISSION = 0;
  37. protected static final Logger logger = Logger.getLogger("Minecraft");
  38. protected Map<String, PermissionUser> users = new HashMap<String, PermissionUser>();
  39. protected Map<String, PermissionGroup> groups = new HashMap<String, PermissionGroup>();
  40. protected Map<String, PermissionGroup> defaultGroups = new HashMap<String, PermissionGroup>();
  41. protected PermissionBackend backend = null;
  42. protected Configuration config;
  43. protected Timer timer = new Timer("PermissionsCleaner");
  44. protected boolean debugMode = false;
  45. public PermissionManager(Configuration config) {
  46. this.config = config;
  47. this.initBackend();
  48. this.debugMode = config.getBoolean("permissions.debug", false);
  49. }
  50. /**
  51. * Check if specified player has specified permission
  52. *
  53. * @param player player object
  54. * @param permission permission string to check against
  55. * @return true on success false otherwise
  56. */
  57. public boolean has(Player player, String permission) {
  58. return this.has(player.getName(), permission, player.getWorld().getName());
  59. }
  60. /**
  61. * Check if player has specified permission in world
  62. *
  63. * @param player player object
  64. * @param permission permission as string to check against
  65. * @param world world's name as string
  66. * @return true on success false otherwise
  67. */
  68. public boolean has(Player player, String permission, String world) {
  69. return this.has(player.getName(), permission, world);
  70. }
  71. /**
  72. * Check if player with name has permission in world
  73. *
  74. * @param player player object
  75. * @param permission permission as string to check against
  76. * @param world world's name as string
  77. * @return true on success false otherwise
  78. */
  79. public boolean has(String playerName, String permission, String world) {
  80. PermissionUser user = this.getUser(playerName);
  81. if (user == null) {
  82. return false;
  83. }
  84. return user.has(permission, world);
  85. }
  86. /**
  87. * Return user's object
  88. *
  89. * @param username get PermissionUser with given name
  90. * @return PermissionUser instance
  91. */
  92. public PermissionUser getUser(String username) {
  93. if (username == null || username.isEmpty()) {
  94. return null;
  95. }
  96. PermissionUser user = users.get(username.toLowerCase());
  97. if (user == null) {
  98. user = this.backend.getUser(username);
  99. if (user != null) {
  100. this.users.put(username.toLowerCase(), user);
  101. }
  102. }
  103. return user;
  104. }
  105. /**
  106. * Return object of specified player
  107. *
  108. * @param player player object
  109. * @return PermissionUser instance
  110. */
  111. public PermissionUser getUser(Player player) {
  112. return this.getUser(player.getName());
  113. }
  114. /**
  115. * Return all registered user objects
  116. *
  117. * @return PermissionUser array
  118. */
  119. public PermissionUser[] getUsers() {
  120. return backend.getUsers();
  121. }
  122. /**
  123. * Return all users in group
  124. *
  125. * @param groupName group's name
  126. * @return PermissionUser array
  127. */
  128. public PermissionUser[] getUsers(String groupName, String worldName) {
  129. return backend.getUsers(groupName, worldName);
  130. }
  131. public PermissionUser[] getUsers(String groupName) {
  132. return backend.getUsers(groupName);
  133. }
  134. /**
  135. * Return all users in group and descendant groups
  136. *
  137. * @param groupName group's name
  138. * @param inheritance true return members of descendant groups of specified group
  139. * @return PermissionUser array for groupnName
  140. */
  141. public PermissionUser[] getUsers(String groupName, String worldName, boolean inheritance) {
  142. return backend.getUsers(groupName, worldName, inheritance);
  143. }
  144. public PermissionUser[] getUsers(String groupName, boolean inheritance) {
  145. return backend.getUsers(groupName, inheritance);
  146. }
  147. /**
  148. * Reset in-memory object of specified group
  149. *
  150. * @param userName user's name
  151. */
  152. public void resetUser(String userName) {
  153. this.users.remove(userName);
  154. }
  155. /**
  156. * Return object for specified group
  157. *
  158. * @param groupname group's name
  159. * @return PermissionGroup object
  160. */
  161. public PermissionGroup getGroup(String groupname) {
  162. if (groupname == null || groupname.isEmpty()) {
  163. return null;
  164. }
  165. PermissionGroup group = groups.get(groupname.toLowerCase());
  166. if (group == null) {
  167. group = this.backend.getGroup(groupname);
  168. if (group != null) {
  169. this.groups.put(groupname.toLowerCase(), group);
  170. }
  171. }
  172. return group;
  173. }
  174. /**
  175. * Return all groups
  176. *
  177. * @return PermissionGroup array
  178. */
  179. public PermissionGroup[] getGroups() {
  180. return backend.getGroups();
  181. }
  182. /**
  183. * Return all child groups of specified group
  184. *
  185. * @param groupName group's name
  186. * @return PermissionGroup array
  187. */
  188. public PermissionGroup[] getGroups(String groupName, String worldName) {
  189. return backend.getGroups(groupName, worldName);
  190. }
  191. public PermissionGroup[] getGroups(String groupName) {
  192. return backend.getGroups(groupName);
  193. }
  194. /**
  195. * Return all descendants or child groups for groupName
  196. *
  197. * @param groupName group's name
  198. * @param inheritance true: only direct child groups would be returned
  199. * @return PermissionGroup array for specified groupName
  200. */
  201. public PermissionGroup[] getGroups(String groupName, String worldName, boolean inheritance) {
  202. return backend.getGroups(groupName, worldName, inheritance);
  203. }
  204. public PermissionGroup[] getGroups(String groupName, boolean inheritance) {
  205. return backend.getGroups(groupName, inheritance);
  206. }
  207. /**
  208. * Return default group object
  209. *
  210. * @return default group object. null if not specified
  211. */
  212. public PermissionGroup getDefaultGroup(String worldName) {
  213. String worldIndex = worldName != null ? worldName : "";
  214. if (!this.defaultGroups.containsKey(worldIndex)) {
  215. this.defaultGroups.put(worldIndex, this.getDefaultGroup(worldName, this.getDefaultGroup(null, null)));
  216. }
  217. return this.defaultGroups.get(worldIndex);
  218. }
  219. public PermissionGroup getDefaultGroup() {
  220. return this.getDefaultGroup(null);
  221. }
  222. protected PermissionGroup getDefaultGroup(String worldName, PermissionGroup fallback) {
  223. PermissionGroup defaultGroup = this.backend.getDefaultGroup(worldName);
  224. if (defaultGroup == null && worldName == null) {
  225. throw new IllegalStateException("No default group defined. Use \"pex default group set <group> [world]\" to define default group.");
  226. }
  227. if (defaultGroup != null) {
  228. return defaultGroup;
  229. }
  230. if (worldName != null) {
  231. // check world-inheritance
  232. for (String parentWorld : this.getWorldInheritance(worldName)) {
  233. defaultGroup = this.getDefaultGroup(parentWorld, null);
  234. if (defaultGroup != null) {
  235. return defaultGroup;
  236. }
  237. }
  238. }
  239. return fallback;
  240. }
  241. /**
  242. * Set default group to specified group
  243. *
  244. * @param group PermissionGroup group object
  245. */
  246. public void setDefaultGroup(PermissionGroup group, String worldName) {
  247. if (group == null || group.equals(this.defaultGroups)) {
  248. return;
  249. }
  250. backend.setDefaultGroup(group, worldName);
  251. this.defaultGroups.clear();
  252. this.callEvent(PermissionSystemEvent.Action.DEFAULTGROUP_CHANGED);
  253. this.callEvent(new PermissionEntityEvent(group, PermissionEntityEvent.Action.DEFAULTGROUP_CHANGED));
  254. }
  255. public void setDefaultGroup(PermissionGroup group) {
  256. this.setDefaultGroup(group, null);
  257. }
  258. /**
  259. * Reset in-memory object for groupName
  260. *
  261. * @param groupName group's name
  262. */
  263. public void resetGroup(String groupName) {
  264. this.groups.remove(groupName);
  265. }
  266. /**
  267. * Set debug mode
  268. *
  269. * @param debug true enables debug mode, false disables
  270. */
  271. public void setDebug(boolean debug) {
  272. this.debugMode = debug;
  273. this.callEvent(PermissionSystemEvent.Action.DEBUGMODE_TOGGLE);
  274. }
  275. /**
  276. * Return current state of debug mode
  277. *
  278. * @return true debug is enabled, false if disabled
  279. */
  280. public boolean isDebug() {
  281. return this.debugMode;
  282. }
  283. /**
  284. * Return groups of specified rank ladder
  285. *
  286. * @param ladderName
  287. * @return Map of ladder, key - rank of group, value - group object. Empty map if ladder does not exist
  288. */
  289. public Map<Integer, PermissionGroup> getRankLadder(String ladderName) {
  290. Map<Integer, PermissionGroup> ladder = new HashMap<Integer, PermissionGroup>();
  291. for (PermissionGroup group : this.getGroups()) {
  292. if (!group.isRanked()) {
  293. continue;
  294. }
  295. if (group.getRankLadder().equalsIgnoreCase(ladderName)) {
  296. ladder.put(group.getRank(), group);
  297. }
  298. }
  299. return ladder;
  300. }
  301. /**
  302. * Return array of world names who has world inheritance
  303. *
  304. * @param world World name
  305. * @return Array of parent world, if world does not exist return empty array
  306. */
  307. public String[] getWorldInheritance(String worldName) {
  308. return backend.getWorldInheritance(worldName);
  309. }
  310. /**
  311. * Set world inheritance parents for world
  312. *
  313. * @param world world name which inheritance should be set
  314. * @param parentWorlds array of parent world names
  315. */
  316. public void setWorldInheritance(String world, String[] parentWorlds) {
  317. backend.setWorldInheritance(world, parentWorlds);
  318. this.callEvent(PermissionSystemEvent.Action.WORLDINHERITANCE_CHANGED);
  319. }
  320. /**
  321. * Return current backend
  322. *
  323. * @return current backend object
  324. */
  325. public PermissionBackend getBackend() {
  326. return this.backend;
  327. }
  328. /**
  329. * Set backend to specified backend.
  330. * This would also cause backend resetting.
  331. *
  332. * @param backendName name of backend to set to
  333. */
  334. public void setBackend(String backendName) {
  335. synchronized (this) {
  336. this.clearCache();
  337. this.backend = PermissionBackend.getBackend(backendName, this, config);
  338. this.backend.initialize();
  339. }
  340. this.callEvent(PermissionSystemEvent.Action.BACKEND_CHANGED);
  341. }
  342. /**
  343. * Register new timer task
  344. *
  345. * @param task TimerTask object
  346. * @param delay delay in seconds
  347. */
  348. protected void registerTask(TimerTask task, int delay) {
  349. if (delay == TRANSIENT_PERMISSION) {
  350. return;
  351. }
  352. timer.schedule(task, delay * 1000);
  353. }
  354. /**
  355. * Reset all in-memory groups and users, clean up runtime stuff, reloads backend
  356. */
  357. public void reset() {
  358. this.clearCache();
  359. if (this.backend != null) {
  360. this.backend.reload();
  361. }
  362. this.callEvent(PermissionSystemEvent.Action.RELOADED);
  363. }
  364. protected void clearCache() {
  365. this.users.clear();
  366. this.groups.clear();
  367. this.defaultGroups.clear();
  368. // Close old timed Permission Timer
  369. timer.cancel();
  370. timer = new Timer("PermissionsCleaner");
  371. }
  372. private void initBackend() {
  373. String backendName = this.config.getString("permissions.backend");
  374. if (backendName == null || backendName.isEmpty()) {
  375. backendName = PermissionBackend.defaultBackend; //Default backend
  376. this.config.setProperty("permissions.backend", backendName);
  377. this.config.save();
  378. }
  379. this.setBackend(backendName);
  380. }
  381. protected void callEvent(PermissionEvent event) {
  382. Bukkit.getServer().getPluginManager().callEvent(event);
  383. }
  384. protected void callEvent(PermissionSystemEvent.Action action) {
  385. this.callEvent(new PermissionSystemEvent(action));
  386. }
  387. }