From e70fb4e266a8d0f2b755608a5a83026a3a3a028f Mon Sep 17 00:00:00 2001 From: Sebastien Date: Mon, 24 Sep 2018 15:50:32 +0300 Subject: [PATCH] Add settings view on web page (Read Only) --- .../Data/Settings/GetSettingsData.java | 30 +++ .../Broken/RestApi/Data/Settings/Value.java | 14 ++ .../RestApi/Data/UserManager/GuildInfo.java | 5 +- .../Broken/RestApi/SettingAPIController.java | 52 ++++++ .../RestApi/UserManagerAPIController.java | 4 +- .../java/net/Broken/Tools/SettingsUtils.java | 171 ++++++++++++++++++ .../net/Broken/webView/GeneralWebView.java | 33 +++- .../java/net/Broken/webView/MusicWebView.java | 5 +- .../net/Broken/webView/RegisterWebView.java | 2 + src/main/resources/static/js/settings.js | 3 + src/main/resources/templates/header.html | 9 +- src/main/resources/templates/index.html | 2 +- src/main/resources/templates/music.html | 2 +- .../resources/templates/oauthCallback.html | 2 +- src/main/resources/templates/register.html | 2 +- src/main/resources/templates/settings.html | 93 ++++++++++ 16 files changed, 419 insertions(+), 10 deletions(-) create mode 100644 src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java create mode 100644 src/main/java/net/Broken/RestApi/Data/Settings/Value.java create mode 100644 src/main/java/net/Broken/RestApi/SettingAPIController.java create mode 100644 src/main/java/net/Broken/Tools/SettingsUtils.java create mode 100644 src/main/resources/static/js/settings.js create mode 100644 src/main/resources/templates/settings.html diff --git a/src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java b/src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java new file mode 100644 index 0000000..a51a453 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java @@ -0,0 +1,30 @@ +package net.Broken.RestApi.Data.Settings; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.List; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class GetSettingsData { + public String name; + public String id; + public TYPE type; + public List values; + public String current; + + public GetSettingsData() { + } + + public GetSettingsData(String name, String id, TYPE type, List values, String current) { + this.name = name; + this.id = id; + this.type = type; + this.values = values; + this.current = current; + } + + public enum TYPE{ + BOOL,LIST,STRING + } +} + diff --git a/src/main/java/net/Broken/RestApi/Data/Settings/Value.java b/src/main/java/net/Broken/RestApi/Data/Settings/Value.java new file mode 100644 index 0000000..7ef7b5d --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/Settings/Value.java @@ -0,0 +1,14 @@ +package net.Broken.RestApi.Data.Settings; + +public class Value { + public String name; + public String id; + + public Value() { + } + + public Value(String name, String id) { + this.name = name; + this.id = id; + } +} diff --git a/src/main/java/net/Broken/RestApi/Data/UserManager/GuildInfo.java b/src/main/java/net/Broken/RestApi/Data/UserManager/GuildInfo.java index cc0f7ad..2c1aa32 100644 --- a/src/main/java/net/Broken/RestApi/Data/UserManager/GuildInfo.java +++ b/src/main/java/net/Broken/RestApi/Data/UserManager/GuildInfo.java @@ -3,9 +3,12 @@ package net.Broken.RestApi.Data.UserManager; public class GuildInfo { public String name; public String id; + public boolean isAdmin; - public GuildInfo(String name, String id) { + public GuildInfo(String name, String id, boolean isAdmin) { this.name = name; this.id = id; + this.isAdmin = isAdmin; + } } diff --git a/src/main/java/net/Broken/RestApi/SettingAPIController.java b/src/main/java/net/Broken/RestApi/SettingAPIController.java new file mode 100644 index 0000000..2e62e04 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/SettingAPIController.java @@ -0,0 +1,52 @@ +package net.Broken.RestApi; + +import net.Broken.DB.Entity.GuildPreferenceEntity; +import net.Broken.DB.Entity.UserEntity; +import net.Broken.DB.Repository.GuildPreferenceRepository; +import net.Broken.DB.Repository.UserRepository; +import net.Broken.MainBot; +import net.Broken.RestApi.Data.Settings.GetSettingsData; +import net.Broken.RestApi.Data.Settings.Value; +import net.Broken.Tools.SettingsUtils; +import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; +import net.Broken.Tools.UserManager.UserUtils; +import net.dv8tion.jda.core.JDA; +import net.dv8tion.jda.core.Permission; +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.Role; +import net.dv8tion.jda.core.entities.TextChannel; +import net.dv8tion.jda.core.entities.User; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CookieValue; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.RegEx; +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/api") +public class SettingAPIController { + private Logger logger = LogManager.getLogger(); + + + + @RequestMapping(value = "/settings", method = RequestMethod.GET) + public ResponseEntity> getSettings(@CookieValue("token") String token, @CookieValue("guild") String guild){ + SettingsUtils settingUtils = SettingsUtils.getInstance(); + if(settingUtils.checkPermission(token, guild)){ + Guild jdaGuild = MainBot.jda.getGuildById(guild); + return new ResponseEntity<>( settingUtils.extractSettings(jdaGuild), HttpStatus.OK); + } + else{ + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } + } + +} diff --git a/src/main/java/net/Broken/RestApi/UserManagerAPIController.java b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java index f7a7d45..645a3c6 100644 --- a/src/main/java/net/Broken/RestApi/UserManagerAPIController.java +++ b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java @@ -9,6 +9,7 @@ import net.Broken.RestApi.Data.UserManager.*; import net.Broken.Tools.UserManager.Exceptions.*; import net.Broken.Tools.UserManager.Oauth; import net.Broken.Tools.UserManager.UserUtils; +import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.User; import org.apache.logging.log4j.LogManager; @@ -108,7 +109,8 @@ public class UserManagerAPIController { List temp = new ArrayList<>(); for (Guild guild : user.getMutualGuilds()){ - temp.add(new GuildInfo(guild.getName(), guild.getId())); + + temp.add(new GuildInfo(guild.getName(), guild.getId(), guild.getMember(user).hasPermission(Permission.ADMINISTRATOR))); } return new ResponseEntity<>(temp, HttpStatus.OK); diff --git a/src/main/java/net/Broken/Tools/SettingsUtils.java b/src/main/java/net/Broken/Tools/SettingsUtils.java new file mode 100644 index 0000000..7ba7b0c --- /dev/null +++ b/src/main/java/net/Broken/Tools/SettingsUtils.java @@ -0,0 +1,171 @@ +package net.Broken.Tools; + +import net.Broken.DB.Entity.GuildPreferenceEntity; +import net.Broken.DB.Entity.UserEntity; +import net.Broken.DB.Repository.GuildPreferenceRepository; +import net.Broken.DB.Repository.PendingPwdResetRepository; +import net.Broken.DB.Repository.UserRepository; +import net.Broken.MainBot; +import net.Broken.RestApi.Data.Settings.GetSettingsData; +import net.Broken.RestApi.Data.Settings.Value; +import net.Broken.SpringContext; +import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; +import net.Broken.Tools.UserManager.UserUtils; +import net.dv8tion.jda.core.Permission; +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.Role; +import net.dv8tion.jda.core.entities.TextChannel; +import net.dv8tion.jda.core.entities.User; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.context.ApplicationContext; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.crypto.password.PasswordEncoder; + +import java.util.ArrayList; +import java.util.List; + +public class SettingsUtils { + + private static SettingsUtils INSTANCE; + Logger logger = LogManager.getLogger(); + + public static SettingsUtils getInstance(){ + return (INSTANCE == null) ? new SettingsUtils() : INSTANCE; + } + + GuildPreferenceRepository guildPreferenceRepository; + UserRepository userRepository; + + + private SettingsUtils() { + ApplicationContext context = SpringContext.getAppContext(); + guildPreferenceRepository = (GuildPreferenceRepository) context.getBean("guildPreferenceRepository"); + userRepository = (UserRepository) context.getBean("userRepository"); + + + } + + public ArrayList extractSettings(Guild guild){ + ArrayList list = new ArrayList<>(); + + List guildPrefList = guildPreferenceRepository.findByGuildId(guild.getId()); + GuildPreferenceEntity guildPref; + if(guildPrefList.isEmpty()){ + guildPref = GuildPreferenceEntity.getDefault(guild); + guildPreferenceRepository.save(guildPref); + } + else + guildPref = guildPrefList.get(0); + + + + list.add(new GetSettingsData( + "Enable Welcome Message", + "welcome", + GetSettingsData.TYPE.BOOL, + null, + Boolean.toString(guildPref.isWelcome()) + )); + list.add(new GetSettingsData( + "Welcome Message chanel", + "welcome_chanel_id", + GetSettingsData.TYPE.LIST, + getTextChannels(guild), + guildPref.getWelcomeChanelID() + )); + list.add(new GetSettingsData( + "Welcome Message", + "welcome_message", + GetSettingsData.TYPE.STRING, + null, + guildPref.getWelcomeMessage() + )); + + + list.add(new GetSettingsData( + "Enable Default Role", + "default_role", + GetSettingsData.TYPE.BOOL, + null, + Boolean.toString(guildPref.isDefaultRole()) + )); + list.add(new GetSettingsData( + "Default Role", + "default_role_id", + GetSettingsData.TYPE.LIST, + getRoles(guild), + guildPref.getDefaultRoleId() + )); + + + list.add(new GetSettingsData( + "Enable Anti Spam", + "anti_spam", + GetSettingsData.TYPE.BOOL, + null, + Boolean.toString(guildPref.isAntiSpam()) + )); + + list.add(new GetSettingsData( + "Enable Daily Madame Message", + "daily_madame", + GetSettingsData.TYPE.BOOL, + null, + Boolean.toString(guildPref.isDailyMadame()) + )); + + return list; + + } + + + public boolean checkPermission(String token, String guild){ + if(token == null || guild == null){ + return false; + } + else{ + try { + UserEntity user = UserUtils.getInstance().getUserWithApiToken(userRepository, token); + User jdaUser = MainBot.jda.getUserById(user.getJdaId()); + Guild jdaGuild = MainBot.jda.getGuildById(guild); + if(jdaGuild == null){ + return false; + } + if(!jdaGuild.getMember(jdaUser).hasPermission(Permission.ADMINISTRATOR)){ + return false; + } + + + + + + + return true; + + } catch (Exception e) { + logger.warn("Unknown Token! " + token); + return false; + } + } + } + + + + private List getTextChannels(Guild guild){ + List channels = new ArrayList<>(); + for(TextChannel channel : guild.getTextChannels()){ + channels.add(new Value(channel.getName(), channel.getId())); + } + return channels; + } + + private List getRoles(Guild guild){ + List roles = new ArrayList<>(); + for(Role role : guild.getRoles()){ + roles.add(new Value(role.getName(), role.getId())); + } + return roles; + } +} diff --git a/src/main/java/net/Broken/webView/GeneralWebView.java b/src/main/java/net/Broken/webView/GeneralWebView.java index 5160304..1252dd0 100644 --- a/src/main/java/net/Broken/webView/GeneralWebView.java +++ b/src/main/java/net/Broken/webView/GeneralWebView.java @@ -6,13 +6,17 @@ import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Repository.UserRepository; import net.Broken.MainBot; import net.Broken.RestApi.Commands.Play; +import net.Broken.Tools.SettingsUtils; import net.dv8tion.jda.core.entities.Guild; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; import java.util.ArrayList; import java.util.List; @@ -23,15 +27,20 @@ import java.util.List; @Controller public class GeneralWebView { + @ResponseStatus(HttpStatus.FORBIDDEN) + public class ForbiddenException extends RuntimeException {} + @RequestMapping("/") - public String music(Model model, @CookieValue(value = "guild", defaultValue = "1") String guildId){ + public String music(Model model, @CookieValue(value = "guild", defaultValue = "1") String guildId, @CookieValue(value = "token", defaultValue = "") String token){ Guild guild = MainBot.jda.getGuildById(guildId); if(guild != null) model.addAttribute("guild_name", guild.getName()); else model.addAttribute("guild_name", ""); model.addAttribute("redirect_url", System.getenv("OAUTH_URL")); + model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId)); + return CheckPage.getPageIfReady("index"); @@ -51,6 +60,28 @@ public class GeneralWebView { return "oauthCallback"; } + @RequestMapping("/settings") + public String settings(Model model, @CookieValue(value = "guild", defaultValue = "") String guildId, @CookieValue(value = "token", defaultValue = "") String token){ + SettingsUtils settingsUtils = SettingsUtils.getInstance(); + if(settingsUtils.checkPermission(token, guildId)){ + Guild guild = MainBot.jda.getGuildById(guildId); + if(guild != null) + model.addAttribute("guild_name", guild.getName()); + else + model.addAttribute("guild_name", ""); + model.addAttribute("redirect_url", System.getenv("OAUTH_URL")); + model.addAttribute("settings", SettingsUtils.getInstance().extractSettings(guild)); + model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId)); + + + + return CheckPage.getPageIfReady("settings"); + } + else + throw new ForbiddenException(); + + } + diff --git a/src/main/java/net/Broken/webView/MusicWebView.java b/src/main/java/net/Broken/webView/MusicWebView.java index 8c24428..d33b00f 100644 --- a/src/main/java/net/Broken/webView/MusicWebView.java +++ b/src/main/java/net/Broken/webView/MusicWebView.java @@ -1,6 +1,7 @@ package net.Broken.webView; import net.Broken.MainBot; +import net.Broken.Tools.SettingsUtils; import net.dv8tion.jda.core.entities.Guild; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -13,14 +14,14 @@ import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MusicWebView { @RequestMapping("/music") - public String music(Model model, @CookieValue(value = "guild", defaultValue = "1") String guildId){ + public String music(Model model, @CookieValue(value = "guild", defaultValue = "1") String guildId, @CookieValue(value = "token", defaultValue = "1") String token){ Guild guild = MainBot.jda.getGuildById(guildId); if(guild != null) model.addAttribute("guild_name", guild.getName()); else model.addAttribute("guild_name", ""); model.addAttribute("redirect_url", System.getenv("OAUTH_URL")); - + model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId)); return CheckPage.getPageIfReady("music"); } diff --git a/src/main/java/net/Broken/webView/RegisterWebView.java b/src/main/java/net/Broken/webView/RegisterWebView.java index 1aa5551..2193494 100644 --- a/src/main/java/net/Broken/webView/RegisterWebView.java +++ b/src/main/java/net/Broken/webView/RegisterWebView.java @@ -15,6 +15,8 @@ public class RegisterWebView { public String music(@RequestParam(value="id", required = true, defaultValue = "") String id, Model model){ model.addAttribute("id", id); model.addAttribute("redirect_url", System.getenv("OAUTH_URL")); + model.addAttribute("isAdmin",false); + return CheckPage.getPageIfReady("register"); } } diff --git a/src/main/resources/static/js/settings.js b/src/main/resources/static/js/settings.js new file mode 100644 index 0000000..e06132e --- /dev/null +++ b/src/main/resources/static/js/settings.js @@ -0,0 +1,3 @@ +$(document).ready(function(){ + $('select').formSelect(); +}); \ No newline at end of file diff --git a/src/main/resources/templates/header.html b/src/main/resources/templates/header.html index 69498e5..61c0113 100644 --- a/src/main/resources/templates/header.html +++ b/src/main/resources/templates/header.html @@ -12,7 +12,7 @@ -
+