UserPerm model
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2023-08-29 15:03:39 +02:00
Ursprung 0fef6c0f8f
Commit 469a504e48
3 geänderte Dateien mit 92 neuen und 21 gelöschten Zeilen

Datei anzeigen

@ -82,27 +82,27 @@ public class SteamwarUser {
SteamwarUser user = usersByName.get(userName.toLowerCase());
if(user != null)
return user;
return loadPunishments(byName.select(userName));
return byName.select(userName);
}
public static SteamwarUser get(UUID uuid){
SteamwarUser user = usersByUUID.get(uuid);
if(user != null)
return user;
return loadPunishments(byUUID.select(uuid));
return byUUID.select(uuid);
}
public static SteamwarUser get(int id) {
SteamwarUser user = usersById.get(id);
if(user != null)
return user;
return loadPunishments(byID.select(id));
return byID.select(id);
}
public static SteamwarUser get(Long discordId) {
if(usersByDiscord.containsKey(discordId))
return usersByDiscord.get(discordId);
return loadPunishments(byDiscord.select(discordId));
return byDiscord.select(discordId);
}
public static SteamwarUser getOrCreate(UUID uuid, String name, Consumer<UUID> newPlayer, BiConsumer<String, String> nameUpdate) {
@ -124,11 +124,11 @@ public class SteamwarUser {
}
public static List<SteamwarUser> getServerTeam() {
return loadPunishments(getServerTeam.listSelect());
return getServerTeam.listSelect();
}
public static List<SteamwarUser> getTeam(int teamId) {
return loadPunishments(byTeam.listSelect(teamId));
return byTeam.listSelect(teamId);
}
public static void batchCache(Set<Integer> ids) {
@ -137,23 +137,10 @@ public class SteamwarUser {
return;
try (SelectStatement<SteamwarUser> batch = new SelectStatement<>(table, "SELECT * FROM UserData WHERE id IN (" + ids.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")")) {
loadPunishments(batch.listSelect());
batch.listSelect();
}
}
private static List<SteamwarUser> loadPunishments(List<SteamwarUser> users) {
users.forEach(user -> user.punishments = Punishment.getPunishmentsOfPlayer(user.getId()));
return users;
}
private static SteamwarUser loadPunishments(SteamwarUser user) {
if(user == null)
return null;
user.punishments = Punishment.getPunishmentsOfPlayer(user.getId());
return user;
}
@Getter
@Field(keys = {Table.PRIMARY}, autoincrement = true)
private final int id;
@ -162,6 +149,7 @@ public class SteamwarUser {
@Getter
@Field
private String userName;
@Deprecated
@Getter
@Field(def = "'Member'")
private final UserGroup userGroup;
@ -179,7 +167,8 @@ public class SteamwarUser {
@Field(keys = {"discordId"}, nullable = true)
private Long discordId;
private Map<Punishment.PunishmentType, Punishment> punishments;
private Map<Punishment.PunishmentType, Punishment> punishments = null;
private Set<UserPerm> permissions = null;
public SteamwarUser(int id, UUID uuid, String userName, UserGroup userGroup, int team, boolean leader, Locale locale, boolean manualLocale, Long discordId) {
this.id = id;
@ -211,10 +200,12 @@ public class SteamwarUser {
}
public Punishment getPunishment(Punishment.PunishmentType type) {
initPunishments();
return punishments.getOrDefault(type, null);
}
public boolean isPunished(Punishment.PunishmentType punishment) {
initPunishments();
if (!punishments.containsKey(punishment)) {
return false;
}
@ -228,6 +219,11 @@ public class SteamwarUser {
return true;
}
public boolean hasPerm(UserPerm perm) {
initPerms();
return permissions.contains(perm);
}
public double getOnlinetime() {
return getPlaytime.select(rs -> {
if (rs.next() && rs.getBigDecimal("Playtime") != null)
@ -245,6 +241,7 @@ public class SteamwarUser {
}
public void punish(Punishment.PunishmentType punishment, Timestamp time, String banReason, int from, boolean perma) {
initPunishments();
punishments.remove(punishment);
punishments.put(punishment, Punishment.createPunishment(id, from, punishment, banReason, time, perma));
}
@ -277,4 +274,18 @@ public class SteamwarUser {
usersByDiscord.put(discordId, this);
}
}
private void initPunishments() {
if(punishments != null)
return;
punishments = Punishment.getPunishmentsOfPlayer(id);
}
private void initPerms() {
if(permissions != null)
return;
permissions = UserPerm.getPerms(id);
}
}

Datei anzeigen

@ -22,6 +22,7 @@ package de.steamwar.sql;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Deprecated
@AllArgsConstructor
public enum UserGroup {
Admin("§4", "§e", "Admin", true, true, true, true),

Datei anzeigen

@ -0,0 +1,59 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import de.steamwar.sql.internal.Field;
import de.steamwar.sql.internal.SelectStatement;
import de.steamwar.sql.internal.SqlTypeMapper;
import de.steamwar.sql.internal.Table;
import lombok.AllArgsConstructor;
import java.util.Set;
import java.util.stream.Collectors;
public enum UserPerm {
YOUTUBE,
GUIDE,
BUILD,
CHECK,
TEAM,
MODERATION,
DEVELOPMENT,
ADMIN;
static {
SqlTypeMapper.nameEnumMapper(UserPerm.class);
}
private static final Table<UserPermTable> table = new Table<>(UserPermTable.class, "UserPerm");
private static final SelectStatement<UserPermTable> getPerms = table.selectFields("user");
public static Set<UserPerm> getPerms(int user) {
return getPerms.listSelect(user).stream().map(up -> up.perm).collect(Collectors.toSet());
}
@AllArgsConstructor
private static class UserPermTable {
@Field(keys = {Table.PRIMARY})
private final int user;
@Field(keys = {Table.PRIMARY})
private final UserPerm perm;
}
}