From 439b3f41c1ba61f5f957ac20f793710c931aaeba Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 3 Jun 2019 12:46:04 +0700 Subject: [PATCH 1/7] =?UTF-8?q?[FEATURE][CRITICAL]=20=D0=94=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D0=B8=20Hibernate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LaunchServer/build.gradle | 1 + .../auth/provider/AuthProvider.java | 2 + .../auth/provider/HibernateAuthProvider.java | 27 +++++++++ .../launchserver/components/Component.java | 1 + .../HibernateConfiguratorComponent.java | 55 ++++++++++++++++++ .../hibernate/HibernateManager.java | 7 +++ .../gravit/launchserver/hibernate/User.java | 49 ++++++++++++++++ .../launchserver/hibernate/UserDAO.java | 12 ++++ .../launchserver/hibernate/UserDAOImpl.java | 58 +++++++++++++++++++ .../launchserver/hibernate/UserService.java | 37 ++++++++++++ 10 files changed, 249 insertions(+) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/HibernateManager.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java 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/auth/provider/AuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java index 76ee25c9..ac47ea59 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 @@ -1,6 +1,7 @@ package pro.gravit.launchserver.auth.provider; import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.hibernate.HibernateManager; import pro.gravit.utils.ProviderMap; import java.io.IOException; @@ -21,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..2fd3fbd6 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java @@ -0,0 +1,27 @@ +package pro.gravit.launchserver.auth.provider; + +import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.hibernate.User; +import pro.gravit.launchserver.hibernate.UserService; +import pro.gravit.utils.helper.SecurityHelper; + +import java.io.IOException; + +public class HibernateAuthProvider extends AuthProvider { + @Override + public AuthProviderResult auth(String login, String password, String ip) throws Exception { + UserService service = new UserService(); + User user = service.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()); + } + + @Override + public void close() throws IOException { + + } +} 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..85c8b52e --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java @@ -0,0 +1,55 @@ +package pro.gravit.launchserver.components; + +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.hibernate.HibernateManager; +import pro.gravit.launchserver.hibernate.User; +import pro.gravit.launchserver.hibernate.UserService; +import pro.gravit.utils.helper.LogHelper; + +import javax.persistence.Persistence; +import java.nio.file.Paths; +import java.util.List; + +public class HibernateConfiguratorComponent extends Component { + public String driver; + public String url; + public String username; + public String password; + public String pool_size; + public String hibernateConfig; + @Override + public void preInit(LaunchServer launchServer) { + + } + + @Override + public void init(LaunchServer launchServer) { + + } + + @Override + public void postInit(LaunchServer launchServer) { + 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(); + //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/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; +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java new file mode 100644 index 00000000..43f90d94 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java @@ -0,0 +1,49 @@ +package pro.gravit.launchserver.hibernate; + +import pro.gravit.utils.helper.LogHelper; +import pro.gravit.utils.helper.SecurityHelper; + +import javax.persistence.*; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +@Entity +@Table(name = "users") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + @Column(unique = true) + public String username; + @Column(name = "password") + private byte[] password; + 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); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java new file mode 100644 index 00000000..513b1a6d --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java @@ -0,0 +1,12 @@ +package pro.gravit.launchserver.hibernate; + +import java.util.List; + +public interface UserDAO { + User findById(int id); + User findByUsername(String username); + void save(User user); + void update(User user); + void delete(User user); + List findAll(); +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java new file mode 100644 index 00000000..68e01008 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java @@ -0,0 +1,58 @@ +package pro.gravit.launchserver.hibernate; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import java.util.List; + +public class UserDAOImpl 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)); + User ret = em.createQuery(personCriteria).getSingleResult(); + em.close(); + return ret; + } + + 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(); + } + + public List findAll() { + List users = (List) HibernateManager.sessionFactory.openSession().createQuery("From User").list(); + return users; + } +} \ No newline at end of file diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java new file mode 100644 index 00000000..fade65f8 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java @@ -0,0 +1,37 @@ +package pro.gravit.launchserver.hibernate; + +import java.util.List; + +public class UserService { + + private UserDAO usersDao = new UserDAOImpl(); + + public UserService() { + } + + public User findUser(int id) { + return usersDao.findById(id); + } + + public User findUserByUsername(String username) { + return usersDao.findByUsername(username); + } + + 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 From 868f5295d26e96e53affc7631eb12772f6ac810f Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 3 Jun 2019 13:26:20 +0700 Subject: [PATCH 2/7] =?UTF-8?q?[FEATURE]=20=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20HibernateAuthHandler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/handler/AuthHandler.java | 2 + .../auth/handler/HibernateAuthHandler.java | 48 +++++++++++++++++++ .../gravit/launchserver/hibernate/User.java | 13 +++++ .../launchserver/hibernate/UserDAO.java | 2 + .../launchserver/hibernate/UserDAOImpl.java | 17 ++++++- .../launchserver/hibernate/UserService.java | 4 ++ 6 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java 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 3ddca838..58df560d 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 @@ -2,6 +2,7 @@ import pro.gravit.launchserver.auth.provider.AuthProviderResult; import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.auth.provider.HibernateAuthProvider; import pro.gravit.utils.ProviderMap; import java.io.IOException; @@ -23,6 +24,7 @@ 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; } } 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..0b1bab5a --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java @@ -0,0 +1,48 @@ +package pro.gravit.launchserver.auth.handler; + +import pro.gravit.launchserver.hibernate.User; +import pro.gravit.launchserver.hibernate.UserService; + +import java.io.IOException; +import java.util.UUID; + +public class HibernateAuthHandler extends CachedAuthHandler { + @Override + protected Entry fetchEntry(String username) throws IOException { + UserService service = new UserService(); + User user = service.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 { + UserService service = new UserService(); + User user = service.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 { + UserService service = new UserService(); + User user = service.findUserByUUID(uuid); + user.setAccessToken(accessToken); + service.updateUser(user); + return true; + } + + @Override + protected boolean updateServerID(UUID uuid, String serverID) throws IOException { + UserService service = new UserService(); + User user = service.findUserByUUID(uuid); + user.serverID = serverID; + service.updateUser(user); + return true; + } + + @Override + public void close() throws IOException { + + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java index 43f90d94..54672c27 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java @@ -8,6 +8,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; +import java.util.UUID; @Entity @Table(name = "users") @@ -17,8 +18,12 @@ public class User { 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) { @@ -46,4 +51,12 @@ public boolean verifyPassword(String password) 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/hibernate/UserDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java index 513b1a6d..7cf563b7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java @@ -1,10 +1,12 @@ package pro.gravit.launchserver.hibernate; 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); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java index 68e01008..fb8d13d1 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java @@ -8,6 +8,7 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import java.util.List; +import java.util.UUID; public class UserDAOImpl implements UserDAO { @@ -22,9 +23,21 @@ public User findByUsername(String username) { CriteriaQuery personCriteria = cb.createQuery(User.class); Root rootUser = personCriteria.from(User.class); personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username)); - User ret = em.createQuery(personCriteria).getSingleResult(); + List ret = em.createQuery(personCriteria).getResultList(); em.close(); - return ret; + 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) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java index fade65f8..437c6319 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java @@ -1,6 +1,7 @@ package pro.gravit.launchserver.hibernate; import java.util.List; +import java.util.UUID; public class UserService { @@ -16,6 +17,9 @@ public User findUser(int 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); From aced376e5e9c52ee07f3deaee3bf31b4e5f149f6 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 3 Jun 2019 13:56:43 +0700 Subject: [PATCH 3/7] =?UTF-8?q?[FEATURE]=20=D0=9E=D0=B1=D0=BE=D0=B1=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20UserDAO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/gravit/launchserver/LaunchServer.java | 6 ++- .../auth/handler/HibernateAuthHandler.java | 18 ++++----- .../auth/provider/HibernateAuthProvider.java | 5 +-- .../HibernateConfiguratorComponent.java | 37 +++++++++++-------- .../dao/LaunchServerDaoFactory.java | 16 ++++++++ .../{hibernate => dao}/UserDAO.java | 4 +- .../{hibernate => dao}/UserService.java | 10 +++-- .../launchserver/hibernate/UserDAOImpl.java | 1 + 8 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java rename LaunchServer/src/main/java/pro/gravit/launchserver/{hibernate => dao}/UserDAO.java (77%) rename LaunchServer/src/main/java/pro/gravit/launchserver/{hibernate => dao}/UserService.java (71%) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index e618b9c7..e6d708da 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -19,6 +19,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.*; import pro.gravit.launchserver.manangers.hook.AuthHookManager; @@ -36,8 +37,6 @@ import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.serialize.signed.SignedObjectHolder; import pro.gravit.utils.helper.*; -import pro.gravit.launchserver.binary.*; -import pro.gravit.launchserver.manangers.*; import pro.gravit.utils.Version; import pro.gravit.utils.command.CommandHandler; import pro.gravit.utils.command.JLineCommandHandler; @@ -426,6 +425,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; @@ -578,6 +579,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/handler/HibernateAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java index 0b1bab5a..c249d50d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java @@ -1,7 +1,7 @@ package pro.gravit.launchserver.auth.handler; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.hibernate.User; -import pro.gravit.launchserver.hibernate.UserService; import java.io.IOException; import java.util.UUID; @@ -9,35 +9,31 @@ public class HibernateAuthHandler extends CachedAuthHandler { @Override protected Entry fetchEntry(String username) throws IOException { - UserService service = new UserService(); - User user = service.findUserByUsername(username); + User user = LaunchServer.server.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 { - UserService service = new UserService(); - User user = service.findUserByUUID(uuid); + User user = LaunchServer.server.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 { - UserService service = new UserService(); - User user = service.findUserByUUID(uuid); + User user = LaunchServer.server.userService.findUserByUUID(uuid); user.setAccessToken(accessToken); - service.updateUser(user); + LaunchServer.server.userService.updateUser(user); return true; } @Override protected boolean updateServerID(UUID uuid, String serverID) throws IOException { - UserService service = new UserService(); - User user = service.findUserByUUID(uuid); + User user = LaunchServer.server.userService.findUserByUUID(uuid); user.serverID = serverID; - service.updateUser(user); + LaunchServer.server.userService.updateUser(user); return 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 index 2fd3fbd6..d2bb7c22 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java @@ -1,8 +1,8 @@ package pro.gravit.launchserver.auth.provider; +import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.hibernate.User; -import pro.gravit.launchserver.hibernate.UserService; import pro.gravit.utils.helper.SecurityHelper; import java.io.IOException; @@ -10,8 +10,7 @@ public class HibernateAuthProvider extends AuthProvider { @Override public AuthProviderResult auth(String login, String password, String ip) throws Exception { - UserService service = new UserService(); - User user = service.findUserByUsername(login); + User user = LaunchServer.server.userService.findUserByUsername(login); if(user == null || !user.verifyPassword(password)) { if(user ==null) throw new AuthException("Username incorrect"); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java index 85c8b52e..7ecfb6fe 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java @@ -1,16 +1,14 @@ package pro.gravit.launchserver.components; -import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.dao.LaunchServerDaoFactory; import pro.gravit.launchserver.hibernate.HibernateManager; import pro.gravit.launchserver.hibernate.User; -import pro.gravit.launchserver.hibernate.UserService; -import pro.gravit.utils.helper.LogHelper; +import pro.gravit.launchserver.hibernate.UserDAOImpl; +import pro.gravit.utils.helper.CommonHelper; -import javax.persistence.Persistence; import java.nio.file.Paths; -import java.util.List; public class HibernateConfiguratorComponent extends Component { public String driver; @@ -19,9 +17,26 @@ public class HibernateConfiguratorComponent extends Component { public String password; public String pool_size; public String hibernateConfig; + public boolean parallelHibernateInit; @Override public void preInit(LaunchServer launchServer) { - + LaunchServerDaoFactory.setUserDaoProvider(UserDAOImpl::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 @@ -31,16 +46,6 @@ public void init(LaunchServer launchServer) { @Override public void postInit(LaunchServer launchServer) { - 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(); //UserService service = new UserService(); //List users = service.findAllUsers(); //User newUser = new User(); 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..80509f86 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java @@ -0,0 +1,16 @@ +package pro.gravit.launchserver.dao; + +import java.util.function.Supplier; + +public class LaunchServerDaoFactory { + private static Supplier getUserDao; + + 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/hibernate/UserDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java similarity index 77% rename from LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java rename to LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java index 7cf563b7..605d52c8 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java @@ -1,4 +1,6 @@ -package pro.gravit.launchserver.hibernate; +package pro.gravit.launchserver.dao; + +import pro.gravit.launchserver.hibernate.User; import java.util.List; import java.util.UUID; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java similarity index 71% rename from LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java rename to LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java index 437c6319..8f647502 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java @@ -1,13 +1,17 @@ -package pro.gravit.launchserver.hibernate; +package pro.gravit.launchserver.dao; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.hibernate.User; import java.util.List; import java.util.UUID; public class UserService { - private UserDAO usersDao = new UserDAOImpl(); + private final UserDAO usersDao; - public UserService() { + public UserService(LaunchServer server) { + usersDao = LaunchServerDaoFactory.createUserDao(); } public User findUser(int id) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java index fb8d13d1..95c91009 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java @@ -2,6 +2,7 @@ import org.hibernate.Session; import org.hibernate.Transaction; +import pro.gravit.launchserver.dao.UserDAO; import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; From e13c5580bc3146840df75d14cc01e8a1166dc36c Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 3 Jun 2019 14:03:17 +0700 Subject: [PATCH 4/7] =?UTF-8?q?[FEATURE]=20=D0=9E=D0=B1=D0=BE=D0=B1=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20UserDAO.=20=D0=9A=D0=BE=D0=BC=D0=B0?= =?UTF-8?q?=D0=BD=D0=B4=D0=B0=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B8=20=D1=81=D1=82=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D0=B0=D1=80=D1=82=D0=BD=D1=8B=D0=B9=20UserDAO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/handler/HibernateAuthHandler.java | 2 +- .../auth/provider/HibernateAuthProvider.java | 2 +- .../command/dao/RegisterCommand.java | 33 ++++++++++++++ .../HibernateConfiguratorComponent.java | 6 +-- .../dao/LaunchServerDaoFactory.java | 4 +- .../launchserver/{hibernate => dao}/User.java | 2 +- .../pro/gravit/launchserver/dao/UserDAO.java | 2 - .../gravit/launchserver/dao/UserService.java | 1 - .../dao/impl/DefaultUserDAOImpl.java | 45 +++++++++++++++++++ .../impl/HibernateUserDAOImpl.java} | 6 ++- 10 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java rename LaunchServer/src/main/java/pro/gravit/launchserver/{hibernate => dao}/User.java (97%) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java rename LaunchServer/src/main/java/pro/gravit/launchserver/{hibernate/UserDAOImpl.java => dao/impl/HibernateUserDAOImpl.java} (92%) 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 index c249d50d..c746232f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java @@ -1,7 +1,7 @@ package pro.gravit.launchserver.auth.handler; import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.hibernate.User; +import pro.gravit.launchserver.dao.User; import java.io.IOException; import java.util.UUID; 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 index d2bb7c22..cb4b6ab7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java @@ -2,7 +2,7 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.auth.AuthException; -import pro.gravit.launchserver.hibernate.User; +import pro.gravit.launchserver.dao.User; import pro.gravit.utils.helper.SecurityHelper; import java.io.IOException; 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..83e0f5b3 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.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 java.util.UUID; + +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(); + LaunchServer.server.userService.saveUser(user); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java index 7ecfb6fe..e3e03507 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java @@ -4,8 +4,8 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.dao.LaunchServerDaoFactory; import pro.gravit.launchserver.hibernate.HibernateManager; -import pro.gravit.launchserver.hibernate.User; -import pro.gravit.launchserver.hibernate.UserDAOImpl; +import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; import pro.gravit.utils.helper.CommonHelper; import java.nio.file.Paths; @@ -20,7 +20,7 @@ public class HibernateConfiguratorComponent extends Component { public boolean parallelHibernateInit; @Override public void preInit(LaunchServer launchServer) { - LaunchServerDaoFactory.setUserDaoProvider(UserDAOImpl::new); + LaunchServerDaoFactory.setUserDaoProvider(HibernateUserDAOImpl::new); Runnable init = () -> { Configuration cfg = new Configuration() .addAnnotatedClass(User.class) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java index 80509f86..86e69ab7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java @@ -1,9 +1,11 @@ package pro.gravit.launchserver.dao; +import pro.gravit.launchserver.dao.impl.DefaultUserDAOImpl; + import java.util.function.Supplier; public class LaunchServerDaoFactory { - private static Supplier getUserDao; + private static Supplier getUserDao = DefaultUserDAOImpl::new; public static void setUserDaoProvider(Supplier getDao) { LaunchServerDaoFactory.getUserDao = getDao; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java similarity index 97% rename from LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java rename to LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java index 54672c27..43c7c79e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java @@ -1,4 +1,4 @@ -package pro.gravit.launchserver.hibernate; +package pro.gravit.launchserver.dao; import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.SecurityHelper; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java index 605d52c8..e400ff79 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java @@ -1,7 +1,5 @@ package pro.gravit.launchserver.dao; -import pro.gravit.launchserver.hibernate.User; - import java.util.List; import java.util.UUID; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java index 8f647502..643a55d3 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java @@ -1,7 +1,6 @@ package pro.gravit.launchserver.dao; import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.hibernate.User; import java.util.List; import java.util.UUID; 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..54c34c4c --- /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 pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.UserDAO; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +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/hibernate/UserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java similarity index 92% rename from LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java rename to LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java index 95c91009..4c385026 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java @@ -1,8 +1,10 @@ -package pro.gravit.launchserver.hibernate; +package pro.gravit.launchserver.dao.impl; 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; import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; @@ -11,7 +13,7 @@ import java.util.List; import java.util.UUID; -public class UserDAOImpl implements UserDAO { +public class HibernateUserDAOImpl implements UserDAO { public User findById(int id) { return HibernateManager.sessionFactory.openSession().get(User.class, id); From 213e19763a098e08703a2ec7d6d138f3fc76e87c Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 3 Jun 2019 14:09:36 +0700 Subject: [PATCH 5/7] =?UTF-8?q?[FEATURE]=20=D0=9A=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D0=B0=20GetUser=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=BC=D0=BE=D1=82=D1=80=D0=B0=20=D0=B8=D0=BD=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0=D0=B8=D1=86=D0=B8=20=D0=BE=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/dao/GetUserCommand.java | 34 +++++++++++++++++++ .../command/handler/CommandHandler.java | 9 +++++ 2 files changed, 43 insertions(+) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java 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..0a476411 --- /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[1]); + return; + } + LogHelper.info("[%s] UUID: %s", user.username, user.uuid.toString()); + } +} 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 47f6e0b2..a348db88 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 @@ -2,6 +2,8 @@ import pro.gravit.launchserver.command.auth.*; import pro.gravit.launchserver.command.basic.*; +import pro.gravit.launchserver.command.dao.GetUserCommand; +import pro.gravit.launchserver.command.dao.RegisterCommand; import pro.gravit.launchserver.command.hash.*; import pro.gravit.launchserver.command.install.CheckInstallCommand; import pro.gravit.launchserver.command.install.MultiCommand; @@ -57,6 +59,13 @@ 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("getUser", new GetUserCommand(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)); From 952279b1f48e2eed2caecab8e7d3ea591290f42a Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 3 Jun 2019 14:14:36 +0700 Subject: [PATCH 6/7] =?UTF-8?q?[FEATURE]=20=D0=9A=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D0=B0=20GetAllUsers=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4?= =?UTF-8?q?=D1=8F=D1=89=D0=B0=D1=8F=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BE=20=D0=B2=D1=81=D0=B5=D1=85=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/dao/GetAllUsersCommand.java | 33 +++++++++++++++++++ .../command/handler/CommandHandler.java | 2 ++ 2 files changed, 35 insertions(+) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java 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/handler/CommandHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java index a348db88..bf1f76bf 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 @@ -2,6 +2,7 @@ import pro.gravit.launchserver.command.auth.*; import pro.gravit.launchserver.command.basic.*; +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.hash.*; @@ -63,6 +64,7 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand BaseCommandCategory dao = new BaseCommandCategory(); dao.registerCommand("register", new RegisterCommand(server)); dao.registerCommand("getUser", new GetUserCommand(server)); + dao.registerCommand("getAllUsers", new GetAllUsersCommand(server)); Category daoCategory = new Category(dao, "DAO", "Data Management"); handler.registerCategory(daoCategory); From 248bead428c626031ee6af37ff9de986d5536d68 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 3 Jun 2019 14:21:09 +0700 Subject: [PATCH 7/7] =?UTF-8?q?[FEATURE]=20=D0=9A=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D0=B0=20GetUserPassword=20=D0=BC=D0=B5=D0=BD=D1=8F=D1=8E?= =?UTF-8?q?=D1=89=D0=B0=D1=8F=20=D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D1=8C=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/dao/GetUserCommand.java | 2 +- .../command/dao/RegisterCommand.java | 2 + .../command/dao/SetUserPasswordCommand.java | 37 +++++++++++++++++++ .../command/handler/CommandHandler.java | 2 + 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java 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 index 0a476411..0cdae071 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java @@ -26,7 +26,7 @@ public void invoke(String... args) throws Exception { User user = server.userService.findUserByUsername(args[0]); if(user == null) { - LogHelper.error("User %s not found", args[1]); + 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 index 83e0f5b3..61585116 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java @@ -3,6 +3,7 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.command.Command; import pro.gravit.launchserver.dao.User; +import pro.gravit.utils.helper.LogHelper; import java.util.UUID; @@ -29,5 +30,6 @@ public void invoke(String... args) throws Exception { user.setPassword(args[1]); user.uuid = UUID.randomUUID(); LaunchServer.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 bf1f76bf..ca7e4453 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 @@ -5,6 +5,7 @@ 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.hash.*; import pro.gravit.launchserver.command.install.CheckInstallCommand; import pro.gravit.launchserver.command.install.MultiCommand; @@ -63,6 +64,7 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand //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");