diff --git a/lib/api.dart b/lib/api.dart index bce13773..1574b6b6 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -304,6 +304,9 @@ class InvenTreeAPI { _BASE_URL = address; + // Clear the list of available plugins + plugins.clear(); + print("Connecting to ${apiUrl} -> username=${username}"); APIResponse response; @@ -398,8 +401,11 @@ class InvenTreeAPI { _token = (data["token"] ?? "") as String; print("Received token - $_token"); - // Request user role information - await getUserRoles(); + // Request user role information (async) + getUserRoles(); + + // Request plugin information (async) + getPluginInformation(); // Ok, probably pretty good... return true; @@ -461,7 +467,7 @@ class InvenTreeAPI { // 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 - var response = await get(_URL_GET_ROLES, expectedStatusCode: 200); + final response = await get(_URL_GET_ROLES, expectedStatusCode: 200); if (!response.successful()) { return; @@ -471,10 +477,35 @@ class InvenTreeAPI { if (data.containsKey("roles")) { // Save a local copy of the user roles - roles = response.data["roles"] as Map; + roles = (response.data["roles"] ?? {}) as Map; } } + // Request plugin information from the server + Future 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 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) { /* * Check if the user has the given role.permission assigned diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 57227fe0..dd91834b 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -509,8 +509,27 @@ class InvenTreePlugin extends InvenTreeModel { InvenTreePlugin.fromJson(Map json) : super.fromJson(json); + @override + InvenTreeModel createFromJson(Map json) { + return InvenTreePlugin.fromJson(json); + } + @override 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 get _meta => (jsondata["meta"] ?? {}) as Map; + + // Return the mixins struct for this plugin + Map get _mixins => (jsondata["mixins"] ?? {}) as Map; + + bool supportsMixin(String mixin) { + return _mixins.containsKey(mixin); + } }