|
| 1 | +# JSON Assets Demo |
| 2 | + |
| 3 | +This demo showcases the `initializeFromJsonAssets` method of the Flutter Policy Engine, demonstrating how to load policy configurations from JSON asset files bundled with your Flutter application. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The JSON Assets Demo provides an interactive interface to: |
| 8 | + |
| 9 | +- Initialize the policy manager from a JSON asset file |
| 10 | +- Test role-based permissions |
| 11 | +- Test content access control |
| 12 | +- View detailed role information |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +### 1. Asset-based Policy Loading |
| 17 | + |
| 18 | +- Loads policies from `assets/policies/user_roles.json` |
| 19 | +- Demonstrates the `initializeFromJsonAssets` method |
| 20 | +- Shows real-time initialization status |
| 21 | + |
| 22 | +### 2. Interactive Testing |
| 23 | + |
| 24 | +- **Role Selection**: Choose from predefined roles (admin, manager, editor, viewer, guest) |
| 25 | +- **Permission Testing**: Test if a role has specific permissions |
| 26 | +- **Content Access Testing**: Verify content access using the `hasAccess` method |
| 27 | +- **Role Information**: View detailed role configuration |
| 28 | + |
| 29 | +### 3. Real-time Feedback |
| 30 | + |
| 31 | +- Visual status indicators |
| 32 | +- Detailed error messages |
| 33 | +- Test results with clear success/failure indicators |
| 34 | + |
| 35 | +## JSON Asset Format |
| 36 | + |
| 37 | +The demo uses a JSON file with the following structure: |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "admin": { |
| 42 | + "allowedContent": [ |
| 43 | + "read", |
| 44 | + "write", |
| 45 | + "delete", |
| 46 | + "manage_users", |
| 47 | + "system_config", |
| 48 | + "all", |
| 49 | + "admin_panel", |
| 50 | + "user_management", |
| 51 | + "system_settings" |
| 52 | + ] |
| 53 | + }, |
| 54 | + "manager": { |
| 55 | + "allowedContent": [ |
| 56 | + "read", |
| 57 | + "write", |
| 58 | + "manage_team", |
| 59 | + "team_content", |
| 60 | + "reports", |
| 61 | + "analytics", |
| 62 | + "public" |
| 63 | + ] |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Required Fields |
| 69 | + |
| 70 | +- `allowedContent`: Array of strings representing permissions and content access |
| 71 | + |
| 72 | +## Usage |
| 73 | + |
| 74 | +1. **Launch the Demo**: Select "JSON Assets Demo" from the main menu |
| 75 | +2. **Initialize**: The policy manager automatically initializes from the JSON asset |
| 76 | +3. **Test Permissions**: Select a role and permission to test |
| 77 | +4. **Test Content Access**: Use the `hasAccess` method to verify content access |
| 78 | +5. **View Role Details**: Get comprehensive information about any role |
| 79 | + |
| 80 | +## Implementation Details |
| 81 | + |
| 82 | +### Asset Configuration |
| 83 | + |
| 84 | +The JSON file is declared in `pubspec.yaml`: |
| 85 | + |
| 86 | +```yaml |
| 87 | +flutter: |
| 88 | + assets: |
| 89 | + - assets/policies/ |
| 90 | +``` |
| 91 | +
|
| 92 | +### Policy Manager Initialization |
| 93 | +
|
| 94 | +```dart |
| 95 | +final policyManager = PolicyManager(); |
| 96 | +await policyManager.initializeFromJsonAssets('assets/policies/user_roles.json'); |
| 97 | +``` |
| 98 | + |
| 99 | +### Permission Testing |
| 100 | + |
| 101 | +```dart |
| 102 | +final role = policyManager.roles[roleName]; |
| 103 | +final hasPermission = role?.allowedContent.contains(permission) ?? false; |
| 104 | +``` |
| 105 | + |
| 106 | +### Content Access Testing |
| 107 | + |
| 108 | +```dart |
| 109 | +final hasAccess = policyManager.hasAccess(roleName, content); |
| 110 | +``` |
| 111 | + |
| 112 | +## Available Roles |
| 113 | + |
| 114 | +| Role | Permissions | Content Access | |
| 115 | +| ------- | ------------------------------------------------ | --------------------------------------------------- | |
| 116 | +| admin | read, write, delete, manage_users, system_config | all, admin_panel, user_management, system_settings | |
| 117 | +| manager | read, write, manage_team | team_content, reports, analytics, public | |
| 118 | +| editor | read, write, publish | content_creation, drafts, published_content, public | |
| 119 | +| viewer | read | public, published_content, reports | |
| 120 | +| guest | read | public | |
| 121 | + |
| 122 | +## Error Handling |
| 123 | + |
| 124 | +The demo includes comprehensive error handling: |
| 125 | + |
| 126 | +- Asset loading failures |
| 127 | +- JSON parsing errors |
| 128 | +- Policy initialization errors |
| 129 | +- Role not found scenarios |
| 130 | +- Permission/content access validation |
| 131 | + |
| 132 | +## Benefits |
| 133 | + |
| 134 | +1. **External Configuration**: Policies can be updated without code changes |
| 135 | +2. **Asset Bundling**: JSON files are bundled with the app for offline access |
| 136 | +3. **Flexible Structure**: Easy to modify role permissions and content access |
| 137 | +4. **Runtime Testing**: Interactive testing of policy configurations |
| 138 | +5. **Clear Feedback**: Visual indicators and detailed error messages |
| 139 | + |
| 140 | +This demo demonstrates how to effectively use the `initializeFromJsonAssets` method for flexible, asset-based policy management in Flutter applications. |
0 commit comments