From 239781fbbda90c590a53f9b331b80af484752786 Mon Sep 17 00:00:00 2001 From: SebClem Date: Sat, 11 Jun 2022 17:25:21 +0200 Subject: [PATCH] :hammer: Add value endpoint --- .../Api/Controllers/SettingController.java | 13 ++- .../net/Broken/Api/Data/Settings/Value.java | 4 + .../CustomMethodSecurityExpressionRoot.java | 12 +++ .../Broken/Api/Services/SettingService.java | 91 +++++++++++++++++++ 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/Broken/Api/Data/Settings/Value.java diff --git a/src/main/java/net/Broken/Api/Controllers/SettingController.java b/src/main/java/net/Broken/Api/Controllers/SettingController.java index 10e5360..9968250 100644 --- a/src/main/java/net/Broken/Api/Controllers/SettingController.java +++ b/src/main/java/net/Broken/Api/Controllers/SettingController.java @@ -1,11 +1,10 @@ package net.Broken.Api.Controllers; import net.Broken.Api.Data.Settings.SettingGroup; +import net.Broken.Api.Data.Settings.Value; import net.Broken.Api.Services.SettingService; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -23,4 +22,10 @@ public class SettingController { public List getSettingDescription(){ return settingService.getSettingDescription(); } + + @GetMapping("/{guildId}/values") + @PreAuthorize("isInGuild(#guildId) && canManageGuild(#guildId)") + public List getSettingValues(@PathVariable String guildId){ + return settingService.getValues(guildId); + } } diff --git a/src/main/java/net/Broken/Api/Data/Settings/Value.java b/src/main/java/net/Broken/Api/Data/Settings/Value.java new file mode 100644 index 0000000..5aff82d --- /dev/null +++ b/src/main/java/net/Broken/Api/Data/Settings/Value.java @@ -0,0 +1,4 @@ +package net.Broken.Api.Data.Settings; + +public record Value(String id, String value) { +} diff --git a/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java b/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java index c130e7d..d54e148 100644 --- a/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java +++ b/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java @@ -3,7 +3,9 @@ package net.Broken.Api.Security.Expression; import net.Broken.Api.Security.Data.JwtPrincipal; import net.Broken.MainBot; import net.Broken.Tools.CacheTools; +import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; import okhttp3.Cache; import org.springframework.security.access.expression.SecurityExpressionRoot; import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations; @@ -29,6 +31,16 @@ public class CustomMethodSecurityExpressionRoot return CacheTools.getJdaUser(jwtPrincipal.user()).getMutualGuilds().contains(guild); } + public boolean canManageGuild(String guildId){ + JwtPrincipal jwtPrincipal = (JwtPrincipal) authentication.getPrincipal(); + Member member = MainBot.jda.getGuildById(guildId).getMemberById(jwtPrincipal.user().getDiscordId()); + return member.hasPermission( + Permission.MANAGE_SERVER, + Permission.MANAGE_PERMISSIONS, + Permission.MANAGE_CHANNEL + ); + } + @Override public void setFilterObject(Object filterObject) { this.filterObject = filterObject; diff --git a/src/main/java/net/Broken/Api/Services/SettingService.java b/src/main/java/net/Broken/Api/Services/SettingService.java index fc00ac9..39795c2 100644 --- a/src/main/java/net/Broken/Api/Services/SettingService.java +++ b/src/main/java/net/Broken/Api/Services/SettingService.java @@ -1,7 +1,13 @@ package net.Broken.Api.Services; +import liquibase.pro.packaged.V; import net.Broken.Api.Data.Settings.SettingDescriber; import net.Broken.Api.Data.Settings.SettingGroup; +import net.Broken.Api.Data.Settings.Value; +import net.Broken.DB.Entity.GuildPreferenceEntity; +import net.Broken.DB.Repository.GuildPreferenceRepository; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -10,6 +16,14 @@ import java.util.List; @Service public class SettingService { + public final GuildPreferenceRepository preferenceRepository; + private final Logger logger = LogManager.getLogger(); + + + public SettingService(GuildPreferenceRepository preferenceRepository) { + this.preferenceRepository = preferenceRepository; + } + public List getSettingDescription() { List toReturn = new ArrayList<>(); toReturn.add(getWelcomeGroup()); @@ -112,4 +126,81 @@ public class SettingService { mainField, fields); } + + + public List getValues(String guildId) { + GuildPreferenceEntity pref = preferenceRepository.findByGuildId(guildId).orElseGet(() -> { + logger.info("[API] : Generate default guild pref"); + return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId)); + }); + List values = new ArrayList<>(getWelcomeValues(pref)); + values.addAll(getDefaultRoleValues(pref)); + values.addAll(getDailyValues(pref)); + values.addAll(getAutoVoiceChannelValues(pref)); + + return values; + } + + private List getWelcomeValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "welcome_enable", + String.valueOf(pref.isWelcome()) + ) + ); + toReturn.add(new Value( + "welcome_chanel_id", + pref.getWelcomeChanelID() + ) + ); + toReturn.add(new Value( + "welcome_message", + pref.getWelcomeMessage() + ) + ); + return toReturn; + } + private List getDefaultRoleValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "default_role", + String.valueOf(pref.isDefaultRole()) + ) + ); + toReturn.add(new Value( + "default_role_id", + pref.getDefaultRoleId() + ) + ); + return toReturn; + } + private List getDailyValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "daily_madame", + String.valueOf(pref.isDefaultRole()) + ) + ); + return toReturn; + } + + private List getAutoVoiceChannelValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "auto_voice", + String.valueOf(pref.isAutoVoice()) + ) + ); + toReturn.add(new Value( + "auto_voice_base_channel", + pref.getAutoVoiceChannelID() + ) + ); + toReturn.add(new Value( + "auto_voice_channel_title", + pref.getAutoVoiceChannelTitle() + ) + ); + return toReturn; + } }