4ff601b4fc
(cherry picked from commit 557d67b816c91076d8cafc9aa680e62d5b012f5b)
194 lines
8.2 KiB
Dart
194 lines
8.2 KiB
Dart
import 'package:core/presentation/state/failure.dart';
|
|
import 'package:core/presentation/state/success.dart';
|
|
import 'package:core/utils/app_logger.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:jmap_dart_client/jmap/account_id.dart';
|
|
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
|
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
|
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
|
import 'package:model/extensions/session_extension.dart';
|
|
import 'package:model/oidc/token_oidc.dart';
|
|
import 'package:tmail_ui_user/features/base/base_controller.dart';
|
|
import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.dart';
|
|
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
|
|
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
|
|
import 'package:tmail_ui_user/features/home/domain/usecases/get_session_interactor.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/state/get_authenticated_account_state.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_state.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_account_interactor.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/usecases/update_authentication_account_interactor.dart';
|
|
import 'package:tmail_ui_user/features/login/presentation/login_form_type.dart';
|
|
import 'package:tmail_ui_user/features/login/presentation/model/login_arguments.dart';
|
|
import 'package:tmail_ui_user/features/manage_account/presentation/vacation/vacation_interactors_bindings.dart';
|
|
import 'package:tmail_ui_user/main/error/capability_validator.dart';
|
|
import 'package:tmail_ui_user/main/exceptions/remote_exception.dart';
|
|
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
|
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
|
|
|
abstract class ReloadableController extends BaseController {
|
|
final GetSessionInteractor _getSessionInteractor = Get.find<GetSessionInteractor>();
|
|
final GetAuthenticatedAccountInteractor _getAuthenticatedAccountInteractor = Get.find<GetAuthenticatedAccountInteractor>();
|
|
final UpdateAuthenticationAccountInteractor _updateAuthenticationAccountInteractor = Get.find<UpdateAuthenticationAccountInteractor>();
|
|
|
|
@override
|
|
void handleFailureViewState(Failure failure) {
|
|
super.handleFailureViewState(failure);
|
|
if (failure is GetCredentialFailure) {
|
|
goToLogin(arguments: LoginArguments(LoginFormType.credentialForm));
|
|
} else if (failure is GetSessionFailure) {
|
|
_handleGetSessionFailure(
|
|
sessionCacheException: failure.exception,
|
|
sessionRemoteException: failure.remoteException
|
|
);
|
|
} else if (failure is GetStoredTokenOidcFailure) {
|
|
goToLogin(arguments: LoginArguments(LoginFormType.ssoForm));
|
|
} else if (failure is GetAuthenticatedAccountFailure) {
|
|
goToLogin(arguments: LoginArguments(LoginFormType.credentialForm));
|
|
}
|
|
}
|
|
|
|
@override
|
|
void handleSuccessViewState(Success success) {
|
|
super.handleSuccessViewState(success);
|
|
if (success is GetCredentialViewState) {
|
|
_handleGetCredentialSuccess(success);
|
|
} else if (success is GetSessionSuccess) {
|
|
_handleGetSessionSuccess(success);
|
|
} else if (success is GetStoredTokenOidcSuccess) {
|
|
_handleGetStoredTokenOIDCSuccess(success);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* trigger reload by getting Credential again then setting up Interceptor and retrieving session
|
|
* */
|
|
void reload() {
|
|
getAuthenticatedAccountAction();
|
|
}
|
|
|
|
void getAuthenticatedAccountAction() {
|
|
consumeState(_getAuthenticatedAccountInteractor.execute());
|
|
}
|
|
|
|
void _setUpInterceptors(GetCredentialViewState credentialViewState) {
|
|
dynamicUrlInterceptors.setJmapUrl(credentialViewState.baseUrl.origin);
|
|
dynamicUrlInterceptors.changeBaseUrl(credentialViewState.baseUrl.origin);
|
|
authorizationInterceptors.setBasicAuthorization(
|
|
credentialViewState.userName.value,
|
|
credentialViewState.password.value,
|
|
);
|
|
authorizationIsolateInterceptors.setBasicAuthorization(
|
|
credentialViewState.userName.value,
|
|
credentialViewState.password.value,
|
|
);
|
|
}
|
|
|
|
void _handleGetCredentialSuccess(GetCredentialViewState credentialViewState) {
|
|
_setUpInterceptors(credentialViewState);
|
|
getSessionAction();
|
|
}
|
|
|
|
void getSessionAction() {
|
|
consumeState(_getSessionInteractor.execute());
|
|
}
|
|
|
|
void _handleGetSessionFailure({
|
|
dynamic sessionRemoteException,
|
|
dynamic sessionCacheException
|
|
}) {
|
|
showToastErrorMessageSessionFailure(
|
|
sessionRemoteException: sessionRemoteException,
|
|
sessionCacheException: sessionCacheException,
|
|
);
|
|
clearDataAndGoToLoginPage();
|
|
}
|
|
|
|
void showToastErrorMessageSessionFailure({
|
|
dynamic sessionRemoteException,
|
|
dynamic sessionCacheException
|
|
}) {
|
|
if (currentContext == null || currentOverlayContext == null) {
|
|
return;
|
|
}
|
|
appToast.showToastErrorMessage(
|
|
currentOverlayContext!,
|
|
getErrorMessageFromSessionFailure(
|
|
context: currentContext!,
|
|
sessionRemoteException: sessionRemoteException,
|
|
sessionCacheException: sessionCacheException,
|
|
)
|
|
);
|
|
}
|
|
|
|
String getErrorMessageFromSessionFailure({
|
|
required BuildContext context,
|
|
dynamic sessionRemoteException,
|
|
dynamic sessionCacheException,
|
|
}) {
|
|
if (sessionRemoteException is ConnectionTimeout ||
|
|
sessionRemoteException is BadGateway ||
|
|
sessionRemoteException is SocketError) {
|
|
return AppLocalizations.of(context).wrongUrlMessage;
|
|
} else if (sessionRemoteException is BadCredentialsException) {
|
|
return AppLocalizations.of(context).badCredentials;
|
|
} else if (sessionRemoteException is ConnectionError) {
|
|
return AppLocalizations.of(context).connectionError;
|
|
} else if (sessionRemoteException is UnknownError && sessionRemoteException.message != null) {
|
|
return '[${sessionRemoteException.code ?? ''}] ${sessionRemoteException.message}';
|
|
} else if (sessionCacheException is NotFoundSessionException) {
|
|
return AppLocalizations.of(context).notFoundSession;
|
|
} else {
|
|
return AppLocalizations.of(context).unknownError;
|
|
}
|
|
}
|
|
|
|
void _handleGetSessionSuccess(GetSessionSuccess success) {
|
|
final session = success.session;
|
|
final personalAccount = session.personalAccount;
|
|
final apiUrl = session.getQualifiedApiUrl(baseUrl: dynamicUrlInterceptors.jmapUrl);
|
|
if (apiUrl.isNotEmpty) {
|
|
dynamicUrlInterceptors.changeBaseUrl(apiUrl);
|
|
updateAuthenticationAccount(session, personalAccount.accountId, session.username);
|
|
handleReloaded(session);
|
|
} else {
|
|
clearDataAndGoToLoginPage();
|
|
}
|
|
}
|
|
|
|
void handleReloaded(Session session) {}
|
|
|
|
void _handleGetStoredTokenOIDCSuccess(GetStoredTokenOidcSuccess tokenOidcSuccess) {
|
|
_setUpInterceptorsOidc(tokenOidcSuccess);
|
|
getSessionAction();
|
|
}
|
|
|
|
void _setUpInterceptorsOidc(GetStoredTokenOidcSuccess tokenOidcSuccess) {
|
|
dynamicUrlInterceptors.setJmapUrl(tokenOidcSuccess.baseUrl.toString());
|
|
dynamicUrlInterceptors.changeBaseUrl(tokenOidcSuccess.baseUrl.toString());
|
|
authorizationInterceptors.setTokenAndAuthorityOidc(
|
|
newToken: tokenOidcSuccess.tokenOidc.toToken(),
|
|
newConfig: tokenOidcSuccess.oidcConfiguration);
|
|
authorizationIsolateInterceptors.setTokenAndAuthorityOidc(
|
|
newToken: tokenOidcSuccess.tokenOidc.toToken(),
|
|
newConfig: tokenOidcSuccess.oidcConfiguration);
|
|
}
|
|
|
|
void injectVacationBindings(Session? session, AccountId? accountId) {
|
|
try {
|
|
requireCapability(session!, accountId!, [CapabilityIdentifier.jmapVacationResponse]);
|
|
VacationInteractorsBindings().dependencies();
|
|
} catch(e) {
|
|
logError('ReloadableController::injectVacationBindings(): exception: $e');
|
|
}
|
|
}
|
|
|
|
void updateAuthenticationAccount(Session session, AccountId accountId, UserName userName) {
|
|
final apiUrl = session.getQualifiedApiUrl(baseUrl: dynamicUrlInterceptors.jmapUrl);
|
|
if (apiUrl.isNotEmpty) {
|
|
consumeState(_updateAuthenticationAccountInteractor.execute(accountId, apiUrl, userName));
|
|
}
|
|
}
|
|
} |