AdminCommandAccessRights.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.datatables;
  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. /**
  24. * @author FBIagent<br>
  25. */
  26. public class AdminCommandAccessRights
  27. {
  28. /** The logger<br> */
  29. private static Logger _log = Logger.getLogger(AdminCommandAccessRights.class.getName());
  30. /** The one and only instance of this class, retriveable by getInstance()<br> */
  31. private static AdminCommandAccessRights _instance = null;
  32. /** The access rights<br> */
  33. private Map< String, AdminCommandAccessRight > _adminCommandAccessRights = new FastMap< String, AdminCommandAccessRight >();
  34. /**
  35. * Loads admin command access rights from database<br>
  36. */
  37. private AdminCommandAccessRights()
  38. {
  39. java.sql.Connection con = null;
  40. try
  41. {
  42. con = L2DatabaseFactory.getInstance().getConnection();
  43. PreparedStatement stmt = con.prepareStatement("SELECT * FROM admin_command_access_rights");
  44. ResultSet rset = stmt.executeQuery();
  45. String adminCommand = null;
  46. String accessLevels = null;
  47. while (rset.next())
  48. {
  49. adminCommand = rset.getString("adminCommand");
  50. accessLevels = rset.getString("accessLevels");
  51. _adminCommandAccessRights.put(adminCommand, new AdminCommandAccessRight(adminCommand, accessLevels));
  52. }
  53. rset.close();
  54. stmt.close();
  55. }
  56. catch (SQLException e)
  57. {
  58. _log.warning("AdminCommandAccessRights: Error loading from database:" + e);
  59. }
  60. finally
  61. {
  62. try
  63. {
  64. con.close();
  65. }
  66. catch (Exception e)
  67. {
  68. }
  69. }
  70. _log.info("AdminCommandAccessRights: Loaded " + _adminCommandAccessRights.size() + " from database.");
  71. }
  72. /**
  73. * Returns the one and only instance of this class<br><br>
  74. *
  75. * @return AdminCommandAccessRights: the one and only instance of this class<br>
  76. */
  77. public static AdminCommandAccessRights getInstance()
  78. {
  79. return _instance == null ? (_instance = new AdminCommandAccessRights()) : _instance;
  80. }
  81. public boolean hasAccess(String adminCommand, AccessLevel accessLevel)
  82. {
  83. if (!accessLevel.isGm())
  84. return false;
  85. if (accessLevel.getLevel() == AccessLevels._masterAccessLevelNum)
  86. return true;
  87. AdminCommandAccessRight acar = _adminCommandAccessRights.get(adminCommand);
  88. if (acar == null)
  89. {
  90. _log.info("AdminCommandAccessRights: No rights defined for admin command " + adminCommand + ".");
  91. return false;
  92. }
  93. return acar.hasAccess(accessLevel);
  94. }
  95. }