AdminCommandAccessRights.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 com.l2jserver.gameserver.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.Map;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.model.L2AccessLevel;
  24. import com.l2jserver.gameserver.model.L2AdminCommandAccessRight;
  25. import javolution.util.FastMap;
  26. /**
  27. * @author FBIagent<br>
  28. */
  29. public class AdminCommandAccessRights
  30. {
  31. /** The logger<br> */
  32. private static Logger _log = Logger.getLogger(AdminCommandAccessRights.class.getName());
  33. private Map<String, L2AdminCommandAccessRight> _adminCommandAccessRights;
  34. /**
  35. * Returns the one and only instance of this class<br><br>
  36. *
  37. * @return AdminCommandAccessRights: the one and only instance of this class<br>
  38. */
  39. public static AdminCommandAccessRights getInstance()
  40. {
  41. return SingletonHolder._instance;
  42. }
  43. /** The access rights<br> */
  44. private AdminCommandAccessRights()
  45. {
  46. loadAdminCommandAccessRights();
  47. }
  48. /**
  49. * Loads admin command access rights from database<br>
  50. */
  51. private void loadAdminCommandAccessRights()
  52. {
  53. _adminCommandAccessRights = new FastMap<String, L2AdminCommandAccessRight>();
  54. Connection con = null;
  55. try
  56. {
  57. con = L2DatabaseFactory.getInstance().getConnection();
  58. PreparedStatement stmt = con.prepareStatement("SELECT * FROM admin_command_access_rights");
  59. ResultSet rset = stmt.executeQuery();
  60. String adminCommand = null;
  61. String accessLevels = null;
  62. while (rset.next())
  63. {
  64. adminCommand = rset.getString("adminCommand");
  65. accessLevels = rset.getString("accessLevels");
  66. _adminCommandAccessRights.put(adminCommand, new L2AdminCommandAccessRight(adminCommand, accessLevels));
  67. }
  68. rset.close();
  69. stmt.close();
  70. }
  71. catch (SQLException e)
  72. {
  73. _log.warning("AdminCommandAccessRights: Error loading from database:" + e);
  74. }
  75. finally
  76. {
  77. try
  78. {
  79. con.close();
  80. }
  81. catch (Exception e)
  82. {
  83. }
  84. }
  85. _log.info("AdminCommandAccessRights: Loaded " + _adminCommandAccessRights.size() + " from database.");
  86. }
  87. public boolean hasAccess(String adminCommand, L2AccessLevel accessLevel)
  88. {
  89. if (!accessLevel.isGm())
  90. return false;
  91. if (accessLevel.getLevel() == AccessLevels._masterAccessLevelNum)
  92. return true;
  93. L2AdminCommandAccessRight acar = _adminCommandAccessRights.get(adminCommand);
  94. if (acar == null)
  95. {
  96. _log.info("AdminCommandAccessRights: No rights defined for admin command " + adminCommand + ".");
  97. return false;
  98. }
  99. return acar.hasAccess(accessLevel);
  100. }
  101. public void reloadAdminCommandAccessRights()
  102. {
  103. loadAdminCommandAccessRights();
  104. }
  105. @SuppressWarnings("synthetic-access")
  106. private static class SingletonHolder
  107. {
  108. protected static final AdminCommandAccessRights _instance = new AdminCommandAccessRights();
  109. }
  110. }