diff --git a/LaunchServer/build.gradle b/LaunchServer/build.gradle index 0316c75a..c4975b31 100644 --- a/LaunchServer/build.gradle +++ b/LaunchServer/build.gradle @@ -53,6 +53,7 @@ bundle project(':Radon') bundle 'commons-codec:commons-codec:1.12' bundle 'org.javassist:javassist:3.25.0-GA' bundle 'io.netty:netty-all:4.1.36.Final' + bundle 'org.hibernate:hibernate-core:5.4.3.Final' bundle 'org.slf4j:slf4j-simple:1.7.25' bundle 'org.slf4j:slf4j-api:1.7.25' diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 3f0f0010..98035a73 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -66,6 +66,7 @@ import pro.gravit.launchserver.components.AuthLimiterComponent; import pro.gravit.launchserver.components.Component; import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; +import pro.gravit.launchserver.dao.UserService; import pro.gravit.launchserver.legacy.Response; import pro.gravit.launchserver.manangers.LaunchServerGsonManager; import pro.gravit.launchserver.manangers.MirrorManager; @@ -458,6 +459,8 @@ public static void main(String... args) throws Throwable { public final ModulesManager modulesManager; + public final UserService userService; + public final MirrorManager mirrorManager; public final ReloadManager reloadManager; @@ -611,6 +614,7 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException socketHookManager = new SocketHookManager(); authHookManager = new AuthHookManager(); configManager = new ConfigManager(); + userService = new UserService(this); GarbageManager.registerNeedGC(sessionManager); reloadManager.registerReloadable("launchServer", this); registerObject("permissionsHandler", config.permissionsHandler); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java index 82be2450..d70e022f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java @@ -24,7 +24,7 @@ public AuthProviderPair(AuthProvider provider, AuthHandler handler, TextureProvi public void init(LaunchServer srv) { provider.init(srv); - handler.init(); + handler.init(srv); } public void close() throws IOException { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java index 92b066b1..954b65ba 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java @@ -3,10 +3,12 @@ import java.io.IOException; import java.util.UUID; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.auth.provider.AuthProviderResult; import pro.gravit.utils.ProviderMap; + public abstract class AuthHandler implements AutoCloseable { public static ProviderMap providers = new ProviderMap<>("AuthHandler"); private static boolean registredHandl = false; @@ -23,10 +25,13 @@ public static void registerHandlers() { providers.register("memory", MemoryAuthHandler.class); providers.register("mysql", MySQLAuthHandler.class); providers.register("request", RequestAuthHandler.class); + providers.register("hibernate", HibernateAuthHandler.class); registredHandl = true; } } + protected transient LaunchServer srv; + public abstract UUID auth(AuthProviderResult authResult) throws IOException; public abstract UUID checkServer(String username, String serverID) throws IOException; @@ -43,7 +48,7 @@ public static void registerHandlers() { public abstract String uuidToUsername(UUID uuid) throws IOException; - public void init() { - + public void init(LaunchServer srv) { + this.srv = srv; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java new file mode 100644 index 00000000..1dc4df16 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java @@ -0,0 +1,43 @@ +package pro.gravit.launchserver.auth.handler; + +import java.io.IOException; +import java.util.UUID; + +import pro.gravit.launchserver.dao.User; + +public class HibernateAuthHandler extends CachedAuthHandler { + @Override + protected Entry fetchEntry(String username) throws IOException { + User user = srv.userService.findUserByUsername(username); + if(user == null) return null; + return new Entry(user.uuid, username, user.getAccessToken(), user.serverID); + } + + @Override + protected Entry fetchEntry(UUID uuid) throws IOException { + User user = srv.userService.findUserByUUID(uuid); + if(user == null) return null; + return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID); + } + + @Override + protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { + User user = srv.userService.findUserByUUID(uuid); + user.setAccessToken(accessToken); + srv.userService.updateUser(user); + return true; + } + + @Override + protected boolean updateServerID(UUID uuid, String serverID) throws IOException { + User user = srv.userService.findUserByUUID(uuid); + user.serverID = serverID; + srv.userService.updateUser(user); + return true; + } + + @Override + public void close() throws IOException { + + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/MySQLAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/MySQLAuthHandler.java index 9415808d..a0ef56f4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/MySQLAuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/MySQLAuthHandler.java @@ -7,6 +7,7 @@ import java.sql.SQLException; import java.util.UUID; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.auth.MySQLSourceConfig; import pro.gravit.utils.helper.LogHelper; @@ -25,7 +26,8 @@ public final class MySQLAuthHandler extends CachedAuthHandler { private transient String updateServerIDSQL; @Override - public void init() { + public void init(LaunchServer srv) { + super.init(srv); //Verify if (mySQLHolder == null) LogHelper.error("[Verify][AuthHandler] mySQLHolder cannot be null"); if (uuidColumn == null) LogHelper.error("[Verify][AuthHandler] uuidColumn cannot be null"); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/RequestAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/RequestAuthHandler.java index 9a288465..2b7bfa47 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/RequestAuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/RequestAuthHandler.java @@ -4,6 +4,7 @@ import java.net.URL; import java.util.UUID; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.utils.helper.CommonHelper; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; @@ -19,7 +20,8 @@ public final class RequestAuthHandler extends CachedAuthHandler { private String goodResponse = "OK"; @Override - public void init() { + public void init(LaunchServer srv) { + super.init(srv); if (usernameFetch == null) LogHelper.error("[Verify][AuthHandler] usernameFetch cannot be null"); if (uuidFetch == null) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java index c19e9742..af84b95d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java @@ -22,6 +22,7 @@ public static void registerProviders() { providers.register("mysql", MySQLAuthProvider.class); providers.register("request", RequestAuthProvider.class); providers.register("json", JsonAuthProvider.class); + providers.register("hibernate", HibernateAuthProvider.class); registredProv = true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java new file mode 100644 index 00000000..b9ec6985 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java @@ -0,0 +1,25 @@ +package pro.gravit.launchserver.auth.provider; + +import java.io.IOException; + +import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.dao.User; +import pro.gravit.utils.helper.SecurityHelper; + +public class HibernateAuthProvider extends AuthProvider { + @Override + public AuthProviderResult auth(String login, String password, String ip) throws Exception { + User user = srv.userService.findUserByUsername(login); + if(user == null || !user.verifyPassword(password)) + { + if(user ==null) throw new AuthException("Username incorrect"); + else throw new AuthException("Username or password incorrect"); + } + return new AuthProviderResult(login, SecurityHelper.randomStringToken(), srv); + } + + @Override + public void close() throws IOException { + + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java new file mode 100644 index 00000000..e5aa13fa --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java @@ -0,0 +1,33 @@ +package pro.gravit.launchserver.command.dao; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.command.Command; +import pro.gravit.launchserver.dao.User; +import pro.gravit.utils.helper.LogHelper; + +public class GetAllUsersCommand extends Command { + public GetAllUsersCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return ""; + } + + @Override + public String getUsageDescription() { + return "get all users information"; + } + + @Override + public void invoke(String... args) throws Exception { + int count = 0; + for(User user : server.userService.findAllUsers()) + { + LogHelper.subInfo("[%s] UUID: %s", user.username, user.uuid.toString()); + count++; + } + LogHelper.info("Print %d users", count); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java new file mode 100644 index 00000000..0cdae071 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java @@ -0,0 +1,34 @@ +package pro.gravit.launchserver.command.dao; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.command.Command; +import pro.gravit.launchserver.dao.User; +import pro.gravit.utils.helper.LogHelper; + +public class GetUserCommand extends Command { + public GetUserCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[username]"; + } + + @Override + public String getUsageDescription() { + return "get user information"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + User user = server.userService.findUserByUsername(args[0]); + if(user == null) + { + LogHelper.error("User %s not found", args[0]); + return; + } + LogHelper.info("[%s] UUID: %s", user.username, user.uuid.toString()); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java new file mode 100644 index 00000000..40a43f80 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java @@ -0,0 +1,35 @@ +package pro.gravit.launchserver.command.dao; + +import java.util.UUID; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.command.Command; +import pro.gravit.launchserver.dao.User; +import pro.gravit.utils.helper.LogHelper; + +public class RegisterCommand extends Command { + public RegisterCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[login] [password]"; + } + + @Override + public String getUsageDescription() { + return "Register new user"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 2); + User user = new User(); + user.username = args[0]; + user.setPassword(args[1]); + user.uuid = UUID.randomUUID(); + server.userService.saveUser(user); + LogHelper.info("User %s registered. UUID: %s", user.username, user.uuid.toString()); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java new file mode 100644 index 00000000..e0b38d9c --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java @@ -0,0 +1,37 @@ +package pro.gravit.launchserver.command.dao; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.command.Command; +import pro.gravit.launchserver.dao.User; +import pro.gravit.utils.helper.LogHelper; + +public class SetUserPasswordCommand extends Command { + + public SetUserPasswordCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[username] [new password]"; + } + + @Override + public String getUsageDescription() { + return "Set user password"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 2); + User user = server.userService.findUserByUsername(args[0]); + if(user == null) + { + LogHelper.error("User %s not found", args[1]); + return; + } + user.setPassword(args[1]); + server.userService.updateUser(user); + LogHelper.info("[%s] UUID: %s | New Password: %s", user.username, user.uuid.toString(), args[1]); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java index 43a307ab..89a03b32 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java @@ -17,6 +17,10 @@ import pro.gravit.launchserver.command.basic.StopCommand; import pro.gravit.launchserver.command.basic.TestCommand; import pro.gravit.launchserver.command.basic.VersionCommand; +import pro.gravit.launchserver.command.dao.GetAllUsersCommand; +import pro.gravit.launchserver.command.dao.GetUserCommand; +import pro.gravit.launchserver.command.dao.RegisterCommand; +import pro.gravit.launchserver.command.dao.SetUserPasswordCommand; import pro.gravit.launchserver.command.dump.DumpEntryCacheCommand; import pro.gravit.launchserver.command.dump.DumpSessionsCommand; import pro.gravit.launchserver.command.hash.DownloadAssetCommand; @@ -82,6 +86,15 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand Category updatesCategory = new Category(updates, "updates", "Update and Sync Management"); handler.registerCategory(updatesCategory); + //Register dao commands + BaseCommandCategory dao = new BaseCommandCategory(); + dao.registerCommand("register", new RegisterCommand(server)); + dao.registerCommand("setUserPassword", new SetUserPasswordCommand(server)); + dao.registerCommand("getUser", new GetUserCommand(server)); + dao.registerCommand("getAllUsers", new GetAllUsersCommand(server)); + Category daoCategory = new Category(dao, "DAO", "Data Management"); + handler.registerCategory(daoCategory); + // Register auth commands BaseCommandCategory auth = new BaseCommandCategory(); auth.registerCommand("auth", new AuthCommand(server)); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java index f19ebe1e..966c3389 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java @@ -11,6 +11,7 @@ public static void registerComponents() { if (!registredComp) { providers.register("authLimiter", AuthLimiterComponent.class); providers.register("commandRemover", CommandRemoverComponent.class); + providers.register("hibernate", HibernateConfiguratorComponent.class); registredComp = true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java new file mode 100644 index 00000000..da4171f5 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java @@ -0,0 +1,61 @@ +package pro.gravit.launchserver.components; + +import java.nio.file.Paths; + +import org.hibernate.cfg.Configuration; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.dao.LaunchServerDaoFactory; +import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; +import pro.gravit.launchserver.hibernate.HibernateManager; +import pro.gravit.utils.helper.CommonHelper; + +public class HibernateConfiguratorComponent extends Component { + public String driver; + public String url; + public String username; + public String password; + public String pool_size; + public String hibernateConfig; + public boolean parallelHibernateInit; + @Override + public void preInit(LaunchServer launchServer) { + LaunchServerDaoFactory.setUserDaoProvider(HibernateUserDAOImpl::new); + Runnable init = () -> { + Configuration cfg = new Configuration() + .addAnnotatedClass(User.class) + .setProperty("hibernate.connection.driver_class", driver) + .setProperty("hibernate.connection.url", url) + .setProperty("hibernate.connection.username", username) + .setProperty("hibernate.connection.password", password) + .setProperty("hibernate.connection.pool_size", pool_size); + if(hibernateConfig != null) + cfg.configure(Paths.get(hibernateConfig).toFile()); + HibernateManager.sessionFactory = cfg.buildSessionFactory(); + }; + if(parallelHibernateInit) + CommonHelper.newThread("Hibernate Thread", true, init); + else + init.run(); + } + + @Override + public void init(LaunchServer launchServer) { + + } + + @Override + public void postInit(LaunchServer launchServer) { + //UserService service = new UserService(); + //List users = service.findAllUsers(); + //User newUser = new User(); + //newUser.username = "VeryTestUser"; + //newUser.setPassword("12345"); + //service.saveUser(newUser); + //for(User u : users) + //{ + // LogHelper.info("Found User %s", u.username); + //} + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java new file mode 100644 index 00000000..c1184c09 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java @@ -0,0 +1,18 @@ +package pro.gravit.launchserver.dao; + +import java.util.function.Supplier; + +import pro.gravit.launchserver.dao.impl.DefaultUserDAOImpl; + +public class LaunchServerDaoFactory { + private static Supplier getUserDao = DefaultUserDAOImpl::new; + + public static void setUserDaoProvider(Supplier getDao) { + LaunchServerDaoFactory.getUserDao = getDao; + } + + public static UserDAO createUserDao() + { + return getUserDao.get(); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java new file mode 100644 index 00000000..6a12e571 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java @@ -0,0 +1,68 @@ +package pro.gravit.launchserver.dao; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import pro.gravit.utils.helper.LogHelper; +import pro.gravit.utils.helper.SecurityHelper; + +@Entity +@Table(name = "users") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + @Column(unique = true) + public String username; + @Column(unique = true) + public UUID uuid; + @Column(name = "password") + private byte[] password; + private String accessToken; + public String serverID; + private String password_salt; + public void setPassword(String password) + { + password_salt = SecurityHelper.randomStringAESKey(); + MessageDigest digest; + try { + digest = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + LogHelper.error(e); + return; + } + this.password = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8)); + } + public boolean verifyPassword(String password) + { + MessageDigest digest; + try { + digest = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + LogHelper.error(e); + return false; + } + byte[] enpassword = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8)); + LogHelper.info(Arrays.toString(enpassword)); + LogHelper.info(Arrays.toString(this.password)); + return Arrays.equals(enpassword, this.password); + } + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java new file mode 100644 index 00000000..e400ff79 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java @@ -0,0 +1,14 @@ +package pro.gravit.launchserver.dao; + +import java.util.List; +import java.util.UUID; + +public interface UserDAO { + User findById(int id); + User findByUsername(String username); + User findByUUID(UUID uuid); + void save(User user); + void update(User user); + void delete(User user); + List findAll(); +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java new file mode 100644 index 00000000..73332786 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java @@ -0,0 +1,44 @@ +package pro.gravit.launchserver.dao; + +import java.util.List; +import java.util.UUID; + +import pro.gravit.launchserver.LaunchServer; + +public class UserService { + + private final UserDAO usersDao; + + public UserService(LaunchServer server) { + usersDao = LaunchServerDaoFactory.createUserDao(); + } + + public User findUser(int id) { + return usersDao.findById(id); + } + + public User findUserByUsername(String username) { + return usersDao.findByUsername(username); + } + public User findUserByUUID(UUID uuid) { + return usersDao.findByUUID(uuid); + } + + public void saveUser(User user) { + usersDao.save(user); + } + + public void deleteUser(User user) { + usersDao.delete(user); + } + + public void updateUser(User user) { + usersDao.update(user); + } + + public List findAllUsers() { + return usersDao.findAll(); + } + + +} \ No newline at end of file diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java new file mode 100644 index 00000000..0c9e6507 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java @@ -0,0 +1,45 @@ +package pro.gravit.launchserver.dao.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.UserDAO; + +public class DefaultUserDAOImpl implements UserDAO { + @Override + public User findById(int id) { + return null; + } + + @Override + public User findByUsername(String username) { + return null; + } + + @Override + public User findByUUID(UUID uuid) { + return null; + } + + @Override + public void save(User user) { + throw new UnsupportedOperationException(); + } + + @Override + public void update(User user) { + throw new UnsupportedOperationException(); + } + + @Override + public void delete(User user) { + throw new UnsupportedOperationException(); + } + + @Override + public List findAll() { + return new ArrayList<>(); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java new file mode 100644 index 00000000..13dd60a9 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java @@ -0,0 +1,76 @@ +package pro.gravit.launchserver.dao.impl; + +import java.util.List; +import java.util.UUID; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.UserDAO; +import pro.gravit.launchserver.hibernate.HibernateManager; + +public class HibernateUserDAOImpl implements UserDAO { + + public User findById(int id) { + return HibernateManager.sessionFactory.openSession().get(User.class, id); + } + + public User findByUsername(String username) { + EntityManager em = HibernateManager.sessionFactory.createEntityManager(); + em.getTransaction().begin(); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery personCriteria = cb.createQuery(User.class); + Root rootUser = personCriteria.from(User.class); + personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username)); + List ret = em.createQuery(personCriteria).getResultList(); + em.close(); + return ret.size() == 0 ? null : ret.get(0); + } + + public User findByUUID(UUID uuid) { + EntityManager em = HibernateManager.sessionFactory.createEntityManager(); + em.getTransaction().begin(); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery personCriteria = cb.createQuery(User.class); + Root rootUser = personCriteria.from(User.class); + personCriteria.select(rootUser).where(cb.equal(rootUser.get("uuid"), uuid)); + List ret = em.createQuery(personCriteria).getResultList(); + em.close(); + return ret.size() == 0 ? null : ret.get(0); + } + + public void save(User user) { + Session session = HibernateManager.sessionFactory.openSession(); + Transaction tx1 = session.beginTransaction(); + session.save(user); + tx1.commit(); + session.close(); + } + + public void update(User user) { + Session session = HibernateManager.sessionFactory.openSession(); + Transaction tx1 = session.beginTransaction(); + session.update(user); + tx1.commit(); + session.close(); + } + + public void delete(User user) { + Session session = HibernateManager.sessionFactory.openSession(); + Transaction tx1 = session.beginTransaction(); + session.delete(user); + tx1.commit(); + session.close(); + } + + @SuppressWarnings("unchecked") + public List findAll() { + return (List) HibernateManager.sessionFactory.openSession().createQuery("From User").list(); + } +} \ No newline at end of file diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java new file mode 100644 index 00000000..fc770f90 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java @@ -0,0 +1,7 @@ +package pro.gravit.launchserver.hibernate; + +import org.hibernate.SessionFactory; + +public class HibernateManager { + public static SessionFactory sessionFactory; +}