L2AdminCommandAccessRights.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.model;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.Map;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastMap;
  22. import net.sf.l2j.L2DatabaseFactory;
  23. import net.sf.l2j.gameserver.datatables.AccessLevels;
  24. import net.sf.l2j.gameserver.datatables.AdminCommandAccessRight;
  25. /**
  26. * @author FBIagent<br>
  27. */
  28. public class L2AdminCommandAccessRights
  29. {
  30. /** The logger<br> */
  31. private static Logger _log = Logger.getLogger(L2AdminCommandAccessRights.class.getName());
  32. /** The one and only instance of this class, retriveable by getInstance()<br> */
  33. private static L2AdminCommandAccessRights _instance = null;
  34. /** The access rights<br> */
  35. private Map< String, AdminCommandAccessRight > _adminCommandAccessRights = new FastMap< String, AdminCommandAccessRight >();
  36. /**
  37. * Loads admin command access rights from database<br>
  38. */
  39. private L2AdminCommandAccessRights()
  40. {
  41. java.sql.Connection con = null;
  42. try
  43. {
  44. con = L2DatabaseFactory.getInstance().getConnection();
  45. PreparedStatement stmt = con.prepareStatement("SELECT * FROM admin_command_access_rights");
  46. ResultSet rset = stmt.executeQuery();
  47. String adminCommand = null;
  48. String accessLevels = null;
  49. while (rset.next())
  50. {
  51. adminCommand = rset.getString("adminCommand");
  52. accessLevels = rset.getString("accessLevels");
  53. _adminCommandAccessRights.put(adminCommand, new AdminCommandAccessRight(adminCommand, accessLevels));
  54. }
  55. rset.close();
  56. stmt.close();
  57. }
  58. catch (SQLException e)
  59. {
  60. _log.warning("AdminCommandAccessRights: Error loading from database:" + e);
  61. }
  62. finally
  63. {
  64. try
  65. {
  66. con.close();
  67. }
  68. catch (Exception e)
  69. {
  70. }
  71. }
  72. _log.info("AdminCommandAccessRights: Loaded " + _adminCommandAccessRights.size() + " from database.");
  73. }
  74. /**
  75. * Returns the one and only instance of this class<br><br>
  76. *
  77. * @return AdminCommandAccessRights: the one and only instance of this class<br>
  78. */
  79. public static L2AdminCommandAccessRights getInstance()
  80. {
  81. return _instance == null ? (_instance = new L2AdminCommandAccessRights()) : _instance;
  82. }
  83. public boolean hasAccess(String adminCommand, L2AccessLevel accessLevel)
  84. {
  85. if (!accessLevel.isGm())
  86. return false;
  87. if (accessLevel.getLevel() == AccessLevels._masterAccessLevelNum)
  88. return true;
  89. AdminCommandAccessRight acar = _adminCommandAccessRights.get(adminCommand);
  90. if (acar == null)
  91. {
  92. _log.info("AdminCommandAccessRights: No rights defined for admin command " + adminCommand + ".");
  93. return false;
  94. }
  95. return acar.hasAccess(accessLevel);
  96. }
  97. }