Skip to content

Commit

Permalink
Merge pull request #44 from bivashy/42-suggestion-configuration-migrator
Browse files Browse the repository at this point in the history
42 suggestion configuration migrator
  • Loading branch information
bivashy authored May 31, 2023
2 parents e551ff9 + dc690a7 commit 257a398
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface PluginConfig {

boolean isNameCaseCheckEnabled();

boolean isAutoMigrateConfigEnabled();

CryptoProvider getActiveHashType();

DatabaseConnectionProvider getStorageType();
Expand Down
8 changes: 8 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<minimessage.version>4.13.0</minimessage.version>
<vk.api.version>0.4.2</vk.api.version>
<password4j.version>1.7.0</password4j.version>
<yaml-config-updater.version>1.4.2</yaml-config-updater.version>
</properties>

<repositories>
Expand Down Expand Up @@ -136,5 +137,12 @@
<artifactId>password4j</artifactId>
<version>${password4j.version}</version>
</dependency>

<!-- Yaml Config Updater -->
<dependency>
<groupId>ru.vyarus</groupId>
<artifactId>yaml-config-updater</artifactId>
<version>${yaml-config-updater.version}</version>
</dependency>
</dependencies>
</project>
24 changes: 24 additions & 0 deletions core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package me.mastercapexd.auth;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.AuthPluginProvider;
import com.bivashy.auth.api.account.AccountFactory;
import com.bivashy.auth.api.asset.resource.Resource;
import com.bivashy.auth.api.asset.resource.impl.FolderResource;
import com.bivashy.auth.api.asset.resource.impl.FolderResourceReader;
import com.bivashy.auth.api.bucket.AuthenticatingAccountBucket;
import com.bivashy.auth.api.bucket.AuthenticationStepContextFactoryBucket;
import com.bivashy.auth.api.bucket.AuthenticationStepFactoryBucket;
Expand Down Expand Up @@ -75,6 +80,7 @@
import me.mastercapexd.auth.util.HashUtils;
import me.mastercapexd.auth.util.TimeUtils;
import net.kyori.adventure.platform.AudienceProvider;
import ru.vyarus.yaml.updater.YamlUpdater;

public class BaseAuthPlugin implements AuthPlugin {
private final ConfigurationProcessor configurationProcessor = new SpongeConfigurateProcessor();
Expand Down Expand Up @@ -118,6 +124,13 @@ private void initializeBasic() {
this.registerCryptoProviders();
this.registerConfigurationProcessor();
this.config = new BasePluginConfig(this);
if (config.isAutoMigrateConfigEnabled()) {
try {
this.migrateConfig();
} catch(IOException | URISyntaxException e) {
e.printStackTrace();
}
}

this.authenticationStepContextFactoryBucket = new BaseAuthenticationStepContextFactoryBucket(config.getAuthenticationSteps());
DatabaseHelper databaseHelper = new DatabaseHelper(this);
Expand Down Expand Up @@ -167,6 +180,17 @@ private void initializeTelegram() {
new TelegramCommandRegistry();
}

private void migrateConfig() throws IOException, URISyntaxException {
FolderResource folderResource = new FolderResourceReader(getClass().getClassLoader(), "configurations").read();
for (Resource resource : folderResource.getResources()) {
String realConfigurationName = resource.getName().substring(folderResource.getName().length() + 1);
File resourceConfiguration = new File(getFolder(), realConfigurationName);
if (!resourceConfiguration.exists())
continue;
YamlUpdater.create(resourceConfiguration, resource.getStream()).backup(true).update();
}
}

private void registerConfigurationProcessor() {
configurationProcessor.registerFieldResolver(ConfigurationServer.class, (context) -> new BaseConfigurationServer(context.getString()))
.registerFieldResolver(ConfigurationDuration.class, (context) -> new ConfigurationDuration(TimeUtils.parseDuration(context.getString("1s"))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public abstract class PluginConfigTemplate implements PluginConfig {
protected final AuthPlugin plugin;
private final List<Pattern> allowedPatternCommands;
protected ConfigurationSectionHolder configurationRoot;
@ConfigField("auto-migrate-config")
private boolean autoMigrateConfig;
@ConfigField("id-type")
private IdentifierType activeIdentifierType = IdentifierType.NAME;
@ConfigField("check-name-case")
Expand Down Expand Up @@ -144,6 +146,11 @@ public boolean isNameCaseCheckEnabled() {
return nameCaseCheckEnabled;
}

@Override
public boolean isAutoMigrateConfigEnabled() {
return autoMigrateConfig;
}

@Override
public boolean isPasswordConfirmationEnabled() {
return passwordConfirmationEnabled;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package me.mastercapexd.auth.server.commands;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.asset.resource.Resource;
import com.bivashy.auth.api.asset.resource.impl.FolderResource;
import com.bivashy.auth.api.asset.resource.impl.FolderResourceReader;
import com.bivashy.auth.api.config.PluginConfig;
import com.bivashy.auth.api.config.message.MessageContext;
import com.bivashy.auth.api.crypto.HashInput;
Expand All @@ -17,6 +24,7 @@
import revxrsal.commands.annotation.DefaultFor;
import revxrsal.commands.annotation.Dependency;
import revxrsal.commands.annotation.Subcommand;
import ru.vyarus.yaml.updater.YamlUpdater;

@Command({"authadmin", "adminauth", "auth"})
@Permission("auth.admin")
Expand Down Expand Up @@ -86,4 +94,17 @@ public void reload(ServerCommandActor actor) {
plugin.getConfig().reload();
actor.reply(config.getServerMessages().getMessage("auth-reloaded"));
}

@Subcommand("migrateconfig")
public void migrateConfig(ServerCommandActor actor) throws IOException, URISyntaxException {
FolderResource folderResource = new FolderResourceReader(plugin.getClass().getClassLoader(), "configurations").read();
for (Resource resource : folderResource.getResources()) {
String realConfigurationName = resource.getName().substring(folderResource.getName().length() + 1);
File resourceConfiguration = new File(plugin.getFolder(), realConfigurationName);
if (!resourceConfiguration.exists())
continue;
YamlUpdater.create(resourceConfiguration, resource.getStream()).backup(true).update();
}
actor.reply(config.getServerMessages().getMessage("config-migrated"));
}
}
4 changes: 4 additions & 0 deletions core/src/main/resources/configurations/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Автоматическая миграция конфигурации. Плагин мигрриует все конфигурации при включении.
# При миграции плагин создает бекап.
auto-migrate-config: false
# Как должен хранить данные плагин
# Возможные варианты: MYSQL, SQLITE, POSTGRESQL, MARIADB
storage-type: SQLITE
Expand Down Expand Up @@ -343,6 +346,7 @@ messages:
auth-change-success: '&aУспешно изменен пароль игрока'
auth-delete-success: '&aАккаунт успешно &cудалён'
auth-reloaded: '&aПлагин успешно перезагрузил конфигурацию'
config-migrated: '&aКонфигурация успешно мигрирована'
# Ошибки в командах
player-offline: '&cИгрок с ником %account_name% оффлайн!'

Expand Down

0 comments on commit 257a398

Please sign in to comment.