From fa6f74e884b7393447040063447ec6954d707388 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Mon, 23 May 2022 17:21:34 +0300 Subject: [PATCH] Finish recovery flow cubit --- assets/translations/en.json | 3 +- assets/translations/ru.json | 4 +- lib/logic/api_maps/server.dart | 6 ++- .../server_installation_cubit.dart | 38 +++++++++++++++---- .../server_installation_repository.dart | 30 ++++++++++++++- .../server_installation_state.dart | 14 +++++++ 6 files changed, 82 insertions(+), 13 deletions(-) diff --git a/assets/translations/en.json b/assets/translations/en.json index 58b844a8..7564e19d 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -311,7 +311,8 @@ "confirm_server_decline": "Choose a different server", "choose_server": "Choose your server", "choose_server_description": "We couldn't figure out which server your are trying to connect to.", - "no_servers": "There is no available servers on your account." + "no_servers": "There is no available servers on your account.", + "domain_not_available_on_token": "Selected domain is not available on this token." }, "modals": { diff --git a/assets/translations/ru.json b/assets/translations/ru.json index c2fc34bc..d3393394 100644 --- a/assets/translations/ru.json +++ b/assets/translations/ru.json @@ -313,7 +313,9 @@ "confirm_server_decline": "Выбрать другой сервер", "choose_server": "Выберите сервер", "choose_server_description": "Не удалось определить, с каким сервером вы устанавливаете связь.", - "no_servers": "На вашем аккаунте нет доступных серверов." + "no_servers": "На вашем аккаунте нет доступных серверов.", + "domain_not_available_on_token": "Введённый токен не имеет доступа к нужному домену." + }, "modals": { "_comment": "messages in modals", diff --git a/lib/logic/api_maps/server.dart b/lib/logic/api_maps/server.dart index 92bd6932..7a6fefb5 100644 --- a/lib/logic/api_maps/server.dart +++ b/lib/logic/api_maps/server.dart @@ -153,13 +153,15 @@ class ServerApi extends ApiMap { ); } - Future>> getUsersList() async { + Future>> getUsersList( + {withMainUser = false} + ) async { List res = []; Response response; var client = await getClient(); try { - response = await client.get('/users'); + response = await client.get('/users', queryParameters: withMainUser ? {'withMainUser': 'true'} : null); for (var user in response.data) { res.add(user.toString()); } diff --git a/lib/logic/cubit/server_installation/server_installation_cubit.dart b/lib/logic/cubit/server_installation/server_installation_cubit.dart index c2849903..13b84610 100644 --- a/lib/logic/cubit/server_installation/server_installation_cubit.dart +++ b/lib/logic/cubit/server_installation/server_installation_cubit.dart @@ -75,6 +75,15 @@ class ServerInstallationCubit extends Cubit { applicationKey: applicationKey, ); await repository.saveBackblazeKey(backblazeCredential); + if (state is ServerInstallationRecovery) { + final mainUser = await repository.getMainUser(); + final updatedState = (state as ServerInstallationRecovery).copyWith( + backblazeCredential: backblazeCredential, + rootUser: mainUser, + ); + emit(updatedState.finish()); + return; + } emit((state as ServerInstallationNotFinished) .copyWith(backblazeCredential: backblazeCredential)); } @@ -410,14 +419,27 @@ class ServerInstallationCubit extends Cubit { )); } - // Future setAndValidateCloudflareToken(String token) async { - // final dataState = this.state as ServerInstallationRecovery; - // final serverDomain = dataState.serverDomain; - // if (serverDomain == null) { - // return; - // } - // final domainId = await repository.getDomainId(serverDomain.domainName); - // } + Future setAndValidateCloudflareToken(String token) async { + final dataState = this.state as ServerInstallationRecovery; + final serverDomain = dataState.serverDomain; + if (serverDomain == null) { + return; + } + final zoneId = await repository.getDomainId(token, serverDomain.domainName); + if (zoneId == null) { + getIt() + .showSnackBar('recovering.domain_not_available_on_token'.tr()); + return; + } + emit(dataState.copyWith( + serverDomain: ServerDomain( + domainName: serverDomain.domainName, + zoneId: zoneId, + provider: DnsProvider.Cloudflare, + ), + currentStep: RecoveryStep.BackblazeToken, + )); + } @override void onChange(Change change) { diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index af006c6b..bbeb5700 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -346,7 +346,7 @@ class ServerInstallationRepository { } try { final parsedVersion = Version.parse(serverApiVersion); - if (parsedVersion.major == 1 && parsedVersion.minor < 2) { + if (!VersionConstraint.parse('>=1.2.0').allows(parsedVersion)) { return ServerRecoveryCapabilities.legacy; } return ServerRecoveryCapabilities.loginTokens; @@ -485,6 +485,34 @@ class ServerInstallationRepository { ); } + Future getMainUser() async { + var serverApi = ServerApi(); + final fallbackUser = User( + isFoundOnServer: false, + note: 'Couldn\'t find main user on server, API is outdated', + login: 'UNKNOWN', + sshKeys: [], + ); + + final serverApiVersion = await serverApi.getApiVersion(); + final users = await serverApi.getUsersList(withMainUser: true); + if (serverApiVersion == null || !users.isSuccess) { + return fallbackUser; + } + try { + final parsedVersion = Version.parse(serverApiVersion); + if (!VersionConstraint.parse('>=1.2.5').allows(parsedVersion)) { + return fallbackUser; + } + return User( + isFoundOnServer: true, + login: users.data[0], + ); + } on FormatException { + return fallbackUser; + } + } + Future> getServersOnHetznerAccount() async { var hetznerApi = HetznerApi(); final servers = await hetznerApi.getServers(); diff --git a/lib/logic/cubit/server_installation/server_installation_state.dart b/lib/logic/cubit/server_installation/server_installation_state.dart index b376914d..605a26ff 100644 --- a/lib/logic/cubit/server_installation/server_installation_state.dart +++ b/lib/logic/cubit/server_installation/server_installation_state.dart @@ -336,4 +336,18 @@ class ServerInstallationRecovery extends ServerInstallationState { currentStep: currentStep ?? this.currentStep, recoveryCapabilities: recoveryCapabilities ?? this.recoveryCapabilities, ); + + ServerInstallationFinished finish() { + return ServerInstallationFinished( + hetznerKey: hetznerKey!, + cloudFlareKey: cloudFlareKey!, + backblazeCredential: backblazeCredential!, + serverDomain: serverDomain!, + rootUser: rootUser!, + serverDetails: serverDetails!, + isServerStarted: true, + isServerResetedFirstTime: true, + isServerResetedSecondTime: true, + ); + } }