diff --git a/.gitignore b/.gitignore index 4cd1342..b66cef7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ META-INF/ classes/artifacts/Discord_Stroumpf_Beta_jar/ + +src/main/resources/templates/css + +src/main/resources/templates/js diff --git a/Dockerfile b/Dockerfile index 8e99f41..78dedc9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,4 @@ RUN chmod +x gradlew ENV PORT=8080 ENV TOKEN=10 RUN ./gradlew build -CMD java -jar build/libs/DiscordBot-0.1.0.jar -t ${TOKEN} +CMD java -Xmx500m -jar build/libs/DiscordBot-0.1.0.jar -t ${TOKEN} diff --git a/README.md b/README.md index 7998a30..3cb6697 100644 --- a/README.md +++ b/README.md @@ -9,67 +9,25 @@ > version: '2' > > services: -> botdiscorddevel: -> image: brokenfire/brokendiscordbot:latest +> botdiscord: +> image: brokenfire/brokendiscordbot:latest > networks: > - proxy -> - internal > environment: -> - PORT=8080 +> - PORT=8081 > - TOKEN=1111111111111 #CHANGE ME! -> - DB_URL=jdbc:mysql://database/botdiscord -> - DB_USER=root -> - DB_PWD=ogahxu1eigohN2Eok0xoawae #CHANGE ME! > labels: > - "traefik.frontend.rule=Host:exemple.com" #CHANGE ME! -> - "traefik.docker.network=proxy" > - "traefik.port=8080" > - "traefik.backend=botdiscord" > - "traefik.frontend.entryPoints=http,https" -> volumes: -> - "./logs:/bot_src/logs" -> depends_on: -> - "database" +> volumes: +> - "./logs:/bot_src/logs" > restart: always > -> phpmyadmin: -> image: phpmyadmin/phpmyadmin:latest -> labels: -> - "traefik.frontend.rule=Host:phpmyadmin.exemple.com" #CHANGE ME! -> - "traefik.port=80" -> - "traefik.docker.network=proxy" -> - "traefik.backend=phpmyadmin" -> - "traefik.frontend.entryPoints=http,https" -> environment: -> - PMA_HOST=database -> depends_on: -> - "database" -> networks: -> - proxy -> - internal -> -> database: -> image: mariadb:latest -> volumes: -> - ./db:/var/lib/mysql -> labels: -> - "traefik.enable=false" -> environment: -> - MYSQL_ROOT_PASSWORD=ogahxu1eigohN2Eok0xoawae #CHANGE ME! -> - MYSQL_DATABASE=botdiscord -> - MYSQL_USER= #CHANGE ME! -> - MYSQL_PASSWORD= #CHANGE ME! -> networks: -> - internal -> -> -> > networks: > proxy: -> external: true -> internal: -> external: false -> +> external: true > ``` > Docker hub [repo](https://hub.docker.com/r/brokenfire/brokendiscordbot/) diff --git a/build.gradle b/build.gradle index 324379e..abd2e38 100644 --- a/build.gradle +++ b/build.gradle @@ -31,15 +31,16 @@ dependencies { exclude group:"org.springframework.boot", module: "spring-boot-starter-logging" } compile("org.springframework.boot:spring-boot-starter-log4j2") - compile("com.sedmelluq:lavaplayer:1.2.45") + compile("com.sedmelluq:lavaplayer:1.2.49") compile 'net.dv8tion:JDA:3.3.1_303' compile group: 'org.json', name: 'json', version: '20160810' - // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) - //compile("org.springframework.boot:spring-boot-starter-data-jpa") { - // exclude group:"org.springframework.boot", module: "spring-boot-starter-logging" - //} - // Use MySQL Connector-J - //compile 'mysql:mysql-connector-java' + compile 'org.springframework.security:spring-security-web:5.0.1.RELEASE' +// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) + compile("org.springframework.boot:spring-boot-starter-data-jpa") { + exclude group:"org.springframework.boot", module: "spring-boot-starter-logging" + } +// Use MySQL Connector-J + compile 'mysql:mysql-connector-java' compile 'org.reflections:reflections:0.9.11' diff --git a/src/main/java/net/Broken/BotListener.java b/src/main/java/net/Broken/BotListener.java index 48f2afd..b0187e8 100644 --- a/src/main/java/net/Broken/BotListener.java +++ b/src/main/java/net/Broken/BotListener.java @@ -2,8 +2,8 @@ package net.Broken; import net.Broken.Commands.Move; import net.Broken.Commands.Music; -import net.Broken.Outils.AntiSpam; -import net.Broken.Outils.Moderateur; +import net.Broken.Tools.AntiSpam; +import net.Broken.Tools.Moderateur; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Member; diff --git a/src/main/java/net/Broken/Commands/Cat.java b/src/main/java/net/Broken/Commands/Cat.java index d8edce5..d8c4b57 100644 --- a/src/main/java/net/Broken/Commands/Cat.java +++ b/src/main/java/net/Broken/Commands/Cat.java @@ -2,7 +2,7 @@ package net.Broken.Commands; import net.Broken.Commande; -import net.Broken.Outils.Redirection; +import net.Broken.Tools.Redirection; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; diff --git a/src/main/java/net/Broken/Commands/Flush.java b/src/main/java/net/Broken/Commands/Flush.java index 072554a..9336deb 100644 --- a/src/main/java/net/Broken/Commands/Flush.java +++ b/src/main/java/net/Broken/Commands/Flush.java @@ -1,7 +1,7 @@ package net.Broken.Commands; import net.Broken.Commande; -import net.Broken.Outils.EmbedMessageUtils; +import net.Broken.Tools.EmbedMessageUtils; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.MessageChannel; diff --git a/src/main/java/net/Broken/Commands/Help.java b/src/main/java/net/Broken/Commands/Help.java index b29f8ea..6890891 100644 --- a/src/main/java/net/Broken/Commands/Help.java +++ b/src/main/java/net/Broken/Commands/Help.java @@ -2,9 +2,9 @@ package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; -import net.Broken.Outils.PrivateMessage; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.MessageTimeOut; +import net.Broken.Tools.PrivateMessage; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Message; diff --git a/src/main/java/net/Broken/Commands/Move.java b/src/main/java/net/Broken/Commands/Move.java index befab13..7852569 100644 --- a/src/main/java/net/Broken/Commands/Move.java +++ b/src/main/java/net/Broken/Commands/Move.java @@ -2,8 +2,8 @@ package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.MessageTimeOut; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; diff --git a/src/main/java/net/Broken/Commands/Music.java b/src/main/java/net/Broken/Commands/Music.java index 6db5a62..42d640c 100644 --- a/src/main/java/net/Broken/Commands/Music.java +++ b/src/main/java/net/Broken/Commands/Music.java @@ -2,10 +2,9 @@ package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.MessageTimeOut; import net.Broken.audio.AudioM; -import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.VoiceChannel; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; diff --git a/src/main/java/net/Broken/Commands/Over18/Ass.java b/src/main/java/net/Broken/Commands/Over18/Ass.java index eabf658..a3ede7a 100644 --- a/src/main/java/net/Broken/Commands/Over18/Ass.java +++ b/src/main/java/net/Broken/Commands/Over18/Ass.java @@ -1,6 +1,6 @@ package net.Broken.Commands.Over18; -import net.Broken.Outils.Command.NumberedCommande; +import net.Broken.Tools.Command.NumberedCommande; import org.apache.logging.log4j.LogManager; /** diff --git a/src/main/java/net/Broken/Commands/Over18/Boobs.java b/src/main/java/net/Broken/Commands/Over18/Boobs.java index c7bf203..d2bafd1 100644 --- a/src/main/java/net/Broken/Commands/Over18/Boobs.java +++ b/src/main/java/net/Broken/Commands/Over18/Boobs.java @@ -1,6 +1,6 @@ package net.Broken.Commands.Over18; -import net.Broken.Outils.Command.NumberedCommande; +import net.Broken.Tools.Command.NumberedCommande; import org.apache.logging.log4j.LogManager; /** diff --git a/src/main/java/net/Broken/Commands/Over18/Madame.java b/src/main/java/net/Broken/Commands/Over18/Madame.java index 4e0f5c9..90cd87c 100644 --- a/src/main/java/net/Broken/Commands/Over18/Madame.java +++ b/src/main/java/net/Broken/Commands/Over18/Madame.java @@ -1,7 +1,7 @@ package net.Broken.Commands.Over18; import net.Broken.Commande; -import net.Broken.Outils.Redirection; +import net.Broken.Tools.Redirection; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/net/Broken/Commands/Over18/Pipe.java b/src/main/java/net/Broken/Commands/Over18/Pipe.java index c83b5bd..009d826 100644 --- a/src/main/java/net/Broken/Commands/Over18/Pipe.java +++ b/src/main/java/net/Broken/Commands/Over18/Pipe.java @@ -1,6 +1,6 @@ package net.Broken.Commands.Over18; -import net.Broken.Outils.Command.NumberedCommande; +import net.Broken.Tools.Command.NumberedCommande; import org.apache.logging.log4j.LogManager; /** diff --git a/src/main/java/net/Broken/Commands/Over18/SM.java b/src/main/java/net/Broken/Commands/Over18/SM.java index 44fac7c..98982b6 100644 --- a/src/main/java/net/Broken/Commands/Over18/SM.java +++ b/src/main/java/net/Broken/Commands/Over18/SM.java @@ -1,7 +1,7 @@ package net.Broken.Commands.Over18; import net.Broken.Commande; -import net.Broken.Outils.Redirection; +import net.Broken.Tools.Redirection; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/net/Broken/Commands/Ping.java b/src/main/java/net/Broken/Commands/Ping.java index 549ebae..cab02e5 100644 --- a/src/main/java/net/Broken/Commands/Ping.java +++ b/src/main/java/net/Broken/Commands/Ping.java @@ -2,7 +2,7 @@ package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; -import net.Broken.Outils.MessageTimeOut; +import net.Broken.Tools.MessageTimeOut; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; diff --git a/src/main/java/net/Broken/Commands/Spam.java b/src/main/java/net/Broken/Commands/Spam.java index e0f1c4a..4d4fa6a 100644 --- a/src/main/java/net/Broken/Commands/Spam.java +++ b/src/main/java/net/Broken/Commands/Spam.java @@ -2,10 +2,10 @@ package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; -import net.Broken.Outils.AntiSpam; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; -import net.Broken.Outils.UserSpamUtils; +import net.Broken.Tools.AntiSpam; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.MessageTimeOut; +import net.Broken.Tools.UserSpamUtils; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Member; diff --git a/src/main/java/net/Broken/Commands/SpamInfo.java b/src/main/java/net/Broken/Commands/SpamInfo.java index 1f64797..9755702 100644 --- a/src/main/java/net/Broken/Commands/SpamInfo.java +++ b/src/main/java/net/Broken/Commands/SpamInfo.java @@ -2,10 +2,10 @@ package net.Broken.Commands; import net.Broken.Commande; import net.Broken.MainBot; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; -import net.Broken.Outils.PrivateMessage; -import net.Broken.Outils.UserSpamUtils; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.MessageTimeOut; +import net.Broken.Tools.PrivateMessage; +import net.Broken.Tools.UserSpamUtils; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Message; diff --git a/src/main/java/net/Broken/DB/Entity/PendingUserEntity.java b/src/main/java/net/Broken/DB/Entity/PendingUserEntity.java new file mode 100644 index 0000000..eb06cd7 --- /dev/null +++ b/src/main/java/net/Broken/DB/Entity/PendingUserEntity.java @@ -0,0 +1,72 @@ +package net.Broken.DB.Entity; + + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class PendingUserEntity { + @Id + @GeneratedValue(strategy= GenerationType.AUTO) + private Integer id; + + private String name; + + private String jdaId; + + private String checkToken; + + private String password; + + public PendingUserEntity() { + } + + public PendingUserEntity(String name, String jdaId, String checkToken, String password) { + this.name = name; + this.jdaId = jdaId; + this.checkToken = checkToken; + this.password = password; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getJdaId() { + return jdaId; + } + + public void setJdaId(String jdaId) { + this.jdaId = jdaId; + } + + public String getCheckToken() { + return checkToken; + } + + public void setCheckToken(String checkToken) { + this.checkToken = checkToken; + } +} diff --git a/src/main/java/net/Broken/DB/Entity/UserEntity.java b/src/main/java/net/Broken/DB/Entity/UserEntity.java new file mode 100644 index 0000000..b0fafd7 --- /dev/null +++ b/src/main/java/net/Broken/DB/Entity/UserEntity.java @@ -0,0 +1,71 @@ +package net.Broken.DB.Entity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class UserEntity { + @Id + @GeneratedValue(strategy= GenerationType.AUTO) + private Integer id; + + private String name; + + private String jdaId; + + private String apiToken; + + private String password; + + public UserEntity() { + } + + public UserEntity(PendingUserEntity pendingUserEntity, String apiToken) { + this.name = pendingUserEntity.getName(); + this.jdaId = pendingUserEntity.getJdaId(); + this.password = pendingUserEntity.getPassword(); + this.apiToken = apiToken; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getJdaId() { + return jdaId; + } + + public void setJdaId(String jdaId) { + this.jdaId = jdaId; + } + + public String getApiToken() { + return apiToken; + } + + public void setApiToken(String apiToken) { + this.apiToken = apiToken; + } +} diff --git a/src/main/java/net/Broken/DB/Repository/PendingUserRepository.java b/src/main/java/net/Broken/DB/Repository/PendingUserRepository.java new file mode 100644 index 0000000..c72adbc --- /dev/null +++ b/src/main/java/net/Broken/DB/Repository/PendingUserRepository.java @@ -0,0 +1,12 @@ +package net.Broken.DB.Repository; + +import net.Broken.DB.Entity.PendingUserEntity; +import org.springframework.data.repository.CrudRepository; + +import java.util.List; + +public interface PendingUserRepository extends CrudRepository { + List findByJdaId(String jdaId); + + +} diff --git a/src/main/java/net/Broken/DB/Repository/UserRepository.java b/src/main/java/net/Broken/DB/Repository/UserRepository.java new file mode 100644 index 0000000..86d05d5 --- /dev/null +++ b/src/main/java/net/Broken/DB/Repository/UserRepository.java @@ -0,0 +1,12 @@ +package net.Broken.DB.Repository; + +import net.Broken.DB.Entity.UserEntity; +import org.springframework.data.repository.CrudRepository; + +import java.util.List; + +public interface UserRepository extends CrudRepository{ + List findByName(String name); + List findByJdaId(String jdaId); + List findByApiToken(String apiToken); +} diff --git a/src/main/java/net/Broken/Init.java b/src/main/java/net/Broken/Init.java index 58eaa2b..6e1f14f 100644 --- a/src/main/java/net/Broken/Init.java +++ b/src/main/java/net/Broken/Init.java @@ -1,7 +1,7 @@ package net.Broken; -import net.Broken.Outils.Command.CommandLoader; -import net.Broken.Outils.DayListener; +import net.Broken.Tools.Command.CommandLoader; +import net.Broken.Tools.DayListener; import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.JDABuilder; diff --git a/src/main/java/net/Broken/MainBot.java b/src/main/java/net/Broken/MainBot.java index b047837..41b8891 100644 --- a/src/main/java/net/Broken/MainBot.java +++ b/src/main/java/net/Broken/MainBot.java @@ -1,8 +1,9 @@ package net.Broken; -import net.Broken.Outils.Command.CommandParser; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.UserSpamUtils; +import net.Broken.Tools.Command.CommandParser; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.UserManager.UserRegister; +import net.Broken.Tools.UserSpamUtils; import net.Broken.RestApi.ApiCommandLoader; import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.entities.ChannelType; @@ -17,6 +18,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.stereotype.Controller; +import java.security.SecureRandom; import java.util.ArrayList; import java.util.HashMap; @@ -33,10 +35,12 @@ public class MainBot { public static HashMap message_compteur =new HashMap<>(); public static boolean roleFlag = false; public static HashMap spamUtils = new HashMap<>(); + public static UserRegister userRegister = new UserRegister(); public static JDA jda; + public static int messageTimeOut = 10; @@ -74,6 +78,7 @@ public class MainBot { })); } + ApiCommandLoader.load(); diff --git a/src/main/java/net/Broken/RestApi/Data/CommandPostData.java b/src/main/java/net/Broken/RestApi/Data/CommandPostData.java index f08c26d..1d73cc5 100644 --- a/src/main/java/net/Broken/RestApi/Data/CommandPostData.java +++ b/src/main/java/net/Broken/RestApi/Data/CommandPostData.java @@ -7,4 +7,6 @@ public class CommandPostData { public String url; public int playlistLimit; public String chanelId; + public String name; + public String token; } diff --git a/src/main/java/net/Broken/RestApi/Data/CommandResponseData.java b/src/main/java/net/Broken/RestApi/Data/CommandResponseData.java index e78e8c2..ffea0d8 100644 --- a/src/main/java/net/Broken/RestApi/Data/CommandResponseData.java +++ b/src/main/java/net/Broken/RestApi/Data/CommandResponseData.java @@ -6,9 +6,16 @@ import com.fasterxml.jackson.annotation.JsonInclude; public class CommandResponseData { public String Commande; public String Message; + public String error; public CommandResponseData(String commande, String message) { Commande = commande; Message = message; } + + public CommandResponseData(String commande, String message, String error) { + Commande = commande; + Message = message; + this.error = error; + } } diff --git a/src/main/java/net/Broken/RestApi/Data/UserManager/CheckResposeData.java b/src/main/java/net/Broken/RestApi/Data/UserManager/CheckResposeData.java new file mode 100644 index 0000000..b169111 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/UserManager/CheckResposeData.java @@ -0,0 +1,15 @@ +package net.Broken.RestApi.Data.UserManager; + +public class CheckResposeData { + public boolean accepted; + public String name; + public String message; + public String id; + + public CheckResposeData(boolean accepted, String name, String message, String id) { + this.accepted = accepted; + this.name = name; + this.message = message; + this.id = id; + } +} diff --git a/src/main/java/net/Broken/RestApi/Data/UserManager/ConfirmData.java b/src/main/java/net/Broken/RestApi/Data/UserManager/ConfirmData.java new file mode 100644 index 0000000..9aad50b --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/UserManager/ConfirmData.java @@ -0,0 +1,7 @@ +package net.Broken.RestApi.Data.UserManager; + +public class ConfirmData { + public String id; + public String checkToken; + +} diff --git a/src/main/java/net/Broken/RestApi/Data/UserManager/UserConnectionData.java b/src/main/java/net/Broken/RestApi/Data/UserManager/UserConnectionData.java new file mode 100644 index 0000000..77563a0 --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/UserManager/UserConnectionData.java @@ -0,0 +1,25 @@ +package net.Broken.RestApi.Data.UserManager; + +public class UserConnectionData { + public boolean accepted; + public String token; + public String message; + public String error; + public String name; + + public UserConnectionData(boolean accepted, String name, String token, String message) { + this.accepted = accepted; + this.token = token; + this.message = message; + this.name = name; + this.error = null; + } + + public UserConnectionData(boolean accepted, String message, String error) { + this.accepted = accepted; + this.token = null; + this.message = message; + this.error = error; + this.name = null; + } +} diff --git a/src/main/java/net/Broken/RestApi/Data/UserManager/UserInfoData.java b/src/main/java/net/Broken/RestApi/Data/UserManager/UserInfoData.java new file mode 100644 index 0000000..c5edf0e --- /dev/null +++ b/src/main/java/net/Broken/RestApi/Data/UserManager/UserInfoData.java @@ -0,0 +1,6 @@ +package net.Broken.RestApi.Data.UserManager; + +public class UserInfoData { + public String name; + public String password; +} diff --git a/src/main/java/net/Broken/RestApi/MusicWebAPIController.java b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java index 309b837..a0a6e5a 100644 --- a/src/main/java/net/Broken/RestApi/MusicWebAPIController.java +++ b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java @@ -4,24 +4,33 @@ import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; import net.Broken.Commands.Music; +import net.Broken.DB.Entity.UserEntity; +import net.Broken.DB.Repository.UserRepository; import net.Broken.MainBot; import net.Broken.RestApi.Data.*; +import net.Broken.RestApi.Data.UserManager.CheckResposeData; +import net.Broken.RestApi.Data.UserManager.UserInfoData; +import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; +import net.Broken.Tools.UserManager.Exceptions.UserNotFoundException; import net.Broken.audio.NotConectedException; import net.Broken.audio.NullMusicManager; import net.dv8tion.jda.core.entities.VoiceChannel; 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.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; -// import net.Broken.DB.SavedPlaylistRepository; +// import net.Broken.DB.Repository.SavedPlaylistRepository; @RestController @RequestMapping("/api/music/") @@ -29,6 +38,9 @@ public class MusicWebAPIController { Logger logger = LogManager.getLogger(); // @Autowired // public SavedPlaylistRepository savedPlaylist; + @Autowired + UserRepository userRepository; + @RequestMapping("/currentMusicInfo") public CurrentMusicData getCurrentM(){ @@ -65,17 +77,32 @@ public class MusicWebAPIController { } @RequestMapping(value = "/command", method = RequestMethod.POST) - public ResponseEntity command(@RequestBody CommandPostData data){ + public ResponseEntity command(@RequestBody CommandPostData data, HttpServletRequest request){ if(data.command != null) { - logger.info("receive command: " + data.command); - Music musicCommande = (Music) MainBot.commandes.get("music"); + if(data.token != null) { + try { + UserEntity user = MainBot.userRegister.getUserWithApiToken(userRepository, data.token); + logger.info("receive command " + data.command + " from " + request.getRemoteAddr() + " USER: " + user.getName()); + Music musicCommande = (Music) MainBot.commandes.get("music"); - if(ApiCommandLoader.apiCommands.containsKey(data.command)) - return ApiCommandLoader.apiCommands.get(data.command).action(musicCommande,data); - else - return new ResponseEntity<>(new CommandResponseData(data.command,"Unknown Command"), HttpStatus.BAD_REQUEST); + if (ApiCommandLoader.apiCommands.containsKey(data.command)) + return ApiCommandLoader.apiCommands.get(data.command).action(musicCommande, data); + else + return new ResponseEntity<>(new CommandResponseData(data.command, "Unknown Command", "command"), HttpStatus.BAD_REQUEST); + } catch (UnknownTokenException e) { + logger.warn("Command with unknown token from: "+request.getRemoteAddr()); + return new ResponseEntity<>(new CommandResponseData(data.command,"Unknown Token!\nPlease Re-connect.", "token"), HttpStatus.UNAUTHORIZED); + + } + + } + else{ + logger.warn("Command without token! ip: "+ request.getRemoteAddr()); + return new ResponseEntity<>(new CommandResponseData(data.command,"Missing token!\nPlease Re-connect.","token"), HttpStatus.UNAUTHORIZED); + + } } else logger.info("Null"); @@ -85,7 +112,6 @@ public class MusicWebAPIController { @RequestMapping(value = "/getChanel", method = RequestMethod.GET) public List getChanel(){ List temp = new ArrayList<>(); - logger.info(MainBot.jda.getVoiceChannels().size()); for(VoiceChannel aChanel : MainBot.jda.getVoiceChannels()){ temp.add(new Chanel(aChanel.getName(),aChanel.getId(),aChanel.getPosition())); } @@ -95,6 +121,7 @@ public class MusicWebAPIController { + // DB Test Ignore it // @RequestMapping(value = "/test", method = RequestMethod.GET) diff --git a/src/main/java/net/Broken/RestApi/UserManagerAPIController.java b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java new file mode 100644 index 0000000..2cb9abb --- /dev/null +++ b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java @@ -0,0 +1,95 @@ +package net.Broken.RestApi; + +import net.Broken.DB.Entity.PendingUserEntity; +import net.Broken.DB.Entity.UserEntity; +import net.Broken.DB.Repository.PendingUserRepository; +import net.Broken.DB.Repository.UserRepository; +import net.Broken.MainBot; +import net.Broken.RestApi.Data.UserManager.CheckResposeData; +import net.Broken.RestApi.Data.UserManager.ConfirmData; +import net.Broken.RestApi.Data.UserManager.UserConnectionData; +import net.Broken.RestApi.Data.UserManager.UserInfoData; +import net.Broken.Tools.UserManager.Exceptions.PasswordNotMatchException; +import net.Broken.Tools.UserManager.Exceptions.TokenNotMatch; +import net.Broken.Tools.UserManager.Exceptions.UserAlreadyRegistered; +import net.Broken.Tools.UserManager.Exceptions.UserNotFoundException; +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.security.crypto.password.PasswordEncoder; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/userManagement") +public class UserManagerAPIController { + Logger logger = LogManager.getLogger(); + @Autowired + PendingUserRepository pendingUserRepository; + + @Autowired + UserRepository userRepository; + + @Autowired + private PasswordEncoder passwordEncoder; + + + @RequestMapping(value = "/preRegister", method = RequestMethod.POST) + public ResponseEntity command(@RequestBody UserInfoData data){ + if(data != null && data.name != null) { + try { + String id = MainBot.userRegister.sendCheckToken(pendingUserRepository, userRepository, passwordEncoder, data); + return new ResponseEntity<>(new CheckResposeData(true, data.name, "Message sent", id), HttpStatus.OK); + } catch (UserNotFoundException e) { + logger.warn("User \"" + data.name + "\" not found!"); + return new ResponseEntity<>(new CheckResposeData(false, data.name, "User not found on server!",""), HttpStatus.NOT_FOUND); + } catch (PasswordNotMatchException userAlreadyRegistered) { + return new ResponseEntity<>(new CheckResposeData(false, data.name, "User already registered in pending database and password not match!",""), HttpStatus.NOT_ACCEPTABLE); + + } catch (UserAlreadyRegistered userAlreadyRegistered) { + return new ResponseEntity<>(new CheckResposeData(false, data.name, "User already registered in database!",""), HttpStatus.NOT_ACCEPTABLE); + } + } + else{ + return new ResponseEntity<>(new CheckResposeData(false, "", "Missing parameter(s)",""), HttpStatus.BAD_REQUEST); + } + } + + @RequestMapping(value = "/confirmAccount", method = RequestMethod.POST) + public ResponseEntity confirAccount(@RequestBody ConfirmData data){ + //TODO move pending user to accepted and return right things + try { + PendingUserEntity pUser = MainBot.userRegister.confirmCheckToken(pendingUserRepository, Integer.parseInt(data.id), data.checkToken); + UserEntity user = new UserEntity(pUser, MainBot.userRegister.generateApiToken()); + userRepository.save(user); + pendingUserRepository.delete(pUser); + + return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(),""),HttpStatus.OK); + } catch (TokenNotMatch tokenNotMatch) { + logger.warn("Pre token not match for "+data.id+"!"); + return new ResponseEntity<>(new UserConnectionData(false,"Token not match!","token"),HttpStatus.NOT_ACCEPTABLE); + } catch (UserNotFoundException e) { + logger.warn("Id not found in DB ("+data.id+")"); + return new ResponseEntity<>(new UserConnectionData(false,"User not found on DB!", "user"),HttpStatus.NOT_ACCEPTABLE); + } + } + + @RequestMapping(value = "/requestToken", method = RequestMethod.POST) + public ResponseEntity requestToken(@RequestBody UserInfoData data){ + try { + UserEntity user = MainBot.userRegister.getUser(userRepository, passwordEncoder, data); + return new ResponseEntity<>(new UserConnectionData(true, user.getName(), user.getApiToken(), ""), HttpStatus.OK); + + } catch (UserNotFoundException e) { + return new ResponseEntity<>(new UserConnectionData(false,"User not registered!", "user"),HttpStatus.NOT_ACCEPTABLE); + } catch (PasswordNotMatchException e) { + return new ResponseEntity<>(new UserConnectionData(false,"Wrong user name or password!", "password"),HttpStatus.NOT_ACCEPTABLE); + } + } + + +} diff --git a/src/main/java/net/Broken/Outils/AntiSpam.java b/src/main/java/net/Broken/Tools/AntiSpam.java similarity index 97% rename from src/main/java/net/Broken/Outils/AntiSpam.java rename to src/main/java/net/Broken/Tools/AntiSpam.java index a8d89d4..00d3355 100644 --- a/src/main/java/net/Broken/Outils/AntiSpam.java +++ b/src/main/java/net/Broken/Tools/AntiSpam.java @@ -1,10 +1,7 @@ -package net.Broken.Outils; +package net.Broken.Tools; import net.Broken.MainBot; import net.Broken.Commands.Move; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; -import net.Broken.Outils.UserSpamUtils; import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.exceptions.HierarchyException; diff --git a/src/main/java/net/Broken/Outils/Command/CommandLoader.java b/src/main/java/net/Broken/Tools/Command/CommandLoader.java similarity index 97% rename from src/main/java/net/Broken/Outils/Command/CommandLoader.java rename to src/main/java/net/Broken/Tools/Command/CommandLoader.java index cd6ece5..9bc7131 100644 --- a/src/main/java/net/Broken/Outils/Command/CommandLoader.java +++ b/src/main/java/net/Broken/Tools/Command/CommandLoader.java @@ -1,4 +1,4 @@ -package net.Broken.Outils.Command; +package net.Broken.Tools.Command; import net.Broken.Commande; import net.Broken.MainBot; diff --git a/src/main/java/net/Broken/Outils/Command/CommandParser.java b/src/main/java/net/Broken/Tools/Command/CommandParser.java similarity index 98% rename from src/main/java/net/Broken/Outils/Command/CommandParser.java rename to src/main/java/net/Broken/Tools/Command/CommandParser.java index 632070a..3160f12 100644 --- a/src/main/java/net/Broken/Outils/Command/CommandParser.java +++ b/src/main/java/net/Broken/Tools/Command/CommandParser.java @@ -1,4 +1,4 @@ -package net.Broken.Outils.Command; +package net.Broken.Tools.Command; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; diff --git a/src/main/java/net/Broken/Outils/Command/NumberedCommande.java b/src/main/java/net/Broken/Tools/Command/NumberedCommande.java similarity index 97% rename from src/main/java/net/Broken/Outils/Command/NumberedCommande.java rename to src/main/java/net/Broken/Tools/Command/NumberedCommande.java index e3900da..cd65172 100644 --- a/src/main/java/net/Broken/Outils/Command/NumberedCommande.java +++ b/src/main/java/net/Broken/Tools/Command/NumberedCommande.java @@ -1,8 +1,8 @@ -package net.Broken.Outils.Command; +package net.Broken.Tools.Command; import net.Broken.Commande; -import net.Broken.Outils.FindContentOnWebPage; -import net.Broken.Outils.LimitChecker; -import net.Broken.Outils.Redirection; +import net.Broken.Tools.FindContentOnWebPage; +import net.Broken.Tools.LimitChecker; +import net.Broken.Tools.Redirection; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/net/Broken/Outils/DayListener.java b/src/main/java/net/Broken/Tools/DayListener.java similarity index 97% rename from src/main/java/net/Broken/Outils/DayListener.java rename to src/main/java/net/Broken/Tools/DayListener.java index 7f4edf1..b4c77b2 100644 --- a/src/main/java/net/Broken/Outils/DayListener.java +++ b/src/main/java/net/Broken/Tools/DayListener.java @@ -1,4 +1,4 @@ -package net.Broken.Outils; +package net.Broken.Tools; import net.Broken.Commands.Spam; import net.dv8tion.jda.core.exceptions.RateLimitedException; import org.apache.logging.log4j.LogManager; diff --git a/src/main/java/net/Broken/Outils/EmbedMessageUtils.java b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java similarity index 92% rename from src/main/java/net/Broken/Outils/EmbedMessageUtils.java rename to src/main/java/net/Broken/Tools/EmbedMessageUtils.java index 4dd380c..15ceaf7 100644 --- a/src/main/java/net/Broken/Outils/EmbedMessageUtils.java +++ b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java @@ -1,10 +1,9 @@ -package net.Broken.Outils; +package net.Broken.Tools; +import net.Broken.MainBot; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.Member; -import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.MessageEmbed; -import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import java.awt.*; @@ -69,5 +68,10 @@ public class EmbedMessageUtils { } + public static MessageEmbed getRegister(String message) { + return new EmbedBuilder().setTitle(":pencil: Web Registration :pencil:").setDescription(message).setColor(Color.green).setFooter("bot.seb6596.ovh", MainBot.jda.getSelfUser().getAvatarUrl()).build(); + } + + } \ No newline at end of file diff --git a/src/main/java/net/Broken/Outils/FindContentOnWebPage.java b/src/main/java/net/Broken/Tools/FindContentOnWebPage.java similarity index 95% rename from src/main/java/net/Broken/Outils/FindContentOnWebPage.java rename to src/main/java/net/Broken/Tools/FindContentOnWebPage.java index 6d13a34..e9a0edc 100644 --- a/src/main/java/net/Broken/Outils/FindContentOnWebPage.java +++ b/src/main/java/net/Broken/Tools/FindContentOnWebPage.java @@ -1,9 +1,8 @@ -package net.Broken.Outils; +package net.Broken.Tools; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; diff --git a/src/main/java/net/Broken/Outils/LimitChecker.java b/src/main/java/net/Broken/Tools/LimitChecker.java similarity index 98% rename from src/main/java/net/Broken/Outils/LimitChecker.java rename to src/main/java/net/Broken/Tools/LimitChecker.java index 89e17ef..fff834c 100644 --- a/src/main/java/net/Broken/Outils/LimitChecker.java +++ b/src/main/java/net/Broken/Tools/LimitChecker.java @@ -1,4 +1,4 @@ -package net.Broken.Outils; +package net.Broken.Tools; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/net/Broken/Outils/MessageTimeOut.java b/src/main/java/net/Broken/Tools/MessageTimeOut.java similarity index 97% rename from src/main/java/net/Broken/Outils/MessageTimeOut.java rename to src/main/java/net/Broken/Tools/MessageTimeOut.java index 01984a3..aa100eb 100644 --- a/src/main/java/net/Broken/Outils/MessageTimeOut.java +++ b/src/main/java/net/Broken/Tools/MessageTimeOut.java @@ -1,4 +1,4 @@ -package net.Broken.Outils; +package net.Broken.Tools; import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.exceptions.ErrorResponseException; diff --git a/src/main/java/net/Broken/Outils/Moderateur.java b/src/main/java/net/Broken/Tools/Moderateur.java similarity index 99% rename from src/main/java/net/Broken/Outils/Moderateur.java rename to src/main/java/net/Broken/Tools/Moderateur.java index 2bb33dc..c8fc8de 100644 --- a/src/main/java/net/Broken/Outils/Moderateur.java +++ b/src/main/java/net/Broken/Tools/Moderateur.java @@ -1,5 +1,5 @@ -package net.Broken.Outils; +package net.Broken.Tools; import net.Broken.MainBot; diff --git a/src/main/java/net/Broken/Outils/PrivateMessage.java b/src/main/java/net/Broken/Tools/PrivateMessage.java similarity index 80% rename from src/main/java/net/Broken/Outils/PrivateMessage.java rename to src/main/java/net/Broken/Tools/PrivateMessage.java index 9ac7c56..9b05c35 100644 --- a/src/main/java/net/Broken/Outils/PrivateMessage.java +++ b/src/main/java/net/Broken/Tools/PrivateMessage.java @@ -1,10 +1,8 @@ -package net.Broken.Outils; +package net.Broken.Tools; import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.MessageEmbed; -import net.dv8tion.jda.core.entities.PrivateChannel; import net.dv8tion.jda.core.entities.User; -import net.dv8tion.jda.core.exceptions.RateLimitedException; import org.apache.logging.log4j.Logger; /** diff --git a/src/main/java/net/Broken/Outils/Redirection.java b/src/main/java/net/Broken/Tools/Redirection.java similarity index 96% rename from src/main/java/net/Broken/Outils/Redirection.java rename to src/main/java/net/Broken/Tools/Redirection.java index 011971b..50b8243 100644 --- a/src/main/java/net/Broken/Outils/Redirection.java +++ b/src/main/java/net/Broken/Tools/Redirection.java @@ -1,4 +1,4 @@ -package net.Broken.Outils; +package net.Broken.Tools; import java.io.IOException; import java.io.InputStream; diff --git a/src/main/java/net/Broken/Tools/ResourceLoader.java b/src/main/java/net/Broken/Tools/ResourceLoader.java new file mode 100644 index 0000000..02eea97 --- /dev/null +++ b/src/main/java/net/Broken/Tools/ResourceLoader.java @@ -0,0 +1,40 @@ +package net.Broken.Tools; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + + + +public class ResourceLoader { + + private Logger logger = LogManager.getLogger(); + + public String getFile(String fileName) { + + StringBuilder result = new StringBuilder(""); + + //Get file from resources folder + ClassLoader classLoader = getClass().getClassLoader(); + InputStream file = classLoader.getResourceAsStream(fileName); + + try (Scanner scanner = new Scanner(file, "UTF-8")) { + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + result.append(line).append("\n"); + } + + scanner.close(); + + } + + return result.toString(); + + } + +} diff --git a/src/main/java/net/Broken/Tools/UserManager/Exceptions/PasswordNotMatchException.java b/src/main/java/net/Broken/Tools/UserManager/Exceptions/PasswordNotMatchException.java new file mode 100644 index 0000000..0eeac2e --- /dev/null +++ b/src/main/java/net/Broken/Tools/UserManager/Exceptions/PasswordNotMatchException.java @@ -0,0 +1,4 @@ +package net.Broken.Tools.UserManager.Exceptions; + +public class PasswordNotMatchException extends Exception{ +} diff --git a/src/main/java/net/Broken/Tools/UserManager/Exceptions/TokenNotMatch.java b/src/main/java/net/Broken/Tools/UserManager/Exceptions/TokenNotMatch.java new file mode 100644 index 0000000..fc5d6e9 --- /dev/null +++ b/src/main/java/net/Broken/Tools/UserManager/Exceptions/TokenNotMatch.java @@ -0,0 +1,4 @@ +package net.Broken.Tools.UserManager.Exceptions; + +public class TokenNotMatch extends Exception { +} diff --git a/src/main/java/net/Broken/Tools/UserManager/Exceptions/UnknownTokenException.java b/src/main/java/net/Broken/Tools/UserManager/Exceptions/UnknownTokenException.java new file mode 100644 index 0000000..e72b52e --- /dev/null +++ b/src/main/java/net/Broken/Tools/UserManager/Exceptions/UnknownTokenException.java @@ -0,0 +1,4 @@ +package net.Broken.Tools.UserManager.Exceptions; + +public class UnknownTokenException extends Exception{ +} diff --git a/src/main/java/net/Broken/Tools/UserManager/Exceptions/UserAlreadyRegistered.java b/src/main/java/net/Broken/Tools/UserManager/Exceptions/UserAlreadyRegistered.java new file mode 100644 index 0000000..f945a30 --- /dev/null +++ b/src/main/java/net/Broken/Tools/UserManager/Exceptions/UserAlreadyRegistered.java @@ -0,0 +1,4 @@ +package net.Broken.Tools.UserManager.Exceptions; + +public class UserAlreadyRegistered extends Exception { +} diff --git a/src/main/java/net/Broken/Tools/UserManager/Exceptions/UserNotFoundException.java b/src/main/java/net/Broken/Tools/UserManager/Exceptions/UserNotFoundException.java new file mode 100644 index 0000000..232c53c --- /dev/null +++ b/src/main/java/net/Broken/Tools/UserManager/Exceptions/UserNotFoundException.java @@ -0,0 +1,4 @@ +package net.Broken.Tools.UserManager.Exceptions; + +public class UserNotFoundException extends Exception{ +} diff --git a/src/main/java/net/Broken/Tools/UserManager/PasswordUtils.java b/src/main/java/net/Broken/Tools/UserManager/PasswordUtils.java new file mode 100644 index 0000000..a965602 --- /dev/null +++ b/src/main/java/net/Broken/Tools/UserManager/PasswordUtils.java @@ -0,0 +1,14 @@ +package net.Broken.Tools.UserManager; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class PasswordUtils { + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/src/main/java/net/Broken/Tools/UserManager/UserRegister.java b/src/main/java/net/Broken/Tools/UserManager/UserRegister.java new file mode 100644 index 0000000..715a218 --- /dev/null +++ b/src/main/java/net/Broken/Tools/UserManager/UserRegister.java @@ -0,0 +1,151 @@ +package net.Broken.Tools.UserManager; + +import net.Broken.DB.Entity.PendingUserEntity; +import net.Broken.DB.Entity.UserEntity; +import net.Broken.DB.Repository.PendingUserRepository; +import net.Broken.DB.Repository.UserRepository; +import net.Broken.MainBot; +import net.Broken.RestApi.Data.UserManager.UserInfoData; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.PrivateMessage; +import net.Broken.Tools.ResourceLoader; +import net.Broken.Tools.UserManager.Exceptions.*; +import net.dv8tion.jda.core.entities.MessageEmbed; +import net.dv8tion.jda.core.entities.User; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.security.crypto.password.PasswordEncoder; + +import java.security.SecureRandom; +import java.util.List; +import java.util.UUID; + +public class UserRegister { + + private Logger logger = LogManager.getLogger(); + + + public String sendCheckToken(PendingUserRepository pendingUserRepository, UserRepository userRepository, PasswordEncoder passwordEncoder, UserInfoData userInfo) throws UserNotFoundException, PasswordNotMatchException, UserAlreadyRegistered { + + logger.info("New registration for " + userInfo.name); + + List users = MainBot.jda.getUsersByName(userInfo.name,true); + if(users.size() < 1) + throw new UserNotFoundException(); + + User user = users.get(0); + logger.info("User found!"); + + PendingUserEntity pendingUserEntity = null; + String token = ""; + + //Test if exist on register user + if(userRepository.findByJdaId(user.getId()).size() > 0){ + logger.warn("User already registered!"); + throw new UserAlreadyRegistered(); + } + + + + //Check if exist in pading user Table + List pendingUsers = pendingUserRepository.findByJdaId(user.getId()); + if(pendingUsers.size() != 0){ + PendingUserEntity thisUser = pendingUsers.get(0); + if(passwordEncoder.matches(userInfo.password, thisUser.getPassword())){ + logger.info("Password matches"); + pendingUserEntity = thisUser; + token = thisUser.getCheckToken(); + }else{ + logger.warn("Password Not Match!"); + throw new PasswordNotMatchException(); + } + } + + + + logger.info("Generating check Token..."); + if(token.equals("")){ + token = generateCheckToken(); + } + + logger.info("Token generated: " + token); + if(pendingUserEntity == null) { + pendingUserEntity = new PendingUserEntity(user.getName(), user.getId(), token, passwordEncoder.encode(userInfo.password)); + pendingUserEntity = pendingUserRepository.save(pendingUserEntity); + } + + String message = new ResourceLoader().getFile("MessagesTemplates/RegisterMessage.md"); + + message = message.replace("%code",token); + + MessageEmbed ebM = EmbedMessageUtils.getRegister(message); + PrivateMessage.send(user,ebM,logger); + return pendingUserEntity.getId().toString(); + + } + + public PendingUserEntity confirmCheckToken(PendingUserRepository pendingUserRepository, int id, String checkToken ) throws TokenNotMatch, UserNotFoundException { + PendingUserEntity pendingUser = pendingUserRepository.findOne(id); + if(pendingUser != null) { + if(pendingUser.getCheckToken().equals(checkToken)){ + logger.info("Check Token match!"); + } + else{ + logger.warn("Check token not match!"); + throw new TokenNotMatch(); + } + } + else{ + logger.warn("Id not Found!"); + throw new UserNotFoundException(); + } + return pendingUser; + } + + public UserEntity getUser(UserRepository userRepository, PasswordEncoder passwordEncoder, UserInfoData userInfoData) throws UserNotFoundException, PasswordNotMatchException { + List users = userRepository.findByName(userInfoData.name); + if(users.size()<1){ + logger.warn("Login with unknown username: " + userInfoData.name); + throw new UserNotFoundException(); + } + else{ + UserEntity user = users.get(0); + if(passwordEncoder.matches(userInfoData.password,user.getPassword())){ + logger.info("Login successful for " + user.getName()); + return user; + } + else + { + logger.warn("Login fail for " + user.getName() + ", wrong password!"); + throw new PasswordNotMatchException(); + } + } + } + + public UserEntity getUserWithApiToken(UserRepository userRepository, String token) throws UnknownTokenException { + List users = userRepository.findByApiToken(token); + if(users.size() > 0){ + return users.get(0); + } + else + throw new UnknownTokenException(); + + } + + public String generateApiToken(){ + return UUID.randomUUID().toString(); + } + + private String generateCheckToken(){ + SecureRandom random = new SecureRandom(); + long longToken = Math.abs( random.nextLong() ); + String randomStr = Long.toString( longToken, 16 ); + randomStr = randomStr.substring(0,4); + randomStr = randomStr.toUpperCase(); + return randomStr; + } + + +} diff --git a/src/main/java/net/Broken/Outils/UserSpamUtils.java b/src/main/java/net/Broken/Tools/UserSpamUtils.java similarity index 97% rename from src/main/java/net/Broken/Outils/UserSpamUtils.java rename to src/main/java/net/Broken/Tools/UserSpamUtils.java index f99fb1c..e7e63d1 100644 --- a/src/main/java/net/Broken/Outils/UserSpamUtils.java +++ b/src/main/java/net/Broken/Tools/UserSpamUtils.java @@ -1,4 +1,4 @@ -package net.Broken.Outils; +package net.Broken.Tools; import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Message; diff --git a/src/main/java/net/Broken/audio/AudioM.java b/src/main/java/net/Broken/audio/AudioM.java index c51d971..d608d67 100644 --- a/src/main/java/net/Broken/audio/AudioM.java +++ b/src/main/java/net/Broken/audio/AudioM.java @@ -9,15 +9,13 @@ import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; import net.Broken.MainBot; -import net.Broken.Outils.EmbedMessageUtils; -import net.Broken.Outils.MessageTimeOut; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.MessageTimeOut; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Message; -import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.VoiceChannel; import net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; -import net.dv8tion.jda.core.managers.AudioManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/net/Broken/webView/ResisterWebView.java b/src/main/java/net/Broken/webView/ResisterWebView.java new file mode 100644 index 0000000..3c0c246 --- /dev/null +++ b/src/main/java/net/Broken/webView/ResisterWebView.java @@ -0,0 +1,16 @@ +package net.Broken.webView; + + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class ResisterWebView { + @RequestMapping("/register") + public String music(@RequestParam(value="id", required = true, defaultValue = "") String id, Model model){ + model.addAttribute("id", id); + return "register"; + } +} diff --git a/src/main/resources/MessagesTemplates/RegisterMessage.md b/src/main/resources/MessagesTemplates/RegisterMessage.md new file mode 100644 index 0000000..630fbf3 --- /dev/null +++ b/src/main/resources/MessagesTemplates/RegisterMessage.md @@ -0,0 +1,5 @@ + +Une tentative d'association à etait demandé. + +Voici le code de vérification: +**%code** diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4c6606e..e1fd2b4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,6 @@ server.port=${PORT} +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=${DB_URL} +spring.datasource.username=${DB_USER} +spring.datasource.password=${DB_PWD} +spring.thymeleaf.cache=true \ No newline at end of file diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 45bcb8a..467272d 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -7,23 +7,29 @@ [%d{HH:mm:ss.SSS}]%highlight{[%-5level]}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan}[%-30.30c{1.}]: %highlight{%msg%n}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan} - + [%d{HH:mm:ss.SSS}]%highlight{[%-5level]}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan}[%-30.30c{1.}]: %highlight{%msg%n}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan} - + + + + [%d{HH:mm:ss.SSS}]%highlight{[%-5level]}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan}[%-30.30c{1.}]: %highlight{%msg%n}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan} + + + diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css index 397fb88..f6d371e 100644 --- a/src/main/resources/static/css/style.css +++ b/src/main/resources/static/css/style.css @@ -1,8 +1,8 @@ -.nav-wrapper{ - margin-right: 1%; - margin-left: 1%; - width: 100%; -} +/*.nav-wrapper{*/ + /*!*margin-right: 1%;*!*/ + /*margin-left: 1%;*/ + /*width: 100%;*/ +/*}*/ .collapsible-body{ diff --git a/src/main/resources/static/favicon.png b/src/main/resources/static/favicon.png new file mode 100644 index 0000000..d690bb4 Binary files /dev/null and b/src/main/resources/static/favicon.png differ diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index aa840ce..d71e1cf 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -4,6 +4,9 @@ Discord Bot + @@ -12,32 +15,109 @@ - + + + + + + + + + + + + + + + + + + + + + + + +
@@ -45,14 +125,8 @@ - + + diff --git a/src/main/resources/static/js/init.js b/src/main/resources/static/js/init.js index f0e86c8..99fde47 100644 --- a/src/main/resources/static/js/init.js +++ b/src/main/resources/static/js/init.js @@ -25,12 +25,14 @@ $(document).ready(function() { $('#btn_play').click(function () { switch (state){ case "PLAYING": - sendCommand(JSON.stringify({ command: "PAUSE"})) + sendCommand({ command: "PAUSE"}) break; case "PAUSE": - sendCommand(JSON.stringify({ command: "PLAY"})) + sendCommand({ command: "PLAY"}) break; + default: + sendCommand({command: "PLAY"}) } }); @@ -38,10 +40,10 @@ $(document).ready(function() { $('#btn_next').click(function () { - sendCommand(JSON.stringify({ command: "NEXT"})); + sendCommand({ command: "NEXT"}); }); $('#btn_stop').click(function () { - sendCommand(JSON.stringify({ command: "STOP"})); + sendCommand({ command: "STOP"}); }); $('.dropdown-button').dropdown({ @@ -85,7 +87,7 @@ $(document).ready(function() { var command = { command: "FLUSH" }; - sendCommand(JSON.stringify(command)); + sendCommand(command); }); $('#btn_add_top').click(function () { @@ -96,7 +98,7 @@ $(document).ready(function() { onHead: true }; $('#input_link').val(''); - sendCommand(JSON.stringify(command)); + sendCommand(command); }); $('#btn_add_bottom').click(function () { @@ -108,7 +110,7 @@ $(document).ready(function() { onHead: false }; $('#input_link').val(''); - sendCommand(JSON.stringify(command)); + sendCommand(command); }); $('#btn_ok_channel').click(function () { @@ -117,7 +119,7 @@ $(document).ready(function() { command: "CONNECT", chanelId: $('input[name=vocalRadio]:checked').val() }; - sendCommand(JSON.stringify(command)); + sendCommand(command); }); }) @@ -144,27 +146,50 @@ function getCurentMusic() { $('#btn_info').addClass("determinate").removeClass("indeterminate"); } $('#music_progress').width("0%"); + if(Cookies.get('token') != undefined){ + if (!$('#btn_stop').hasClass("disabled")) { + $('#btn_stop').addClass("disabled"); + } + if (!$('#btn_info').hasClass("disabled")) { + $('#btn_info').addClass("disabled"); + } + if ($('#add_btn').hasClass("disabled")) { + $('#add_btn').removeClass("disabled"); + } + if ($('#flush_btn').hasClass("disabled")) { + $('#flush_btn').removeClass("disabled"); + } + if ($('#btn_play').hasClass("disabled")) { + $('#btn_play').removeClass("disabled"); + } + if ($('#btn_next').hasClass("disabled")) { + $('#btn_next').removeClass("disabled"); + } + } + else{ + if (!$('#btn_stop').hasClass("disabled")) { + $('#btn_stop').addClass("disabled"); + } + if (!$('#btn_info').hasClass("disabled")) { + $('#btn_info').addClass("disabled"); + } + if (!$('#add_btn').hasClass("disabled")) { + $('#add_btn').addClass("disabled"); + } + if (!$('#flush_btn').hasClass("disabled")) { + $('#flush_btn').addClass("disabled"); + } + + if (!$('#btn_play').hasClass("disabled")) { + $('#btn_play').addClass("disabled"); + } + if (!$('#btn_next').hasClass("disabled")) { + $('#btn_next').addClass("disabled"); + } + } $('#btn_play').children().text("play_arrow"); - if (!$('#btn_stop').hasClass("disabled")) { - $('#btn_stop').addClass("disabled"); - } - if (!$('#btn_info').hasClass("disabled")) { - $('#btn_info').addClass("disabled"); - } - if ($('#add_btn').hasClass("disabled")) { - $('#add_btn').removeClass("disabled"); - } - if ($('#flush_btn').hasClass("disabled")) { - $('#flush_btn').removeClass("disabled"); - } - if ($('#btn_play').hasClass("disabled")) { - $('#btn_play').removeClass("disabled"); - } - if ($('#btn_next').hasClass("disabled")) { - $('#btn_next').removeClass("disabled"); - } $('#music_img').attr("src","/img/no_music.jpg"); $('#total_time').text("00:00"); @@ -206,6 +231,7 @@ function getCurentMusic() { $('#music_progress').width("0%"); $('#btn_play').children().text("play_arrow"); + if (!$('#btn_play').hasClass("disabled")) { $('#btn_play').addClass("disabled"); } @@ -227,14 +253,16 @@ function getCurentMusic() { $('#music_img').attr("src","/img/disconnected.png"); - - if(!disconected){ - getChannels(); - disconected = true; + if(Cookies.get('token') != undefined){ + if(!disconected){ + getChannels(); + disconected = true; + } } + break; } getPlayList(); @@ -278,7 +306,7 @@ function getPlayList() { command: "DELL", url: $(this).attr("data_url") }; - sendCommand(JSON.stringify(command)); + sendCommand(command); }); @@ -334,30 +362,55 @@ function updateControl(data){ $('#music_text').text(data.info.title); var percent = (data.currentPos / data.info.length) * 100; // console.log(percent) - if (!$('#btn_info').hasClass("indeterminate")) { - $('#btn_info').addClass("determinate").removeClass("indeterminate"); + if (!$('#music_progress').hasClass("indeterminate")) { + $('#music_progress').addClass("determinate").removeClass("indeterminate"); } $('#music_progress').width(percent + "%"); - if ($('#btn_play').hasClass("disabled")) { - $('#btn_play').removeClass("disabled"); + if(Cookies.get('token') != undefined){ + if ($('#btn_play').hasClass("disabled")) { + $('#btn_play').removeClass("disabled"); + } + if ($('#btn_stop').hasClass("disabled")) { + $('#btn_stop').removeClass("disabled"); + } + if ($('#btn_info').hasClass("disabled")) { + $('#btn_info').removeClass("disabled"); + } + if ($('#add_btn').hasClass("disabled")) { + $('#add_btn').removeClass("disabled"); + } + if ($('#flush_btn').hasClass("disabled")) { + $('#flush_btn').removeClass("disabled"); + } + + if ($('#btn_next').hasClass("disabled")) { + $('#btn_next').removeClass("disabled"); + } } - if ($('#btn_stop').hasClass("disabled")) { - $('#btn_stop').removeClass("disabled"); - } - if ($('#btn_info').hasClass("disabled")) { - $('#btn_info').removeClass("disabled"); - } - if ($('#add_btn').hasClass("disabled")) { - $('#add_btn').removeClass("disabled"); - } - if ($('#flush_btn').hasClass("disabled")) { - $('#flush_btn').removeClass("disabled"); + else + { + if (!$('#btn_play').hasClass("disabled")) { + $('#btn_play').addClass("disabled"); + } + if (!$('#btn_stop').hasClass("disabled")) { + $('#btn_stop').addClass("disabled"); + } + if (!$('#btn_info').hasClass("disabled")) { + $('#btn_info').addClass("disabled"); + } + if (!$('#add_btn').hasClass("disabled")) { + $('#add_btn').addClass("disabled"); + } + if (!$('#flush_btn').hasClass("disabled")) { + $('#flush_btn').addClass("disabled"); + } + + if (!$('#btn_next').hasClass("disabled")) { + $('#btn_next').addClass("disabled"); + } } - if ($('#btn_next').hasClass("disabled")) { - $('#btn_next').removeClass("disabled"); - } $('#music_img').attr("src","https://img.youtube.com/vi/"+data.info.identifier+"/hqdefault.jpg"); // console.log(data); @@ -366,13 +419,15 @@ function updateControl(data){ updateModal(data); } -function sendCommand(commandStr){ +function sendCommand(command){ + command["token"] = Cookies.get('token'); + console.log(command) $.ajax({ type: "POST", dataType: 'json', contentType: 'application/json', url: "/api/music/command", - data: commandStr, + data: JSON.stringify(command), success: function (data) { console.log(data); } @@ -380,6 +435,11 @@ function sendCommand(commandStr){ }).fail(function (data) { console.log(data); alert(data.responseJSON.Message); + if(data.responseJSON.error === "token"){ + Cookies.remove('token'); + Cookies.remove('name'); + location.reload(); + } }); } diff --git a/src/main/resources/static/js/js.cookie.js b/src/main/resources/static/js/js.cookie.js new file mode 100644 index 0000000..9a0945e --- /dev/null +++ b/src/main/resources/static/js/js.cookie.js @@ -0,0 +1,165 @@ +/*! + * JavaScript Cookie v2.2.0 + * https://github.com/js-cookie/js-cookie + * + * Copyright 2006, 2015 Klaus Hartl & Fagner Brack + * Released under the MIT license + */ +;(function (factory) { + var registeredInModuleLoader = false; + if (typeof define === 'function' && define.amd) { + define(factory); + registeredInModuleLoader = true; + } + if (typeof exports === 'object') { + module.exports = factory(); + registeredInModuleLoader = true; + } + if (!registeredInModuleLoader) { + var OldCookies = window.Cookies; + var api = window.Cookies = factory(); + api.noConflict = function () { + window.Cookies = OldCookies; + return api; + }; + } +}(function () { + function extend () { + var i = 0; + var result = {}; + for (; i < arguments.length; i++) { + var attributes = arguments[ i ]; + for (var key in attributes) { + result[key] = attributes[key]; + } + } + return result; + } + + function init (converter) { + function api (key, value, attributes) { + var result; + if (typeof document === 'undefined') { + return; + } + + // Write + + if (arguments.length > 1) { + attributes = extend({ + path: '/' + }, api.defaults, attributes); + + if (typeof attributes.expires === 'number') { + var expires = new Date(); + expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5); + attributes.expires = expires; + } + + // We're using "expires" because "max-age" is not supported by IE + attributes.expires = attributes.expires ? attributes.expires.toUTCString() : ''; + + try { + result = JSON.stringify(value); + if (/^[\{\[]/.test(result)) { + value = result; + } + } catch (e) {} + + if (!converter.write) { + value = encodeURIComponent(String(value)) + .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); + } else { + value = converter.write(value, key); + } + + key = encodeURIComponent(String(key)); + key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent); + key = key.replace(/[\(\)]/g, escape); + + var stringifiedAttributes = ''; + + for (var attributeName in attributes) { + if (!attributes[attributeName]) { + continue; + } + stringifiedAttributes += '; ' + attributeName; + if (attributes[attributeName] === true) { + continue; + } + stringifiedAttributes += '=' + attributes[attributeName]; + } + return (document.cookie = key + '=' + value + stringifiedAttributes); + } + + // Read + + if (!key) { + result = {}; + } + + // To prevent the for loop in the first place assign an empty array + // in case there are no cookies at all. Also prevents odd result when + // calling "get()" + var cookies = document.cookie ? document.cookie.split('; ') : []; + var rdecode = /(%[0-9A-Z]{2})+/g; + var i = 0; + + for (; i < cookies.length; i++) { + var parts = cookies[i].split('='); + var cookie = parts.slice(1).join('='); + + if (!this.json && cookie.charAt(0) === '"') { + cookie = cookie.slice(1, -1); + } + + try { + var name = parts[0].replace(rdecode, decodeURIComponent); + cookie = converter.read ? + converter.read(cookie, name) : converter(cookie, name) || + cookie.replace(rdecode, decodeURIComponent); + + if (this.json) { + try { + cookie = JSON.parse(cookie); + } catch (e) {} + } + + if (key === name) { + result = cookie; + break; + } + + if (!key) { + result[name] = cookie; + } + } catch (e) {} + } + + return result; + } + + api.set = api; + api.get = function (key) { + return api.call(api, key); + }; + api.getJSON = function () { + return api.apply({ + json: true + }, [].slice.call(arguments)); + }; + api.defaults = {}; + + api.remove = function (key, attributes) { + api(key, '', extend(attributes, { + expires: -1 + })); + }; + + api.withConverter = init; + + return api; + } + + return init(function () {}); +})); diff --git a/src/main/resources/static/js/navabar.js b/src/main/resources/static/js/navabar.js new file mode 100644 index 0000000..8ae0ba7 --- /dev/null +++ b/src/main/resources/static/js/navabar.js @@ -0,0 +1,138 @@ +var nav_bar_account_link; +var connected_link = "account_box"; +var disconnected_link = "account_box"; +var input_name; +var input_psw; +var btn_submit; +var btn_disconnect; +var nav_name; + +$(document).ready(function() { + $('.button-navbar-mobile').sideNav({ + menuWidth: 400, // Default is 300 + edge: 'right', // Choose the horizontal origin + closeOnClick: false, // Closes side-nav on clicks, useful for Angular/Meteor + draggable: true // Choose whether you can drag to open on touch screens, + }); + + + nav_bar_account_link = $("#nav-bar-account"); + input_name = $("#user_input"); + input_psw = $("#password_input"); + btn_submit = $("#btn-submit-connect"); + btn_disconnect = $("#nav-disconnect"); + nav_name = $("#nav-name"); + + if(Cookies.get('token') === undefined){ + disconnected() + } + else{ + connected(); + } + + listeners(); +}); + + +function popOutSubmit(){ + if (btn_submit.hasClass("scale-in")) { + btn_submit.removeClass("scale-in"); + btn_submit.addClass("scale-out"); + } +} + +function popInSubmit(){ + if (btn_submit.hasClass("scale-out")) { + btn_submit.removeClass("scale-out"); + btn_submit.addClass("scale-in"); + } +} + + + +function connected(){ + console.log("Connected!"); + nav_bar_account_link.html(connected_link); + $('.dropdown-account').dropdown({ + constrainWidth: false, // Does not change width of dropdown to that of the activator + belowOrigin: true, // Displays dropdown below the button + alignment: 'left', // Displays dropdown with edge aligned to the left of button + stopPropagation: false // Stops event propagation + } + ); + nav_name.text(Cookies.get('name')); +} + +function disconnected() { + console.log("Disconnected"); + nav_bar_account_link.html(disconnected_link); + var modalConnection = $('#modal_connection'); + modalConnection.modal(); + if (typeof needLogin !== 'undefined'){ + modalConnection.modal('open'); + } + +} + + +function tryConnection() { + var request = { name: input_name.val(), password: input_psw.val()}; + $.ajax({ + type: "POST", + dataType: 'json', + contentType: 'application/json', + url: "/api/userManagement/requestToken", + data: JSON.stringify(request), + success: function (data) { + console.log(data); + Cookies.set('token',data.token); + Cookies.set('name', data.name); + debugger; + location.reload(); + } + + }).fail(function (data) { + console.log(data); + switch(data.responseJSON.error){ + case "user": + input_name.addClass("invalid"); + break; + + case "password": + input_psw.addClass("invalid"); + break; + } + + }); + + + return true; +} + + +function listeners() { + input_name.on("input", function () { + if(input_name.val() !== "" && input_psw.val() !== "") { + popInSubmit(); + }else + { + popOutSubmit(); + } + }); + + input_psw.on("input", function () { + if(input_name.val() !== "" && input_psw.val() !== "") { + popInSubmit(); + }else + { + popOutSubmit(); + } + }); + + btn_disconnect.click(function () { + Cookies.remove('token'); + Cookies.remove('name'); + location.reload(); + }); + +} \ No newline at end of file diff --git a/src/main/resources/static/js/register.js b/src/main/resources/static/js/register.js new file mode 100644 index 0000000..f64baee --- /dev/null +++ b/src/main/resources/static/js/register.js @@ -0,0 +1,176 @@ + +var ok_passwrd = false; +$(document).ready(function() { + var baseUrl = window.location.protocol + "//" +window.location.host + window.location.pathname; + console.log(baseUrl); + $('.button-collapse-1').sideNav({ + menuWidth: 400, // Default is 300 + edge: 'right', // Choose the horizontal origin + closeOnClick: false, // Closes side-nav on clicks, useful for Angular/Meteor + draggable: true // Choose whether you can drag to open on touch screens, + }); + + var sendBtn = $('#sendBtn'); + + + $('#name').on("input", function () { + if($('#name').val() === ""){ + if (sendBtn.hasClass("scale-in")) { + sendBtn.removeClass("scale-in"); + sendBtn.addClass("scale-out"); + } + } + else{ + if (sendBtn.hasClass("scale-out") && ok_passwrd) { + sendBtn.removeClass("scale-out"); + sendBtn.addClass("scale-in"); + } + } + }); + + + var passwrd = $('#passwrd'); + var confirm = $('#passwrd2'); + passwrd.on("input", function () { + if((passwrd.val() === confirm.val())&& passwrd.val() !== ''){ + if (passwrd.hasClass("invalid")) { + passwrd.addClass("valid"); + passwrd.removeClass("invalid"); + confirm.addClass("valid"); + confirm.removeClass("invalid"); + + + } + if($('#name').val() !== ""){ + if (sendBtn.hasClass("scale-out")) { + sendBtn.removeClass("scale-out"); + sendBtn.addClass("scale-in"); + } + } + ok_passwrd = true; + } + else{ + if (!passwrd.hasClass("invalid")) { + passwrd.addClass("invalid"); + passwrd.removeClass("valid"); + confirm.addClass("invalid"); + confirm.removeClass("valid"); + + } + if (sendBtn.hasClass("scale-in")) { + sendBtn.removeClass("scale-in"); + sendBtn.addClass("scale-out"); + } + ok_passwrd = false; + } + }); + + confirm.on("input", function () { + if((passwrd.val() === confirm.val())&& passwrd.val() !== ''){ + if (passwrd.hasClass("invalid")) { + passwrd.addClass("valid"); + passwrd.removeClass("invalid"); + confirm.addClass("valid"); + confirm.removeClass("invalid"); + + } + if($('#name').val() !== ""){ + if (sendBtn.hasClass("scale-out")) { + sendBtn.removeClass("scale-out"); + sendBtn.addClass("scale-in"); + } + } + ok_passwrd = true; + } + else{ + if (!passwrd.hasClass("invalid")) { + passwrd.addClass("invalid"); + passwrd.removeClass("valid"); + confirm.addClass("invalid"); + confirm.removeClass("valid"); + } + + if (sendBtn.hasClass("scale-in")) { + sendBtn.removeClass("scale-in"); + sendBtn.addClass("scale-out"); + } + ok_passwrd = false; + } + }); + + + $('#sendBtn').click(function () { + var name = $('#name').val(); + var password = $('#passwrd').val(); + + $.ajax({ + type: "POST", + dataType: 'json', + contentType: 'application/json', + url: "/api/userManagement/preRegister", + data: JSON.stringify({ name: name, password: password}), + success: function (data) { + console.log(data); + window.location.href = baseUrl + "?id="+data.id + } + + }).fail(function (data) { + console.log(data); + if(data.status === 404){ + alert("User Not Found!"); + $('#name').addClass("invalid"); + $('#name').removeClass("valid"); + + } + else{ + alert(data.responseJSON.message); + } + + }); + }); + + $('#modalToken').modal({dismissible: false}); + if(id !== ''){ + $('#modalToken').modal('open'); + } + + + $('#input_preToken').on("input", function () { + var sendBtn = $('#preTokenSend'); + if($('#input_preToken').val().length < 4){ + if (sendBtn.hasClass("scale-in")) { + sendBtn.removeClass("scale-in"); + sendBtn.addClass("scale-out"); + } + } + else{ + if (sendBtn.hasClass("scale-out")) { + sendBtn.removeClass("scale-out"); + sendBtn.addClass("scale-in"); + } + } + }); + $('#preTokenSend').click(function () { + $.ajax({ + type: "POST", + dataType: 'json', + contentType: 'application/json', + url: "/api/userManagement/confirmAccount", + data: JSON.stringify({ id: id.toString(), checkToken: $('#input_preToken').val()}), + success: function (data) { + console.log(data); + Cookies.set('token',data.token); + Cookies.set('name', data.name); + debugger; + window.location.href = "/" + } + + }).fail(function (data) { + console.log(data); + alert(data.responseJSON.message); + + }); + }); + + +}); diff --git a/src/main/resources/templates/music.html b/src/main/resources/templates/music.html index 7ac65a4..cd1d19c 100644 --- a/src/main/resources/templates/music.html +++ b/src/main/resources/templates/music.html @@ -3,7 +3,10 @@ - Discord Bot - Music Control + Music Control - Discord Bot + @@ -13,31 +16,106 @@ + + + + + + + + + + + + + + + + + + + + +
@@ -214,6 +292,11 @@ + + + diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html new file mode 100644 index 0000000..b8bf740 --- /dev/null +++ b/src/main/resources/templates/register.html @@ -0,0 +1,210 @@ + + + + + + User Registration - Discord Bot + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Account Registration

+
+
+
+
+ assignment_ind + + +
+
+
+
+
+
+ security + + +
+
+
+
+
+
+ security + + +
+
+
+
+ Sendsend +
+
+
+ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file