PageRenderTime 123ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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