@@ -194,4 +194,80 @@ class PolicyManager extends ChangeNotifier {
194
194
}
195
195
return _evaluator! .evaluate (role, content);
196
196
}
197
+
198
+ /// Adds a new role to the policy manager.
199
+ ///
200
+ /// Adds the specified [role] to the internal policy cache and updates the
201
+ /// evaluator with the new role configuration. The role is also persisted
202
+ /// to storage and listeners are notified of the change.
203
+ ///
204
+ /// If a role with the same name already exists, it will be overwritten.
205
+ ///
206
+ /// [role] must not be null and should have a valid name.
207
+ ///
208
+ /// Throws:
209
+ /// - [ArgumentError] if [role] is null or has an invalid name
210
+ /// - Storage-related exceptions if persistence fails
211
+ Future <void > addRole (Role role) async {
212
+ if (role.name.isEmpty) {
213
+ throw ArgumentError ('Role name cannot be empty' );
214
+ }
215
+
216
+ _roles[role.name] = role;
217
+ _evaluator = RoleEvaluator (_roles);
218
+ await _storage.savePolicies (_roles);
219
+ notifyListeners ();
220
+ }
221
+
222
+ /// Removes a role from the policy manager.
223
+ ///
224
+ /// Removes the role identified by [roleName] from the internal policy cache
225
+ /// and updates the evaluator with the modified role configuration. The
226
+ /// updated policy state is persisted to storage and listeners are notified
227
+ /// of the change.
228
+ ///
229
+ /// If no role exists with the specified [roleName] , the operation completes
230
+ /// successfully without any changes.
231
+ ///
232
+ /// [roleName] must not be null or empty.
233
+ ///
234
+ /// Throws:
235
+ /// - [ArgumentError] if [roleName] is null or empty
236
+ /// - Storage-related exceptions if persistence fails
237
+ Future <void > removeRole (String roleName) async {
238
+ if (roleName.isEmpty) {
239
+ throw ArgumentError ('Role name cannot be empty' );
240
+ }
241
+
242
+ _roles.remove (roleName);
243
+ _evaluator = RoleEvaluator (_roles);
244
+ await _storage.savePolicies (_roles);
245
+ notifyListeners ();
246
+ }
247
+
248
+ /// Updates an existing role in the policy manager.
249
+ ///
250
+ /// Replaces the role identified by [roleName] with the new [role] configuration.
251
+ /// The evaluator is updated with the modified role configuration, the updated
252
+ /// policy state is persisted to storage, and listeners are notified of the change.
253
+ ///
254
+ /// If no role exists with the specified [roleName] , a new role is added instead.
255
+ /// This method effectively combines the functionality of [addRole] and [removeRole] .
256
+ ///
257
+ /// [roleName] must not be null or empty.
258
+ /// [role] must not be null and should have a valid name.
259
+ ///
260
+ /// Throws:
261
+ /// - [ArgumentError] if [roleName] is null/empty or [role] is null/invalid
262
+ /// - Storage-related exceptions if persistence fails
263
+ Future <void > updateRole (String roleName, Role role) async {
264
+ if (roleName.isEmpty && role.name.isEmpty) {
265
+ throw ArgumentError ('Role name cannot be empty' );
266
+ }
267
+
268
+ _roles[roleName] = role;
269
+ _evaluator = RoleEvaluator (_roles);
270
+ await _storage.savePolicies (_roles);
271
+ notifyListeners ();
272
+ }
197
273
}
0 commit comments