mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 21:46:46 +00:00
Request and cache plugin information when connecting to the server
This commit is contained in:
parent
0c9c7b2a68
commit
de45e18359
39
lib/api.dart
39
lib/api.dart
@ -304,6 +304,9 @@ class InvenTreeAPI {
|
|||||||
|
|
||||||
_BASE_URL = address;
|
_BASE_URL = address;
|
||||||
|
|
||||||
|
// Clear the list of available plugins
|
||||||
|
plugins.clear();
|
||||||
|
|
||||||
print("Connecting to ${apiUrl} -> username=${username}");
|
print("Connecting to ${apiUrl} -> username=${username}");
|
||||||
|
|
||||||
APIResponse response;
|
APIResponse response;
|
||||||
@ -398,8 +401,11 @@ class InvenTreeAPI {
|
|||||||
_token = (data["token"] ?? "") as String;
|
_token = (data["token"] ?? "") as String;
|
||||||
print("Received token - $_token");
|
print("Received token - $_token");
|
||||||
|
|
||||||
// Request user role information
|
// Request user role information (async)
|
||||||
await getUserRoles();
|
getUserRoles();
|
||||||
|
|
||||||
|
// Request plugin information (async)
|
||||||
|
getPluginInformation();
|
||||||
|
|
||||||
// Ok, probably pretty good...
|
// Ok, probably pretty good...
|
||||||
return true;
|
return true;
|
||||||
@ -461,7 +467,7 @@ class InvenTreeAPI {
|
|||||||
// Any "older" version of the server allows any API method for any logged in user!
|
// Any "older" version of the server allows any API method for any logged in user!
|
||||||
// We will return immediately, but request the user roles in the background
|
// We will return immediately, but request the user roles in the background
|
||||||
|
|
||||||
var response = await get(_URL_GET_ROLES, expectedStatusCode: 200);
|
final response = await get(_URL_GET_ROLES, expectedStatusCode: 200);
|
||||||
|
|
||||||
if (!response.successful()) {
|
if (!response.successful()) {
|
||||||
return;
|
return;
|
||||||
@ -471,10 +477,35 @@ class InvenTreeAPI {
|
|||||||
|
|
||||||
if (data.containsKey("roles")) {
|
if (data.containsKey("roles")) {
|
||||||
// Save a local copy of the user roles
|
// Save a local copy of the user roles
|
||||||
roles = response.data["roles"] as Map<String, dynamic>;
|
roles = (response.data["roles"] ?? {}) as Map<String, dynamic>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Request plugin information from the server
|
||||||
|
Future<void> getPluginInformation() async {
|
||||||
|
|
||||||
|
// The server does not support plugins, or they are not enabled
|
||||||
|
if (!pluginsEnabled()) {
|
||||||
|
plugins.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request a list of plugins from the server
|
||||||
|
final List<InvenTreeModel> results = await InvenTreePlugin().list();
|
||||||
|
|
||||||
|
for (var result in results) {
|
||||||
|
if (result is InvenTreePlugin) {
|
||||||
|
if (result.active) {
|
||||||
|
// Only add plugins that are active
|
||||||
|
plugins.add(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print("Discovered ${plugins.length} active plugins!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool checkPermission(String role, String permission) {
|
bool checkPermission(String role, String permission) {
|
||||||
/*
|
/*
|
||||||
* Check if the user has the given role.permission assigned
|
* Check if the user has the given role.permission assigned
|
||||||
|
@ -509,8 +509,27 @@ class InvenTreePlugin extends InvenTreeModel {
|
|||||||
|
|
||||||
InvenTreePlugin.fromJson(Map<String, dynamic> json) : super.fromJson(json);
|
InvenTreePlugin.fromJson(Map<String, dynamic> json) : super.fromJson(json);
|
||||||
|
|
||||||
|
@override
|
||||||
|
InvenTreeModel createFromJson(Map<String, dynamic> json) {
|
||||||
|
return InvenTreePlugin.fromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get URL => "plugin/";
|
String get URL => "plugin/";
|
||||||
|
|
||||||
|
String get key => (jsondata["key"] ?? "") as String;
|
||||||
|
|
||||||
|
bool get active => (jsondata["active"] ?? false) as bool;
|
||||||
|
|
||||||
|
// Return the metadata struct for this plugin
|
||||||
|
Map<String, dynamic> get _meta => (jsondata["meta"] ?? {}) as Map<String, dynamic>;
|
||||||
|
|
||||||
|
// Return the mixins struct for this plugin
|
||||||
|
Map<String, dynamic> get _mixins => (jsondata["mixins"] ?? {}) as Map<String, dynamic>;
|
||||||
|
|
||||||
|
bool supportsMixin(String mixin) {
|
||||||
|
return _mixins.containsKey(mixin);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user