Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/permissions/PermissionsManager.java

143 lines
4.4 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.permissions;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.plugin.HabboPlugin;
2019-04-22 01:42:00 +02:00
import gnu.trove.map.hash.THashMap;
2018-07-06 15:30:00 +02:00
import gnu.trove.map.hash.TIntIntHashMap;
import gnu.trove.map.hash.TIntObjectHashMap;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 15:30:00 +02:00
import java.sql.*;
2019-04-22 01:42:00 +02:00
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class PermissionsManager {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(PermissionsManager.class);
2018-07-06 15:30:00 +02:00
private final TIntObjectHashMap<Rank> ranks;
private final TIntIntHashMap enables;
2019-04-22 01:42:00 +02:00
private final THashMap<String, List<Rank>> badges;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public PermissionsManager() {
long millis = System.currentTimeMillis();
this.ranks = new TIntObjectHashMap<>();
2018-07-06 15:30:00 +02:00
this.enables = new TIntIntHashMap();
2019-04-22 01:42:00 +02:00
this.badges = new THashMap<String, List<Rank>>();
2018-07-06 15:30:00 +02:00
this.reload();
2020-05-04 22:24:09 +02:00
LOGGER.info("Permissions Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public void reload() {
2018-07-06 15:30:00 +02:00
this.loadPermissions();
this.loadEnables();
}
2019-05-26 20:14:53 +02:00
private void loadPermissions() {
2019-04-22 01:42:00 +02:00
this.badges.clear();
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM permissions ORDER BY id ASC")) {
while (set.next()) {
Rank rank = null;
if (!this.ranks.containsKey(set.getInt("id"))) {
rank = new Rank(set);
this.ranks.put(set.getInt("id"), rank);
} else {
rank = this.ranks.get(set.getInt("id"));
rank.load(set);
}
2019-04-22 01:42:00 +02:00
if (rank != null && !rank.getBadge().isEmpty()) {
if (!this.badges.containsKey(rank.getBadge())) {
this.badges.put(rank.getBadge(), new ArrayList<Rank>());
2018-07-06 15:30:00 +02:00
}
this.badges.get(rank.getBadge()).add(rank);
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
private void loadEnables() {
synchronized (this.enables) {
2018-07-06 15:30:00 +02:00
this.enables.clear();
2019-05-26 20:14:53 +02:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM special_enables")) {
while (set.next()) {
2018-07-06 15:30:00 +02:00
this.enables.put(set.getInt("effect_id"), set.getInt("min_rank"));
}
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public boolean rankExists(int rankId) {
2018-07-06 15:30:00 +02:00
return this.ranks.containsKey(rankId);
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public Rank getRank(int rankId) {
2018-07-06 15:30:00 +02:00
return this.ranks.get(rankId);
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public Rank getRankByName(String rankName) {
for (Rank rank : this.ranks.valueCollection()) {
2018-07-06 15:30:00 +02:00
if (rank.getName().equalsIgnoreCase(rankName))
return rank;
}
return null;
}
2019-05-26 20:14:53 +02:00
public boolean isEffectBlocked(int effectId, int rank) {
2018-07-06 15:30:00 +02:00
return this.enables.contains(effectId) && this.enables.get(effectId) > rank;
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public boolean hasPermission(Habbo habbo, String permission) {
2018-07-06 15:30:00 +02:00
return this.hasPermission(habbo, permission, false);
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public boolean hasPermission(Habbo habbo, String permission, boolean withRoomRights) {
if (!this.hasPermission(habbo.getHabboInfo().getRank(), permission, withRoomRights)) {
for (HabboPlugin plugin : Emulator.getPluginManager().getPlugins()) {
if (plugin.hasPermission(habbo, permission)) {
2018-07-06 15:30:00 +02:00
return true;
}
}
return false;
}
2019-03-18 02:22:00 +01:00
return true;
2018-07-06 15:30:00 +02:00
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
public boolean hasPermission(Rank rank, String permission, boolean withRoomRights) {
2018-07-06 15:30:00 +02:00
return rank.hasPermission(permission, withRoomRights);
}
2019-04-22 01:42:00 +02:00
2019-05-26 20:14:53 +02:00
public Set<String> getStaffBadges() {
2019-04-22 01:42:00 +02:00
return this.badges.keySet();
}
2019-05-26 20:14:53 +02:00
public List<Rank> getRanksByBadgeCode(String code) {
2019-04-22 01:42:00 +02:00
return this.badges.get(code);
}
public List<Rank> getAllRanks() {
return new ArrayList<>(this.ranks.valueCollection());
}
2018-07-06 15:30:00 +02:00
}