mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 13:36:50 +00:00
188 lines
4.6 KiB
Dart
188 lines
4.6 KiB
Dart
|
|
/*
|
|
* Class for InvenTree user / login details
|
|
*/
|
|
import 'package:sembast/sembast.dart';
|
|
import 'preferences.dart';
|
|
|
|
class UserProfile {
|
|
|
|
UserProfile({
|
|
this.key,
|
|
this.name,
|
|
this.server,
|
|
this.username,
|
|
this.password,
|
|
this.selected,
|
|
});
|
|
|
|
// ID of the profile
|
|
int key;
|
|
|
|
// Name of the user profile
|
|
String name;
|
|
|
|
// Base address of the InvenTree server
|
|
String server;
|
|
|
|
// Username
|
|
String username;
|
|
|
|
// Password
|
|
String password;
|
|
|
|
bool selected = false;
|
|
|
|
// User ID (will be provided by the server on log-in)
|
|
int user_id;
|
|
|
|
factory UserProfile.fromJson(int key, Map<String, dynamic> json) => UserProfile(
|
|
key: key,
|
|
name: json['name'],
|
|
server: json['server'],
|
|
username: json['username'],
|
|
password: json['password'],
|
|
selected: json['selected'] ?? false,
|
|
);
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
"name": name,
|
|
"server": server,
|
|
"username": username,
|
|
"password": password,
|
|
"selected": selected,
|
|
};
|
|
|
|
@override
|
|
String toString() {
|
|
return "${server} - ${username}:${password}";
|
|
}
|
|
}
|
|
|
|
class UserProfileDBManager {
|
|
|
|
static const String folder_name = "profiles";
|
|
|
|
final _folder = intMapStoreFactory.store(folder_name);
|
|
|
|
Future<Database> get _db async => await InvenTreePreferencesDB.instance.database;
|
|
|
|
Future addProfile(UserProfile profile) async {
|
|
|
|
UserProfile existingProfile = await getProfile(profile.name);
|
|
|
|
if (existingProfile != null) {
|
|
print("UserProfile '${profile.name}' already exists");
|
|
return;
|
|
}
|
|
|
|
int key = await _folder.add(await _db, profile.toJson());
|
|
|
|
print("Added user profile <${key}> - '${profile.name}'");
|
|
|
|
// Record the key
|
|
profile.key = key;
|
|
}
|
|
|
|
Future updateProfile(UserProfile profile) async {
|
|
|
|
if (profile.key == null) {
|
|
addProfile(profile);
|
|
return;
|
|
}
|
|
|
|
final finder = Finder(filter: Filter.byKey(profile.key));
|
|
await _folder.update(await _db, profile.toJson(), finder: finder);
|
|
|
|
print("Updated user profile <${profile.key}> - '${profile.name}");
|
|
}
|
|
|
|
Future deleteProfile(UserProfile profile) async {
|
|
final finder = Finder(filter: Filter.equals("name", profile.name));
|
|
await _folder.delete(await _db, finder: finder);
|
|
|
|
print("Deleted user profile <${profile.key}> - '${profile.name}'");
|
|
}
|
|
|
|
Future<UserProfile> getSelectedProfile() async {
|
|
/*
|
|
* Return the currently selected profile.
|
|
*
|
|
* If multiple profiles are selected,
|
|
* mark all but the first as unselected
|
|
*
|
|
* If no profile is currently selected,
|
|
* then force the first profile to be selected.
|
|
*/
|
|
|
|
final selected_finder = Finder(filter: Filter.equals("selected", true));
|
|
|
|
final selected_profiles = await _folder.find(await _db, finder: selected_finder);
|
|
|
|
if (selected_profiles.length == 1) {
|
|
// A single profile is selected
|
|
return UserProfile.fromJson(selected_profiles[0].key, selected_profiles[0].value);
|
|
} else if (selected_profiles.length > 1) {
|
|
// Multiple selected profiles - de-select others
|
|
for (int idx = 1; idx < selected_profiles.length; idx++) {
|
|
UserProfile profile = UserProfile.fromJson(selected_profiles[idx].key, selected_profiles[idx].value);
|
|
|
|
profile.selected = false;
|
|
updateProfile(profile);
|
|
}
|
|
|
|
// And return the first profile
|
|
return UserProfile.fromJson(selected_profiles[0].key, selected_profiles[0].value);
|
|
} else {
|
|
// No profiles selected!
|
|
|
|
final all_profiles = await getAllProfiles();
|
|
|
|
if (all_profiles.length == 0) {
|
|
// No profiles available
|
|
return null;
|
|
} else {
|
|
UserProfile prf = all_profiles[0];
|
|
prf.selected = true;
|
|
updateProfile(prf);
|
|
|
|
// Return the selected profile
|
|
return prf;
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<UserProfile> getProfile(String name) async {
|
|
|
|
print("Looking for user profile '${name}'");
|
|
|
|
// Lookup profile by name (or return null if does not exist)
|
|
final finder = Finder(filter: Filter.equals("name", name));
|
|
|
|
final profiles = await _folder.find(await _db, finder: finder);
|
|
|
|
if (profiles.length == 0) {
|
|
print("No matching profiles found");
|
|
return null;
|
|
}
|
|
|
|
// Return the first matching profile object
|
|
return UserProfile.fromJson(profiles[0].key, profiles[0].value);
|
|
}
|
|
|
|
/*
|
|
* Return all user profile objects
|
|
*/
|
|
Future<List<UserProfile>> getAllProfiles() async {
|
|
final profiles = await _folder.find(await _db);
|
|
|
|
List<UserProfile> profileList = new List<UserProfile>();
|
|
|
|
for (int idx = 0; idx < profiles.length; idx++) {
|
|
profileList.add(UserProfile.fromJson(profiles[idx].key, profiles[idx].value));
|
|
}
|
|
|
|
return profileList;
|
|
}
|
|
}
|