Skip to content

Commit 9486406

Browse files
feat(role): add role management demo and testing interface
- Introduced a new Role Management Test screen to interactively test role management operations. - Implemented role addition, update, and removal functionalities with real-time policy evaluation. - Enhanced the main app structure to include navigation to the Role Management Demo. - Included comprehensive error handling and user feedback mechanisms for role management actions.
1 parent d094bbd commit 9486406

File tree

3 files changed

+593
-1
lines changed

3 files changed

+593
-1
lines changed

example/ROLE_MANAGEMENT_TEST.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Role Management Test
2+
3+
This dynamic test screen allows you to interactively test the role management operations from the Flutter Policy Engine.
4+
5+
## Features
6+
7+
### 1. Current Roles Display
8+
9+
- Shows all currently available roles in the system
10+
- Displays permissions for each role
11+
- Click on any role to select it for testing or editing
12+
13+
### 2. Role Management Operations
14+
15+
Test the three main role management functions:
16+
17+
#### Add Role
18+
19+
- Enter a new role name and permissions
20+
- Permissions should be comma-separated (e.g., "LoginPage, Dashboard, Settings")
21+
- Click "Add Role" to create the new role
22+
23+
#### Update Role
24+
25+
- Select an existing role or enter a role name
26+
- Modify the permissions as needed
27+
- Click "Update Role" to apply changes
28+
29+
#### Remove Role
30+
31+
- Enter the name of the role to remove
32+
- Click "Remove Role" to delete it from the system
33+
34+
### 3. Policy Testing
35+
36+
- Select any role from the available roles
37+
- See real-time policy evaluation for different content types:
38+
- Login Page
39+
- Dashboard
40+
- User Management
41+
- Settings
42+
- Green cards indicate access granted
43+
- Red cards indicate access denied
44+
45+
## Usage Examples
46+
47+
### Adding a New Role
48+
49+
1. Enter role name: "moderator"
50+
2. Enter permissions: "LoginPage, Dashboard, UserManagement"
51+
3. Click "Add Role"
52+
4. The new role appears in the Current Roles list
53+
54+
### Testing Access Control
55+
56+
1. Select "admin" role
57+
2. Observe that all content types show green (access granted)
58+
3. Switch to "guest" role
59+
4. Observe that only Login Page shows green, others show red (access denied)
60+
61+
### Updating Permissions
62+
63+
1. Click on "user" role in the Current Roles list
64+
2. The form will be populated with current permissions
65+
3. Add "Settings" to the permissions field
66+
4. Click "Update Role"
67+
5. Test the updated permissions in the Policy Testing section
68+
69+
## Technical Details
70+
71+
This test screen demonstrates:
72+
73+
- `PolicyManager.addRole()` - Adding new roles
74+
- `PolicyManager.removeRole()` - Removing existing roles
75+
- `PolicyManager.updateRole()` - Updating role permissions
76+
- `PolicyWidget` - Real-time policy evaluation
77+
- Error handling and user feedback
78+
- Dynamic UI updates based on policy changes
79+
80+
## Error Handling
81+
82+
The test includes comprehensive error handling:
83+
84+
- Validation for empty role names
85+
- Try-catch blocks for all operations
86+
- User-friendly status messages
87+
- Automatic status message clearing after 3 seconds
88+
89+
## Integration
90+
91+
This test screen is accessible from the main app home screen and provides a comprehensive way to validate the role management functionality of the Flutter Policy Engine.

example/lib/main.dart

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_policy_engine_example/policy_engine_demo.dart';
3+
import 'package:flutter_policy_engine_example/role_management_demo.dart';
34

45
void main() {
56
runApp(const MyApp());
@@ -16,7 +17,90 @@ class MyApp extends StatelessWidget {
1617
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
1718
useMaterial3: true,
1819
),
19-
home: const PolicyEngineDemo(),
20+
home: const HomeScreen(),
21+
);
22+
}
23+
}
24+
25+
class HomeScreen extends StatelessWidget {
26+
const HomeScreen({super.key});
27+
28+
@override
29+
Widget build(BuildContext context) {
30+
return Scaffold(
31+
appBar: AppBar(
32+
title: const Text('Flutter Policy Engine Demo'),
33+
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
34+
),
35+
body: Padding(
36+
padding: const EdgeInsets.all(16.0),
37+
child: Column(
38+
mainAxisAlignment: MainAxisAlignment.center,
39+
crossAxisAlignment: CrossAxisAlignment.stretch,
40+
children: [
41+
Card(
42+
child: Padding(
43+
padding: const EdgeInsets.all(16.0),
44+
child: Column(
45+
children: [
46+
const Icon(
47+
Icons.security,
48+
size: 64,
49+
color: Colors.blue,
50+
),
51+
const SizedBox(height: 16),
52+
Text(
53+
'Flutter Policy Engine',
54+
style: Theme.of(context).textTheme.headlineMedium,
55+
textAlign: TextAlign.center,
56+
),
57+
const SizedBox(height: 8),
58+
Text(
59+
'Interactive demo and testing tools',
60+
style: Theme.of(context).textTheme.bodyLarge,
61+
textAlign: TextAlign.center,
62+
),
63+
],
64+
),
65+
),
66+
),
67+
const SizedBox(height: 24),
68+
ElevatedButton.icon(
69+
onPressed: () {
70+
Navigator.push(
71+
context,
72+
MaterialPageRoute(
73+
builder: (context) => const PolicyEngineDemo(),
74+
),
75+
);
76+
},
77+
icon: const Icon(Icons.play_arrow),
78+
label: const Text('Basic Policy Demo'),
79+
style: ElevatedButton.styleFrom(
80+
padding: const EdgeInsets.all(16),
81+
),
82+
),
83+
const SizedBox(height: 16),
84+
ElevatedButton.icon(
85+
onPressed: () {
86+
Navigator.push(
87+
context,
88+
MaterialPageRoute(
89+
builder: (context) => const RoleManagementDemo(),
90+
),
91+
);
92+
},
93+
icon: const Icon(Icons.admin_panel_settings),
94+
label: const Text('Role Management Demo'),
95+
style: ElevatedButton.styleFrom(
96+
padding: const EdgeInsets.all(16),
97+
backgroundColor: Colors.orange,
98+
foregroundColor: Colors.white,
99+
),
100+
),
101+
],
102+
),
103+
),
20104
);
21105
}
22106
}

0 commit comments

Comments
 (0)