[FEATURE] Обобщение UserDAO

This commit is contained in:
Gravit 2019-06-03 13:56:43 +07:00
parent 868f5295d2
commit aced376e5e
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
8 changed files with 61 additions and 36 deletions

View file

@ -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);

View file

@ -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;
}

View file

@ -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");

View file

@ -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<User> users = service.findAllUsers();
//User newUser = new User();

View file

@ -0,0 +1,16 @@
package pro.gravit.launchserver.dao;
import java.util.function.Supplier;
public class LaunchServerDaoFactory {
private static Supplier<UserDAO> getUserDao;
public static void setUserDaoProvider(Supplier<UserDAO> getDao) {
LaunchServerDaoFactory.getUserDao = getDao;
}
public static UserDAO createUserDao()
{
return getUserDao.get();
}
}

View file

@ -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;

View file

@ -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) {

View file

@ -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;