rustdesk/flutter/lib/models/user_model.dart

203 lines
5.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
import 'package:bot_toast/bot_toast.dart';
2023-06-22 10:04:16 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_hbb/common/hbbs/hbbs.dart';
import 'package:flutter_hbb/models/ab_model.dart';
import 'package:get/get.dart';
2022-09-27 17:52:36 +08:00
import '../common.dart';
import '../utils/http_service.dart' as http;
import 'model.dart';
import 'platform_model.dart';
bool refreshingUser = false;
class UserModel {
final RxString userName = ''.obs;
final RxBool isAdmin = false.obs;
bool get isLogin => userName.isNotEmpty;
WeakReference<FFI> parent;
UserModel(this.parent);
void refreshCurrentUser() async {
if (bind.isDisableAccount()) return;
final token = bind.mainGetLocalOption(key: 'access_token');
if (token == '') {
await updateOtherModels();
return;
}
_updateLocalUserInfo();
final url = await bind.mainGetApiServer();
final body = {
'id': await bind.mainGetMyId(),
'uuid': await bind.mainGetUuid()
};
if (refreshingUser) return;
try {
refreshingUser = true;
final response = await http.post(Uri.parse('$url/api/currentUser'),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token'
},
body: json.encode(body));
refreshingUser = false;
final status = response.statusCode;
if (status == 401 || status == 400) {
reset(resetOther: status == 401);
return;
}
final data = json.decode(utf8.decode(response.bodyBytes));
final error = data['error'];
if (error != null) {
throw error;
}
final user = UserPayload.fromJson(data);
_parseAndUpdateUser(user);
} catch (e) {
debugPrint('Failed to refreshCurrentUser: $e');
} finally {
refreshingUser = false;
await updateOtherModels();
}
}
static Map<String, dynamic>? getLocalUserInfo() {
final userInfo = bind.mainGetLocalOption(key: 'user_info');
if (userInfo == '') {
return null;
}
try {
return json.decode(userInfo);
} catch (e) {
2023-06-22 22:33:54 +08:00
debugPrint('Failed to get local user info "$userInfo": $e');
}
return null;
}
_updateLocalUserInfo() {
final userInfo = getLocalUserInfo();
if (userInfo != null) {
userName.value = userInfo['name'];
}
}
Future<void> reset({bool resetOther = false}) async {
await bind.mainSetLocalOption(key: 'access_token', value: '');
await bind.mainSetLocalOption(key: 'user_info', value: '');
if (resetOther) {
await gFFI.abModel.reset();
await gFFI.groupModel.reset();
}
userName.value = '';
}
_parseAndUpdateUser(UserPayload user) {
userName.value = user.name;
isAdmin.value = user.isAdmin;
bind.mainSetLocalOption(key: 'user_info', value: jsonEncode(user));
}
// update ab and group status
static Future<void> updateOtherModels() async {
await Future.wait([
gFFI.abModel.pullAb(force: ForcePullAb.listAndCurrent, quiet: false),
gFFI.groupModel.pull()
]);
}
Future<void> logOut({String? apiServer}) async {
2022-10-09 18:57:38 +08:00
final tag = gFFI.dialogManager.showLoading(translate('Waiting'));
try {
final url = apiServer ?? await bind.mainGetApiServer();
2023-01-28 21:02:42 +08:00
final authHeaders = getHttpHeaders();
authHeaders['Content-Type'] = "application/json";
await http
.post(Uri.parse('$url/api/logout'),
2023-01-28 21:02:42 +08:00
body: jsonEncode({
'id': await bind.mainGetMyId(),
'uuid': await bind.mainGetUuid(),
2023-01-28 21:02:42 +08:00
}),
headers: authHeaders)
.timeout(Duration(seconds: 2));
} catch (e) {
debugPrint("request /api/logout failed: err=$e");
} finally {
await reset(resetOther: true);
gFFI.dialogManager.dismissByTag(tag);
}
}
/// throw [RequestException]
Future<LoginResponse> login(LoginRequest loginRequest) async {
final url = await bind.mainGetApiServer();
final resp = await http.post(Uri.parse('$url/api/login'),
body: jsonEncode(loginRequest.toJson()));
final Map<String, dynamic> body;
try {
body = jsonDecode(utf8.decode(resp.bodyBytes));
} catch (e) {
debugPrint("login: jsonDecode resp body failed: ${e.toString()}");
if (resp.statusCode != 200) {
BotToast.showText(
contentColor: Colors.red, text: 'HTTP ${resp.statusCode}');
}
rethrow;
}
if (resp.statusCode != 200) {
throw RequestException(resp.statusCode, body['error'] ?? '');
}
2023-06-22 22:33:54 +08:00
if (body['error'] != null) {
throw RequestException(0, body['error']);
}
return getLoginResponseFromAuthBody(body);
}
LoginResponse getLoginResponseFromAuthBody(Map<String, dynamic> body) {
final LoginResponse loginResponse;
try {
loginResponse = LoginResponse.fromJson(body);
} catch (e) {
debugPrint("login: jsonDecode LoginResponse failed: ${e.toString()}");
rethrow;
}
if (loginResponse.user != null) {
_parseAndUpdateUser(loginResponse.user!);
}
return loginResponse;
}
static Future<List<dynamic>> queryOidcLoginOptions() async {
try {
final url = await bind.mainGetApiServer();
if (url.trim().isEmpty) return [];
final resp = await http.get(Uri.parse('$url/api/login-options'));
final List<String> ops = [];
for (final item in jsonDecode(resp.body)) {
ops.add(item as String);
}
for (final item in ops) {
if (item.startsWith('common-oidc/')) {
return jsonDecode(item.substring('common-oidc/'.length));
}
}
return ops
.where((item) => item.startsWith('oidc/'))
.map((item) => {'name': item.substring('oidc/'.length)})
.toList();
} catch (e) {
2023-06-22 10:04:16 +08:00
debugPrint(
"queryOidcLoginOptions: jsonDecode resp body failed: ${e.toString()}");
return [];
}
}
}