Skip to content

Commit 26995d4

Browse files
feat(policy_manager)!: add initializeFromJsonAssets method
- Loading policies from JSON assets with error handling
1 parent e143d35 commit 26995d4

File tree

2 files changed

+477
-0
lines changed

2 files changed

+477
-0
lines changed

lib/src/core/policy_manager.dart

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter_policy_engine/src/core/interfaces/i_policy_storage.dart'
44
import 'package:flutter_policy_engine/src/core/memory_policy_storage.dart';
55
import 'package:flutter_policy_engine/src/core/role_evaluator.dart';
66
import 'package:flutter_policy_engine/src/models/role.dart';
7+
import 'package:flutter_policy_engine/src/utils/external_asset_handler.dart';
78
import 'package:flutter_policy_engine/src/utils/json_handler.dart';
89
import 'package:flutter_policy_engine/src/utils/log_handler.dart';
910

@@ -175,6 +176,95 @@ class PolicyManager extends ChangeNotifier {
175176
}
176177
}
177178

179+
/// Initializes the policy manager with policy data from a JSON asset file.
180+
///
181+
/// Loads and parses JSON policy data from the specified [assetPath] and
182+
/// initializes the policy manager with the loaded policies. This method
183+
/// provides a convenient way to load policies from external asset files
184+
/// bundled with the Flutter application.
185+
///
186+
/// ## Parameters
187+
///
188+
/// * [assetPath] - The path to the JSON asset file relative to the assets
189+
/// directory (e.g., 'assets/policies/config.json'). Must be declared in
190+
/// the `pubspec.yaml` file under the `assets` section.
191+
///
192+
/// ## Usage
193+
///
194+
/// ```dart
195+
/// final policyManager = PolicyManager();
196+
///
197+
/// // Load policies from an asset file
198+
/// await policyManager.initializeFromJsonAssets('assets/policies/user_roles.json');
199+
///
200+
/// // Check if initialization was successful
201+
/// if (policyManager.isInitialized) {
202+
/// print('Policy manager initialized successfully');
203+
/// }
204+
/// ```
205+
///
206+
/// ## Asset File Format
207+
///
208+
/// The JSON asset file should contain a map where keys are role identifiers
209+
/// and values are JSON representations of [Role] objects:
210+
///
211+
/// ```json
212+
/// {
213+
/// "admin": {
214+
/// "name": "admin",
215+
/// "permissions": ["read", "write", "delete"],
216+
/// "content": ["all"]
217+
/// },
218+
/// "user": {
219+
/// "name": "user",
220+
/// "permissions": ["read"],
221+
/// "content": ["public", "user_content"]
222+
/// }
223+
/// }
224+
/// ```
225+
///
226+
/// ## Error Handling
227+
///
228+
/// This method handles errors gracefully by:
229+
/// * Validating the asset path is not empty
230+
/// * Catching and logging asset loading errors
231+
/// * Catching and logging JSON parsing errors
232+
/// * Catching and logging policy initialization errors
233+
///
234+
/// If any error occurs during the process, it is logged using [LogHandler.error]
235+
/// with detailed context information for debugging.
236+
///
237+
/// ## Throws
238+
///
239+
/// * [ArgumentError] - If [assetPath] is empty or null
240+
///
241+
/// ## Dependencies
242+
///
243+
/// This method depends on:
244+
/// * [ExternalAssetHandler] - For loading and parsing the asset file
245+
/// * [initialize] - For processing the loaded policy data
246+
/// * [LogHandler] - For error logging and debugging
247+
///
248+
Future<void> initializeFromJsonAssets(String assetPath) async {
249+
if (assetPath.isEmpty) {
250+
throw ArgumentError('Asset path cannot be empty');
251+
}
252+
253+
try {
254+
final assetHandler = ExternalAssetHandler(assetPath: assetPath);
255+
final jsonPolicies = await assetHandler.loadAssets();
256+
await initialize(jsonPolicies);
257+
} catch (e, stackTrace) {
258+
LogHandler.error(
259+
'Failed to initialize policy manager from JSON assets',
260+
error: e,
261+
stackTrace: stackTrace,
262+
context: {'asset_path': assetPath},
263+
operation: 'policy_manager_initialize_from_json_assets_error',
264+
);
265+
}
266+
}
267+
178268
/// Checks if the specified [role] has access to the given [content].
179269
///
180270
/// Returns `true` if the policy manager is initialized and the evaluator

0 commit comments

Comments
 (0)