2022-09-05 04:01:36 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-07-29 05:24:42 +00:00
|
|
|
import 'package:hive/hive.dart';
|
2022-09-05 04:01:36 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2021-07-29 05:24:42 +00:00
|
|
|
import 'package:selfprivacy/config/hive_config.dart';
|
2022-11-16 00:24:40 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
|
2022-04-03 19:54:18 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
2022-05-14 02:54:40 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2022-03-03 17:38:30 +00:00
|
|
|
|
2020-12-03 16:52:53 +00:00
|
|
|
export 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
part 'users_state.dart';
|
|
|
|
|
2022-05-17 13:31:34 +00:00
|
|
|
class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
|
2022-06-05 19:36:32 +00:00
|
|
|
UsersCubit(final ServerInstallationCubit serverInstallationCubit)
|
2022-04-03 19:54:18 +00:00
|
|
|
: super(
|
2022-06-05 22:40:34 +00:00
|
|
|
serverInstallationCubit,
|
|
|
|
const UsersState(
|
|
|
|
<User>[],
|
2022-09-05 10:51:01 +00:00
|
|
|
false,
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
|
|
|
);
|
2022-05-18 08:27:36 +00:00
|
|
|
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
|
2022-03-03 17:38:30 +00:00
|
|
|
Future<void> load() async {
|
2022-09-05 04:01:36 +00:00
|
|
|
if (serverInstallationCubit.state is! ServerInstallationFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final List<User> loadedUsers = box.values.toList();
|
|
|
|
if (loadedUsers.isNotEmpty) {
|
2022-06-05 22:40:34 +00:00
|
|
|
emit(
|
|
|
|
UsersState(
|
2022-09-05 04:01:36 +00:00
|
|
|
loadedUsers,
|
2022-09-05 10:51:01 +00:00
|
|
|
false,
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
|
|
|
);
|
2022-04-03 19:54:18 +00:00
|
|
|
}
|
2022-03-03 17:38:30 +00:00
|
|
|
|
2022-09-05 04:01:36 +00:00
|
|
|
refresh();
|
|
|
|
}
|
2022-03-03 17:38:30 +00:00
|
|
|
|
2022-09-05 04:01:36 +00:00
|
|
|
Future<void> refresh() async {
|
|
|
|
if (serverInstallationCubit.state is! ServerInstallationFinished) {
|
|
|
|
return;
|
2021-07-29 05:24:42 +00:00
|
|
|
}
|
2022-09-05 10:51:01 +00:00
|
|
|
emit(state.copyWith(isLoading: true));
|
2022-09-05 04:01:36 +00:00
|
|
|
final List<User> usersFromServer = await api.getAllUsers();
|
|
|
|
if (usersFromServer.isNotEmpty) {
|
|
|
|
emit(
|
|
|
|
UsersState(
|
|
|
|
usersFromServer,
|
2022-09-05 10:51:01 +00:00
|
|
|
false,
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
|
|
|
);
|
2022-09-05 04:01:36 +00:00
|
|
|
// Update the users it the box
|
2022-09-08 15:13:18 +00:00
|
|
|
await box.clear();
|
|
|
|
await box.addAll(usersFromServer);
|
2022-09-05 04:01:36 +00:00
|
|
|
} else {
|
2022-09-05 04:34:47 +00:00
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar('users.could_not_fetch_users'.tr());
|
2022-09-05 10:51:01 +00:00
|
|
|
emit(state.copyWith(isLoading: false));
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> createUser(final User user) async {
|
2022-03-03 17:38:30 +00:00
|
|
|
// 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)) {
|
2022-03-03 17:38:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-09-05 04:01:36 +00:00
|
|
|
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());
|
2022-03-03 17:38:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-02 11:56:46 +00:00
|
|
|
// If API returned error, do nothing
|
2022-12-02 18:40:08 +00:00
|
|
|
final APIGenericResult<User?> result =
|
2022-09-05 04:34:47 +00:00
|
|
|
await api.createUser(user.login, password);
|
2022-10-28 08:17:08 +00:00
|
|
|
if (result.data == null) {
|
2022-09-05 04:34:47 +00:00
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar(result.message ?? 'users.could_not_create_user'.tr());
|
2022-05-02 11:56:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final List<User> loadedUsers = List<User>.from(state.users);
|
2022-10-28 08:17:08 +00:00
|
|
|
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-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> deleteUser(final User user) async {
|
2022-03-03 17:38:30 +00:00
|
|
|
// If user is primary or root, don't delete
|
2022-09-05 04:01:36 +00:00
|
|
|
if (user.type != UserType.normal) {
|
2022-09-05 04:34:47 +00:00
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar('users.could_not_delete_user'.tr());
|
2022-03-03 17:38:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-05 19:36:32 +00:00
|
|
|
final List<User> loadedUsers = List<User>.from(state.users);
|
2022-12-02 18:40:08 +00:00
|
|
|
final APIGenericResult result = await api.deleteUser(user.login);
|
2022-10-28 08:17:08 +00:00
|
|
|
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));
|
2022-10-28 08:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.success) {
|
|
|
|
getIt<NavigationService>().showSnackBar('jobs.generic_error'.tr());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.data) {
|
2022-09-05 04:34:47 +00:00
|
|
|
getIt<NavigationService>()
|
2022-10-28 08:17:08 +00:00
|
|
|
.showSnackBar(result.message ?? 'jobs.generic_error'.tr());
|
2022-09-05 04:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 04:34:47 +00:00
|
|
|
Future<void> changeUserPassword(
|
2022-09-05 10:51:01 +00:00
|
|
|
final User user,
|
|
|
|
final String newPassword,
|
|
|
|
) async {
|
2022-09-05 04:01:36 +00:00
|
|
|
if (user.type == UserType.root) {
|
2022-09-05 04:34:47 +00:00
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar('users.could_not_change_password'.tr());
|
2022-09-05 04:01:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-02 18:40:08 +00:00
|
|
|
final APIGenericResult<User?> result =
|
2022-09-05 04:34:47 +00:00
|
|
|
await api.updateUser(user.login, newPassword);
|
2022-10-28 08:17:08 +00:00
|
|
|
if (result.data == null) {
|
2022-09-05 04:34:47 +00:00
|
|
|
getIt<NavigationService>().showSnackBar(
|
2022-09-05 10:51:01 +00:00
|
|
|
result.message ?? 'users.could_not_change_password'.tr(),
|
|
|
|
);
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> addSshKey(final User user, final String publicKey) async {
|
2022-12-02 18:40:08 +00:00
|
|
|
final APIGenericResult<User?> result =
|
2022-09-05 04:34:47 +00:00
|
|
|
await api.addSshKey(user.login, publicKey);
|
2022-10-28 08:17:08 +00:00
|
|
|
if (result.data != null) {
|
|
|
|
final User updatedUser = result.data!;
|
2022-09-08 15:13:18 +00:00
|
|
|
final int index =
|
|
|
|
state.users.indexWhere((final User u) => u.login == user.login);
|
|
|
|
await box.putAt(index, updatedUser);
|
2022-09-05 04:01:36 +00:00
|
|
|
emit(
|
|
|
|
state.copyWith(
|
|
|
|
users: box.values.toList(),
|
|
|
|
),
|
|
|
|
);
|
2022-03-03 17:38:30 +00:00
|
|
|
} else {
|
2022-09-05 04:34:47 +00:00
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar(result.message ?? 'users.could_not_add_ssh_key'.tr());
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
Future<void> deleteSshKey(final User user, final String publicKey) async {
|
2022-12-02 18:40:08 +00:00
|
|
|
final APIGenericResult<User?> result =
|
2022-09-05 04:34:47 +00:00
|
|
|
await api.removeSshKey(user.login, publicKey);
|
2022-10-28 08:17:08 +00:00
|
|
|
if (result.data != null) {
|
|
|
|
final User updatedUser = result.data!;
|
2022-09-08 15:13:18 +00:00
|
|
|
final int index =
|
|
|
|
state.users.indexWhere((final User u) => u.login == user.login);
|
|
|
|
await box.putAt(index, updatedUser);
|
2022-06-05 22:40:34 +00:00
|
|
|
emit(
|
|
|
|
state.copyWith(
|
|
|
|
users: box.values.toList(),
|
|
|
|
),
|
|
|
|
);
|
2022-03-03 17:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-03 16:52:53 +00:00
|
|
|
|
2022-04-03 19:54:18 +00:00
|
|
|
@override
|
|
|
|
void clear() async {
|
2022-06-05 22:40:34 +00:00
|
|
|
emit(
|
|
|
|
const UsersState(
|
|
|
|
<User>[],
|
2022-09-05 10:51:01 +00:00
|
|
|
false,
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
|
|
|
);
|
2022-04-03 19:54:18 +00:00
|
|
|
}
|
2020-12-03 16:52:53 +00:00
|
|
|
}
|