@@ -86,54 +86,43 @@ class PolicyManager extends ChangeNotifier {
86
86
final key = entry.key;
87
87
final value = entry.value;
88
88
89
- try {
90
- if (value == null ) {
91
- LogHandler .warning (
92
- 'Skipping null policy value' ,
93
- context: {'role' : key},
94
- operation: 'policy_validation_skip' ,
95
- );
96
- continue ;
97
- }
98
-
99
- if (value is ! List ) {
100
- LogHandler .warning (
101
- 'Skipping invalid policy value type' ,
102
- context: {
103
- 'role' : key,
104
- 'expected_type' : 'List' ,
105
- 'actual_type' : value.runtimeType.toString (),
106
- },
107
- operation: 'policy_validation_skip' ,
108
- );
109
- continue ;
110
- }
111
-
112
- if (value.any ((item) => item is ! String )) {
113
- LogHandler .warning (
114
- 'Skipping policy with non-string content items' ,
115
- context: {'role' : key},
116
- operation: 'policy_validation_skip' ,
117
- );
118
- continue ;
119
- }
120
-
121
- // Create the policy and add to valid policies
122
- final policy = Policy (
123
- roleName: key,
124
- allowedContent: value.cast <String >(),
89
+ if (value == null ) {
90
+ LogHandler .warning (
91
+ 'Skipping null policy value' ,
92
+ context: {'role' : key},
93
+ operation: 'policy_validation_skip' ,
94
+ );
95
+ continue ;
96
+ }
97
+
98
+ if (value is ! List ) {
99
+ LogHandler .warning (
100
+ 'Skipping invalid policy value type' ,
101
+ context: {
102
+ 'role' : key,
103
+ 'expected_type' : 'List' ,
104
+ 'actual_type' : value.runtimeType.toString (),
105
+ },
106
+ operation: 'policy_validation_skip' ,
125
107
);
126
- validPolicies[key] = policy. toJson () ;
127
- } catch (e, stackTrace) {
128
- LogHandler . error (
129
- 'Failed to process policy' ,
130
- error : e,
131
- stackTrace : stackTrace ,
108
+ continue ;
109
+ }
110
+
111
+ if (value. any ((item) => item is ! String )) {
112
+ LogHandler . warning (
113
+ 'Skipping policy with non-string content items' ,
132
114
context: {'role' : key},
133
- operation: 'policy_processing_error ' ,
115
+ operation: 'policy_validation_skip ' ,
134
116
);
135
- // Continue with other policies
117
+ continue ;
136
118
}
119
+
120
+ // Create the policy and add to valid policies
121
+ final policy = Policy (
122
+ roleName: key,
123
+ allowedContent: value.cast <String >(),
124
+ );
125
+ validPolicies[key] = policy.toJson ();
137
126
}
138
127
139
128
_policies = JsonHandler .parseMap (
@@ -185,4 +174,24 @@ class PolicyManager extends ChangeNotifier {
185
174
rethrow ;
186
175
}
187
176
}
177
+
178
+ /// Checks if the specified [role] has access to the given [content] .
179
+ ///
180
+ /// Returns `true` if the policy manager is initialized and the evaluator
181
+ /// determines that the [role] is permitted to access the [content] .
182
+ /// Returns `false` if the policy manager is not initialized, the evaluator
183
+ /// is not set, or if access is denied.
184
+ ///
185
+ /// Logs an error if called before initialization or if the evaluator is missing.
186
+ bool hasAccess (String role, String content) {
187
+ if (! _isInitialized || _evaluator == null ) {
188
+ LogHandler .error (
189
+ 'Policy manager not initialized or evaluator not set' ,
190
+ context: {'role' : role, 'content' : content},
191
+ operation: 'policy_manager_access_check' ,
192
+ );
193
+ return false ;
194
+ }
195
+ return _evaluator! .evaluate (role, content);
196
+ }
188
197
}
0 commit comments