selfprivacy.org.app/lib/logic/cubit/users/users_cubit.dart

185 lines
5.4 KiB
Dart
Raw Normal View History

import 'package:easy_localization/easy_localization.dart';
2021-07-29 05:24:42 +00:00
import 'package:hive/hive.dart';
import 'package:selfprivacy/config/get_it_config.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/config/hive_config.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
import 'package:selfprivacy/logic/models/hive/user.dart';
2020-12-03 16:52:53 +00:00
export 'package:provider/provider.dart';
part 'users_state.dart';
class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
2022-06-05 19:36:32 +00:00
UsersCubit(final ServerInstallationCubit serverInstallationCubit)
: super(
serverInstallationCubit,
const UsersState(
<User>[],
false,
),
);
Box<User> box = Hive.box<User>(BNames.usersBox);
Box serverInstallationBox = Hive.box(BNames.serverInstallationBox);
2020-12-03 16:52:53 +00:00
2022-06-05 19:36:32 +00:00
final ServerApi api = ServerApi();
2021-12-06 18:31:19 +00:00
2022-05-24 18:55:39 +00:00
@override
Future<void> load() async {
if (serverInstallationCubit.state is! ServerInstallationFinished) {
return;
}
final List<User> loadedUsers = box.values.toList();
if (loadedUsers.isNotEmpty) {
emit(
UsersState(
loadedUsers,
false,
),
);
}
refresh();
}
Future<void> refresh() async {
if (serverInstallationCubit.state is! ServerInstallationFinished) {
return;
2021-07-29 05:24:42 +00:00
}
emit(state.copyWith(isLoading: true));
final List<User> usersFromServer = await api.getAllUsers();
if (usersFromServer.isNotEmpty) {
emit(
UsersState(
usersFromServer,
false,
),
);
// Update the users it the box
await box.clear();
await box.addAll(usersFromServer);
} else {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>()
.showSnackBar('users.could_not_fetch_users'.tr());
emit(state.copyWith(isLoading: false));
}
}
2022-06-05 19:36:32 +00:00
Future<void> createUser(final User user) async {
// If user exists on server, do nothing
2022-09-05 04:34:47 +00:00
if (state.users
.any((final User u) => u.login == user.login && u.isFoundOnServer)) {
return;
}
final String? password = user.password;
if (password == null) {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>()
.showSnackBar('users.could_not_create_user'.tr());
return;
}
// If API returned error, do nothing
final GenericMutationResult<User?> result =
2022-09-05 04:34:47 +00:00
await api.createUser(user.login, password);
if (result.data == null) {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>()
.showSnackBar(result.message ?? 'users.could_not_create_user'.tr());
return;
}
2022-06-05 19:36:32 +00:00
final List<User> loadedUsers = List<User>.from(state.users);
loadedUsers.add(result.data!);
2022-03-23 14:07:52 +00:00
await box.clear();
await box.addAll(loadedUsers);
emit(state.copyWith(users: loadedUsers));
}
2022-06-05 19:36:32 +00:00
Future<void> deleteUser(final User user) async {
// If user is primary or root, don't delete
if (user.type != UserType.normal) {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>()
.showSnackBar('users.could_not_delete_user'.tr());
return;
}
2022-06-05 19:36:32 +00:00
final List<User> loadedUsers = List<User>.from(state.users);
final GenericMutationResult result = await api.deleteUser(user.login);
if (result.success && result.data) {
2022-06-05 19:36:32 +00:00
loadedUsers.removeWhere((final User u) => u.login == user.login);
2022-03-23 14:07:52 +00:00
await box.clear();
await box.addAll(loadedUsers);
emit(state.copyWith(users: loadedUsers));
}
if (!result.success) {
getIt<NavigationService>().showSnackBar('jobs.generic_error'.tr());
}
if (!result.data) {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>()
.showSnackBar(result.message ?? 'jobs.generic_error'.tr());
}
}
2022-09-05 04:34:47 +00:00
Future<void> changeUserPassword(
final User user,
final String newPassword,
) async {
if (user.type == UserType.root) {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>()
.showSnackBar('users.could_not_change_password'.tr());
return;
}
final GenericMutationResult<User?> result =
2022-09-05 04:34:47 +00:00
await api.updateUser(user.login, newPassword);
if (result.data == null) {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>().showSnackBar(
result.message ?? 'users.could_not_change_password'.tr(),
);
}
}
2022-06-05 19:36:32 +00:00
Future<void> addSshKey(final User user, final String publicKey) async {
final GenericMutationResult<User?> result =
2022-09-05 04:34:47 +00:00
await api.addSshKey(user.login, publicKey);
if (result.data != null) {
final User updatedUser = result.data!;
final int index =
state.users.indexWhere((final User u) => u.login == user.login);
await box.putAt(index, updatedUser);
emit(
state.copyWith(
users: box.values.toList(),
),
);
} else {
2022-09-05 04:34:47 +00:00
getIt<NavigationService>()
.showSnackBar(result.message ?? 'users.could_not_add_ssh_key'.tr());
}
}
2022-06-05 19:36:32 +00:00
Future<void> deleteSshKey(final User user, final String publicKey) async {
final GenericMutationResult<User?> result =
2022-09-05 04:34:47 +00:00
await api.removeSshKey(user.login, publicKey);
if (result.data != null) {
final User updatedUser = result.data!;
final int index =
state.users.indexWhere((final User u) => u.login == user.login);
await box.putAt(index, updatedUser);
emit(
state.copyWith(
users: box.values.toList(),
),
);
}
}
2020-12-03 16:52:53 +00:00
@override
void clear() async {
emit(
const UsersState(
<User>[],
false,
),
);
}
2020-12-03 16:52:53 +00:00
}