Skip to content

Commit 5166044

Browse files
feat(json_assets_demo): add JSON Assets Demo for policy management
1 parent 2c96f16 commit 5166044

File tree

5 files changed

+548
-0
lines changed

5 files changed

+548
-0
lines changed

example/JSON_ASSETS_DEMO.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"admin": {
3+
"allowedContent": [
4+
"read",
5+
"write",
6+
"delete",
7+
"manage_users",
8+
"system_config",
9+
"all",
10+
"admin_panel",
11+
"user_management",
12+
"system_settings"
13+
]
14+
},
15+
"manager": {
16+
"allowedContent": [
17+
"read",
18+
"write",
19+
"manage_team",
20+
"team_content",
21+
"reports",
22+
"analytics",
23+
"public"
24+
]
25+
},
26+
"editor": {
27+
"allowedContent": [
28+
"read",
29+
"write",
30+
"publish",
31+
"content_creation",
32+
"drafts",
33+
"published_content",
34+
"public"
35+
]
36+
},
37+
"viewer": {
38+
"allowedContent": ["read", "public", "published_content", "reports"]
39+
},
40+
"guest": {
41+
"allowedContent": ["read", "public"]
42+
}
43+
}

0 commit comments

Comments
 (0)