mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 09:01:08 +03:00
[FEATURE] DaoProvider
This commit is contained in:
parent
c8211ae7dd
commit
8ef61b8673
15 changed files with 51 additions and 73 deletions
|
@ -64,6 +64,7 @@
|
|||
import pro.gravit.launchserver.components.Component;
|
||||
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
|
||||
import pro.gravit.launchserver.dao.UserService;
|
||||
import pro.gravit.launchserver.dao.provider.DaoProvider;
|
||||
import pro.gravit.launchserver.manangers.LaunchServerGsonManager;
|
||||
import pro.gravit.launchserver.manangers.MirrorManager;
|
||||
import pro.gravit.launchserver.manangers.ModulesManager;
|
||||
|
@ -113,6 +114,8 @@ public static final class Config {
|
|||
|
||||
public AuthProviderPair[] auth;
|
||||
|
||||
public DaoProvider dao;
|
||||
|
||||
private transient AuthProviderPair authDefault;
|
||||
|
||||
public AuthProviderPair getAuthProviderPair(String name) {
|
||||
|
@ -425,8 +428,6 @@ public static void main(String... args) throws Throwable {
|
|||
|
||||
public final ModulesManager modulesManager;
|
||||
|
||||
public final UserService userService;
|
||||
|
||||
public final MirrorManager mirrorManager;
|
||||
|
||||
public final ReloadManager reloadManager;
|
||||
|
@ -590,7 +591,6 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException
|
|||
reconfigurableManager = new ReconfigurableManager();
|
||||
authHookManager = new AuthHookManager();
|
||||
configManager = new ConfigManager();
|
||||
userService = new UserService(this);
|
||||
GarbageManager.registerNeedGC(sessionManager);
|
||||
reloadManager.registerReloadable("launchServer", this);
|
||||
registerObject("permissionsHandler", config.permissionsHandler);
|
||||
|
|
|
@ -8,31 +8,31 @@
|
|||
public class HibernateAuthHandler extends CachedAuthHandler {
|
||||
@Override
|
||||
protected Entry fetchEntry(String username) throws IOException {
|
||||
User user = srv.userService.findUserByUsername(username);
|
||||
User user = srv.config.dao.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);
|
||||
User user = srv.config.dao.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 user = srv.config.dao.userService.findUserByUUID(uuid);
|
||||
user.setAccessToken(accessToken);
|
||||
srv.userService.updateUser(user);
|
||||
srv.config.dao.userService.updateUser(user);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
|
||||
User user = srv.userService.findUserByUUID(uuid);
|
||||
User user = srv.config.dao.userService.findUserByUUID(uuid);
|
||||
user.serverID = serverID;
|
||||
srv.userService.updateUser(user);
|
||||
srv.config.dao.userService.updateUser(user);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,17 +7,17 @@ public class HibernatePermissionsHandler extends PermissionsHandler {
|
|||
|
||||
@Override
|
||||
public ClientPermissions getPermissions(String username) {
|
||||
User user = srv.userService.findUserByUsername(username);
|
||||
User user = srv.config.dao.userService.findUserByUsername(username);
|
||||
if(user == null) return ClientPermissions.DEFAULT;
|
||||
return user.getPermissions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPermissions(String username, ClientPermissions permissions) {
|
||||
User user = srv.userService.findUserByUsername(username);
|
||||
User user = srv.config.dao.userService.findUserByUsername(username);
|
||||
if(user == null) return;
|
||||
user.setPermissions(permissions);
|
||||
srv.userService.updateUser(user);
|
||||
srv.config.dao.userService.updateUser(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
public class HibernateAuthProvider extends AuthProvider {
|
||||
@Override
|
||||
public AuthProviderResult auth(String login, String password, String ip) throws Exception {
|
||||
User user = srv.userService.findUserByUsername(login);
|
||||
User user = srv.config.dao.userService.findUserByUsername(login);
|
||||
if(user == null || !user.verifyPassword(password))
|
||||
{
|
||||
if(user ==null) throw new AuthException("Username incorrect");
|
||||
|
|
|
@ -23,7 +23,7 @@ public String getUsageDescription() {
|
|||
@Override
|
||||
public void invoke(String... args) throws Exception {
|
||||
int count = 0;
|
||||
for(User user : server.userService.findAllUsers())
|
||||
for(User user : server.config.dao.userService.findAllUsers())
|
||||
{
|
||||
LogHelper.subInfo("[%s] UUID: %s", user.username, user.uuid.toString());
|
||||
count++;
|
||||
|
|
|
@ -24,7 +24,7 @@ public String getUsageDescription() {
|
|||
@Override
|
||||
public void invoke(String... args) throws Exception {
|
||||
verifyArgs(args, 1);
|
||||
User user = server.userService.findUserByUsername(args[0]);
|
||||
User user = server.config.dao.userService.findUserByUsername(args[0]);
|
||||
if(user == null)
|
||||
{
|
||||
LogHelper.error("User %s not found", args[0]);
|
||||
|
|
|
@ -29,7 +29,7 @@ public void invoke(String... args) throws Exception {
|
|||
user.username = args[0];
|
||||
user.setPassword(args[1]);
|
||||
user.uuid = UUID.randomUUID();
|
||||
server.userService.saveUser(user);
|
||||
server.config.dao.userService.saveUser(user);
|
||||
LogHelper.info("User %s registered. UUID: %s", user.username, user.uuid.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,14 +24,14 @@ public String getUsageDescription() {
|
|||
@Override
|
||||
public void invoke(String... args) throws Exception {
|
||||
verifyArgs(args, 2);
|
||||
User user = server.userService.findUserByUsername(args[0]);
|
||||
User user = server.config.dao.userService.findUserByUsername(args[0]);
|
||||
if(user == null)
|
||||
{
|
||||
LogHelper.error("User %s not found", args[1]);
|
||||
return;
|
||||
}
|
||||
user.setPassword(args[1]);
|
||||
server.userService.updateUser(user);
|
||||
server.config.dao.userService.updateUser(user);
|
||||
LogHelper.info("[%s] UUID: %s | New Password: %s", user.username, user.uuid.toString(), args[1]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ public static void registerComponents() {
|
|||
if (!registredComp) {
|
||||
providers.register("authLimiter", AuthLimiterComponent.class);
|
||||
providers.register("commandRemover", CommandRemoverComponent.class);
|
||||
providers.register("hibernate", HibernateConfiguratorComponent.class);
|
||||
registredComp = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
package pro.gravit.launchserver.dao;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import pro.gravit.launchserver.LaunchServer;
|
||||
import pro.gravit.launchserver.dao.impl.DefaultUserDAOImpl;
|
||||
|
||||
public class LaunchServerDaoFactory {
|
||||
private static final Function<LaunchServer, UserDAO> defDao = DefaultUserDAOImpl::new;
|
||||
private static final Map<LaunchServer, Function<LaunchServer, UserDAO>> daos = new ConcurrentHashMap<>();
|
||||
|
||||
public static void setUserDaoProvider(LaunchServer srv, Function<LaunchServer, UserDAO> getDao) {
|
||||
daos.put(srv, getDao);
|
||||
}
|
||||
|
||||
public static UserDAO createUserDao(LaunchServer srv)
|
||||
{
|
||||
return daos.getOrDefault(srv, defDao).apply(srv);
|
||||
}
|
||||
}
|
|
@ -9,8 +9,8 @@ public class UserService {
|
|||
|
||||
private final UserDAO usersDao;
|
||||
|
||||
public UserService(LaunchServer server) {
|
||||
usersDao = LaunchServerDaoFactory.createUserDao(server);
|
||||
public UserService(UserDAO usersDAO) {
|
||||
this.usersDao = usersDAO;
|
||||
}
|
||||
|
||||
public User findUser(int id) {
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package pro.gravit.launchserver.dao.provider;
|
||||
|
||||
import pro.gravit.launchserver.LaunchServer;
|
||||
import pro.gravit.launchserver.dao.UserDAO;
|
||||
import pro.gravit.launchserver.dao.UserService;
|
||||
import pro.gravit.utils.ProviderMap;
|
||||
|
||||
public abstract class DaoProvider {
|
||||
public static ProviderMap<DaoProvider> providers = new ProviderMap<>("DaoProvider");
|
||||
public UserDAO userDAO;
|
||||
public UserService userService;
|
||||
public static void registerProviders()
|
||||
{
|
||||
providers.register("hibernate", HibernateDaoProvider.class);
|
||||
}
|
||||
public abstract void init(LaunchServer server);
|
||||
}
|
|
@ -1,18 +1,17 @@
|
|||
package pro.gravit.launchserver.components;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
package pro.gravit.launchserver.dao.provider;
|
||||
|
||||
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.UserHWID;
|
||||
import pro.gravit.launchserver.dao.UserService;
|
||||
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
|
||||
import pro.gravit.launchserver.hibernate.SessionFactoryManager;
|
||||
import pro.gravit.utils.helper.CommonHelper;
|
||||
|
||||
public class HibernateConfiguratorComponent extends Component {
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class HibernateDaoProvider extends DaoProvider {
|
||||
public String driver;
|
||||
public String url;
|
||||
public String username;
|
||||
|
@ -20,9 +19,11 @@ public class HibernateConfiguratorComponent extends Component {
|
|||
public String pool_size;
|
||||
public String hibernateConfig;
|
||||
public boolean parallelHibernateInit;
|
||||
|
||||
@Override
|
||||
public void preInit(LaunchServer launchServer) {
|
||||
LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new);
|
||||
public void init(LaunchServer server) {
|
||||
userDAO = new HibernateUserDAOImpl(server);
|
||||
userService = new UserService(userDAO);
|
||||
Runnable init = () -> {
|
||||
Configuration cfg = new Configuration()
|
||||
.addAnnotatedClass(User.class)
|
||||
|
@ -34,30 +35,11 @@ public void preInit(LaunchServer launchServer) {
|
|||
.setProperty("hibernate.connection.pool_size", pool_size);
|
||||
if(hibernateConfig != null)
|
||||
cfg.configure(Paths.get(hibernateConfig).toFile());
|
||||
SessionFactoryManager.forLaunchServer(launchServer).fact = cfg.buildSessionFactory();
|
||||
SessionFactoryManager.forLaunchServer(server).fact = 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<User> 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);
|
||||
//}
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@
|
|||
import pro.gravit.launchserver.auth.provider.AuthProvider;
|
||||
import pro.gravit.launchserver.auth.texture.TextureProvider;
|
||||
import pro.gravit.launchserver.components.Component;
|
||||
import pro.gravit.launchserver.dao.provider.DaoProvider;
|
||||
import pro.gravit.utils.UniversalJsonAdapter;
|
||||
|
||||
public class LaunchServerGsonManager extends GsonManager {
|
||||
|
@ -23,5 +24,6 @@ public void registerAdapters(GsonBuilder builder) {
|
|||
builder.registerTypeAdapter(HWIDHandler.class, new UniversalJsonAdapter<>(HWIDHandler.providers));
|
||||
builder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers));
|
||||
builder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers));
|
||||
builder.registerTypeAdapter(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception
|
|||
sendError("Hash invalid");
|
||||
return;
|
||||
}
|
||||
User checkUser = server.userService.findUserByUsername(login);
|
||||
User checkUser = server.config.dao.userService.findUserByUsername(login);
|
||||
if(checkUser != null)
|
||||
{
|
||||
sendError("User already register");
|
||||
|
@ -34,7 +34,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception
|
|||
user.username = login;
|
||||
user.setPassword(password);
|
||||
user.uuid = UUID.randomUUID();
|
||||
server.userService.saveUser(user);
|
||||
server.config.dao.userService.saveUser(user);
|
||||
}
|
||||
@Override
|
||||
public String getType() {
|
||||
|
|
Loading…
Reference in a new issue