2022-03-03 17:38:30 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-05-25 21:53:54 +00:00
|
|
|
import 'package:equatable/equatable.dart';
|
2021-06-08 18:52:44 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-02-09 15:01:05 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2024-01-31 05:14:23 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2024-02-20 16:33:24 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/server_job.dart';
|
2022-10-04 18:58:25 +00:00
|
|
|
import 'package:selfprivacy/logic/models/service.dart';
|
2021-08-18 09:36:40 +00:00
|
|
|
import 'package:selfprivacy/utils/password_generator.dart';
|
2021-05-25 21:53:54 +00:00
|
|
|
|
2021-06-08 18:52:44 +00:00
|
|
|
@immutable
|
2022-10-06 19:15:25 +00:00
|
|
|
abstract class ClientJob extends Equatable {
|
2022-08-24 05:35:49 +00:00
|
|
|
ClientJob({
|
2021-05-25 21:53:54 +00:00
|
|
|
required this.title,
|
2022-06-05 19:36:32 +00:00
|
|
|
final String? id,
|
2024-02-20 16:33:24 +00:00
|
|
|
this.requiresRebuild = true,
|
|
|
|
this.status = JobStatusEnum.created,
|
|
|
|
this.message,
|
2021-08-18 09:36:40 +00:00
|
|
|
}) : id = id ?? StringGenerators.simpleId();
|
2021-05-25 21:53:54 +00:00
|
|
|
|
|
|
|
final String title;
|
|
|
|
final String id;
|
2024-02-20 16:33:24 +00:00
|
|
|
final bool requiresRebuild;
|
|
|
|
|
|
|
|
final JobStatusEnum status;
|
|
|
|
final String? message;
|
2021-05-25 21:53:54 +00:00
|
|
|
|
2022-10-07 17:50:18 +00:00
|
|
|
bool canAddTo(final List<ClientJob> jobs) => true;
|
2024-02-20 17:04:39 +00:00
|
|
|
Future<(bool, String)> execute();
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2021-05-25 21:53:54 +00:00
|
|
|
@override
|
2024-02-20 16:33:24 +00:00
|
|
|
List<Object> get props => [id, title, status];
|
|
|
|
|
|
|
|
ClientJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
});
|
2021-05-25 21:53:54 +00:00
|
|
|
}
|
2021-06-08 18:52:44 +00:00
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
class UpgradeServerJob extends ClientJob {
|
|
|
|
UpgradeServerJob({
|
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
|
|
|
}) : super(title: 'jobs.start_server_upgrade'.tr());
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool canAddTo(final List<ClientJob> jobs) =>
|
|
|
|
!jobs.any((final job) => job is UpgradeServerJob);
|
|
|
|
|
|
|
|
@override
|
2024-02-20 17:09:14 +00:00
|
|
|
Future<(bool, String)> execute() async => (false, 'unimplemented');
|
2024-02-20 16:33:24 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
UpgradeServerJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
UpgradeServerJob(
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RebootServerJob extends ClientJob {
|
|
|
|
RebootServerJob({
|
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
|
|
|
}) : super(title: 'jobs.reboot_server'.tr(), requiresRebuild: false);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool canAddTo(final List<ClientJob> jobs) =>
|
|
|
|
!jobs.any((final job) => job is RebootServerJob);
|
|
|
|
|
|
|
|
@override
|
2024-02-20 17:09:14 +00:00
|
|
|
Future<(bool, String)> execute() async => (false, 'unimplemented');
|
2024-02-20 16:33:24 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
RebootServerJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
RebootServerJob(
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
class CreateUserJob extends ClientJob {
|
2021-06-08 18:52:44 +00:00
|
|
|
CreateUserJob({
|
|
|
|
required this.user,
|
2024-02-20 16:33:24 +00:00
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
2022-10-03 23:32:35 +00:00
|
|
|
}) : super(title: '${"jobs.create_user".tr()} ${user.login}');
|
2021-06-08 18:52:44 +00:00
|
|
|
|
|
|
|
final User user;
|
|
|
|
|
2022-10-06 19:15:25 +00:00
|
|
|
@override
|
2024-02-20 17:04:39 +00:00
|
|
|
Future<(bool, String)> execute() async =>
|
2024-02-20 16:33:24 +00:00
|
|
|
getIt<ApiConnectionRepository>().createUser(user);
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2021-06-08 18:52:44 +00:00
|
|
|
@override
|
2024-02-20 16:33:24 +00:00
|
|
|
List<Object> get props => [...super.props, user];
|
|
|
|
|
|
|
|
@override
|
|
|
|
CreateUserJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
CreateUserJob(
|
|
|
|
user: user,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
2021-06-08 18:52:44 +00:00
|
|
|
}
|
2021-08-18 10:44:46 +00:00
|
|
|
|
2022-09-18 20:25:26 +00:00
|
|
|
class ResetUserPasswordJob extends ClientJob {
|
|
|
|
ResetUserPasswordJob({
|
|
|
|
required this.user,
|
2024-02-20 16:33:24 +00:00
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
2022-10-03 23:32:35 +00:00
|
|
|
}) : super(title: '${"jobs.reset_user_password".tr()} ${user.login}');
|
2022-09-18 20:25:26 +00:00
|
|
|
|
|
|
|
final User user;
|
|
|
|
|
2022-10-06 19:15:25 +00:00
|
|
|
@override
|
2024-02-20 17:04:39 +00:00
|
|
|
Future<(bool, String)> execute() async =>
|
2024-02-20 16:33:24 +00:00
|
|
|
getIt<ApiConnectionRepository>().changeUserPassword(user, user.password!);
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2022-09-18 20:25:26 +00:00
|
|
|
@override
|
2024-02-20 16:33:24 +00:00
|
|
|
List<Object> get props => [...super.props, user];
|
|
|
|
|
|
|
|
@override
|
|
|
|
ResetUserPasswordJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
ResetUserPasswordJob(
|
|
|
|
user: user,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
2022-09-18 20:25:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
class DeleteUserJob extends ClientJob {
|
2021-12-20 15:25:31 +00:00
|
|
|
DeleteUserJob({
|
|
|
|
required this.user,
|
2024-02-20 16:33:24 +00:00
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
2022-10-03 23:32:35 +00:00
|
|
|
}) : super(title: '${"jobs.delete_user".tr()} ${user.login}');
|
2021-12-20 15:25:31 +00:00
|
|
|
|
|
|
|
final User user;
|
|
|
|
|
2022-10-07 17:50:18 +00:00
|
|
|
@override
|
2022-10-08 16:17:58 +00:00
|
|
|
bool canAddTo(final List<ClientJob> jobs) => !jobs.any(
|
|
|
|
(final job) => job is DeleteUserJob && job.user.login == user.login,
|
|
|
|
);
|
2022-10-07 17:50:18 +00:00
|
|
|
|
2022-10-06 19:15:25 +00:00
|
|
|
@override
|
2024-02-20 17:04:39 +00:00
|
|
|
Future<(bool, String)> execute() async =>
|
2024-02-20 16:33:24 +00:00
|
|
|
getIt<ApiConnectionRepository>().deleteUser(user);
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2021-12-20 15:25:31 +00:00
|
|
|
@override
|
2024-02-20 16:33:24 +00:00
|
|
|
List<Object> get props => [...super.props, user];
|
|
|
|
|
|
|
|
@override
|
|
|
|
DeleteUserJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
DeleteUserJob(
|
|
|
|
user: user,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
2021-12-20 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 17:50:18 +00:00
|
|
|
class ServiceToggleJob extends ClientJob {
|
2022-01-25 17:00:47 +00:00
|
|
|
ServiceToggleJob({
|
2022-10-07 17:50:18 +00:00
|
|
|
required this.service,
|
2021-08-18 10:44:46 +00:00
|
|
|
required this.needToTurnOn,
|
2024-02-20 16:33:24 +00:00
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
2021-08-29 15:02:51 +00:00
|
|
|
}) : super(
|
2022-01-25 17:00:47 +00:00
|
|
|
title:
|
2022-10-04 18:58:25 +00:00
|
|
|
'${needToTurnOn ? "jobs.service_turn_on".tr() : "jobs.service_turn_off".tr()} ${service.displayName}',
|
2022-01-25 17:00:47 +00:00
|
|
|
);
|
2021-08-18 10:44:46 +00:00
|
|
|
|
2022-10-07 17:50:18 +00:00
|
|
|
final bool needToTurnOn;
|
|
|
|
final Service service;
|
|
|
|
|
|
|
|
@override
|
2022-10-08 16:17:58 +00:00
|
|
|
bool canAddTo(final List<ClientJob> jobs) => !jobs.any(
|
|
|
|
(final job) => job is ServiceToggleJob && job.service.id == service.id,
|
|
|
|
);
|
2022-10-07 17:50:18 +00:00
|
|
|
|
2022-10-06 19:15:25 +00:00
|
|
|
@override
|
2024-02-20 17:04:39 +00:00
|
|
|
Future<(bool, String)> execute() async {
|
2024-02-21 02:00:45 +00:00
|
|
|
final result = await getIt<ApiConnectionRepository>()
|
2024-02-20 17:09:14 +00:00
|
|
|
.api
|
|
|
|
.switchService(service.id, needToTurnOn);
|
2024-02-21 02:00:45 +00:00
|
|
|
return (result.success, result.message ?? 'jobs.generic_error'.tr());
|
2022-10-06 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 17:50:18 +00:00
|
|
|
@override
|
|
|
|
List<Object> get props => [...super.props, service];
|
2024-02-20 16:33:24 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
ServiceToggleJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
ServiceToggleJob(
|
|
|
|
service: service,
|
|
|
|
needToTurnOn: needToTurnOn,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
2021-08-18 10:44:46 +00:00
|
|
|
}
|
2021-09-02 19:32:07 +00:00
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
class CreateSSHKeyJob extends ClientJob {
|
2022-03-03 17:38:30 +00:00
|
|
|
CreateSSHKeyJob({
|
|
|
|
required this.user,
|
|
|
|
required this.publicKey,
|
2024-02-20 16:33:24 +00:00
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
2022-05-24 18:55:39 +00:00
|
|
|
}) : super(title: 'jobs.create_ssh_key'.tr(args: [user.login]));
|
2022-03-03 17:38:30 +00:00
|
|
|
|
|
|
|
final User user;
|
|
|
|
final String publicKey;
|
2021-09-02 19:32:07 +00:00
|
|
|
|
2022-10-06 19:15:25 +00:00
|
|
|
@override
|
2024-02-20 17:04:39 +00:00
|
|
|
Future<(bool, String)> execute() async =>
|
2024-02-20 16:33:24 +00:00
|
|
|
getIt<ApiConnectionRepository>().addSshKey(user, publicKey);
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2021-09-02 19:32:07 +00:00
|
|
|
@override
|
2024-02-20 16:33:24 +00:00
|
|
|
List<Object> get props => [...super.props, user, publicKey];
|
|
|
|
|
|
|
|
@override
|
|
|
|
CreateSSHKeyJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
CreateSSHKeyJob(
|
|
|
|
user: user,
|
|
|
|
publicKey: publicKey,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
class DeleteSSHKeyJob extends ClientJob {
|
2022-03-03 17:38:30 +00:00
|
|
|
DeleteSSHKeyJob({
|
|
|
|
required this.user,
|
|
|
|
required this.publicKey,
|
2024-02-20 16:33:24 +00:00
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
2022-05-24 18:55:39 +00:00
|
|
|
}) : super(title: 'jobs.delete_ssh_key'.tr(args: [user.login]));
|
2022-03-03 17:38:30 +00:00
|
|
|
|
|
|
|
final User user;
|
|
|
|
final String publicKey;
|
|
|
|
|
2022-10-07 17:50:18 +00:00
|
|
|
@override
|
2022-10-08 16:17:58 +00:00
|
|
|
bool canAddTo(final List<ClientJob> jobs) => !jobs.any(
|
|
|
|
(final job) =>
|
|
|
|
job is DeleteSSHKeyJob &&
|
|
|
|
job.publicKey == publicKey &&
|
|
|
|
job.user.login == user.login,
|
|
|
|
);
|
2022-10-07 17:50:18 +00:00
|
|
|
|
2022-10-06 19:15:25 +00:00
|
|
|
@override
|
2024-02-20 17:04:39 +00:00
|
|
|
Future<(bool, String)> execute() async =>
|
2024-02-20 16:33:24 +00:00
|
|
|
getIt<ApiConnectionRepository>().deleteSshKey(user, publicKey);
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<Object> get props => [...super.props, user, publicKey];
|
|
|
|
|
|
|
|
@override
|
|
|
|
DeleteSSHKeyJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
DeleteSSHKeyJob(
|
|
|
|
user: user,
|
|
|
|
publicKey: publicKey,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ReplaceableJob extends ClientJob {
|
|
|
|
ReplaceableJob({
|
|
|
|
required super.title,
|
|
|
|
super.id,
|
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
});
|
|
|
|
|
|
|
|
bool shouldRemoveInsteadOfAdd(final List<ClientJob> jobs) => false;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChangeAutoUpgradeSettingsJob extends ReplaceableJob {
|
|
|
|
ChangeAutoUpgradeSettingsJob({
|
|
|
|
required this.enable,
|
|
|
|
required this.allowReboot,
|
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
|
|
|
}) : super(title: 'jobs.change_auto_upgrade_settings'.tr());
|
|
|
|
|
|
|
|
final bool enable;
|
|
|
|
final bool allowReboot;
|
|
|
|
|
|
|
|
@override
|
2024-02-20 20:17:36 +00:00
|
|
|
Future<(bool, String)> execute() async => getIt<ApiConnectionRepository>()
|
|
|
|
.setAutoUpgradeSettings(enable, allowReboot);
|
2024-02-20 16:33:24 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool shouldRemoveInsteadOfAdd(final List<ClientJob> jobs) {
|
2024-02-20 20:17:36 +00:00
|
|
|
final currentSettings = getIt<ApiConnectionRepository>()
|
|
|
|
.apiData
|
|
|
|
.settings
|
|
|
|
.data
|
|
|
|
?.autoUpgradeSettings;
|
|
|
|
if (currentSettings == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return currentSettings.enable == enable &&
|
|
|
|
currentSettings.allowReboot == allowReboot;
|
2024-02-20 16:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<Object> get props => [...super.props, enable, allowReboot];
|
|
|
|
|
|
|
|
@override
|
|
|
|
ChangeAutoUpgradeSettingsJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
ChangeAutoUpgradeSettingsJob(
|
|
|
|
enable: enable,
|
|
|
|
allowReboot: allowReboot,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChangeServerTimezoneJob extends ReplaceableJob {
|
|
|
|
ChangeServerTimezoneJob({
|
|
|
|
required this.timezone,
|
|
|
|
super.status,
|
|
|
|
super.message,
|
|
|
|
super.id,
|
|
|
|
}) : super(title: 'jobs.change_server_timezone'.tr());
|
|
|
|
|
|
|
|
final String timezone;
|
|
|
|
|
|
|
|
@override
|
2024-02-20 20:17:36 +00:00
|
|
|
Future<(bool, String)> execute() async =>
|
|
|
|
getIt<ApiConnectionRepository>().setServerTimezone(timezone);
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2022-03-03 17:38:30 +00:00
|
|
|
@override
|
2024-02-20 16:33:24 +00:00
|
|
|
bool shouldRemoveInsteadOfAdd(final List<ClientJob> jobs) {
|
2024-02-20 20:17:36 +00:00
|
|
|
final currentSettings =
|
|
|
|
getIt<ApiConnectionRepository>().apiData.settings.data?.timezone;
|
|
|
|
if (currentSettings == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return currentSettings == timezone;
|
2024-02-20 16:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<Object> get props => [...super.props, timezone];
|
|
|
|
|
|
|
|
@override
|
|
|
|
ChangeServerTimezoneJob copyWithNewStatus({
|
|
|
|
required final JobStatusEnum status,
|
|
|
|
final String? message,
|
|
|
|
}) =>
|
|
|
|
ChangeServerTimezoneJob(
|
|
|
|
timezone: timezone,
|
|
|
|
status: status,
|
|
|
|
message: message,
|
|
|
|
id: id,
|
|
|
|
);
|
2021-09-02 19:32:07 +00:00
|
|
|
}
|