This example shows how to guard privileged role changes with policy checks and audit friendly context.
It is the most governance focused sample in the repository. Use it when you want to see how to protect sensitive operations such as granting admin rights, revoking permissions, or migrating many roles at once.
The domain state maps user IDs to a set of roles.
The example covers three workflows:
- granting role to user
- revoking role from user
- batch migration of role assignments
- privilege changing mutations
- last admin protection
- approval metadata in
MutationContext - two man approval for critical mutations
- batch role operations across multiple users
Program.csIamRoles.csprojState/UserPermissionsState.csMutations/GrantUserRoleMutation.csMutations/RevokeUserRoleMutation.csPolicies/PreventLastAdminRemovalPolicy.csPolicies/RequireTwoManApprovalPolicy.csScenarios/GrantAdminScenario.csScenarios/RevokeAdminScenario.csScenarios/BatchRoleMigrationScenario.cs
Program.cs:
- registers the engine with strict options
- resolves
IMutationEngine - registers the two governance policies
- runs the three role management scenarios
- prints summary statistics
The sample keeps the domain model simple so the policies stay visible.
GrantUserRoleMutation grants role to user.
- validates the user ID and role name
- rejects duplicate assignments
- writes the role into copied dictionary
- emits state change for the affected user
RevokeUserRoleMutation removes role from user.
- checks that the user actually has the role
- removes the role from copied state
- emits removal change
PreventLastAdminRemovalPolicy blocks the removal of the last remaining Admin role.
This shows classic safety rule for sensitive systems.
RequireTwoManApprovalPolicy requires additional approval metadata for critical changes.
The policy demonstrates:
- inspecting mutation risk level
- reading the approval list from metadata
- rejecting self approval
GrantAdminScenario promotes user to admin.
RevokeAdminScenario removes admin from user.
BatchRoleMigrationScenario applies multiple role grants in single batch.
It demonstrates:
- batch execution
- state driven mutation generation
- policy evaluation on each item
- reporting per mutation failures
State/UserPermissionsState.csMutations/GrantUserRoleMutation.csMutations/RevokeUserRoleMutation.csPolicies/PreventLastAdminRemovalPolicy.csPolicies/RequireTwoManApprovalPolicy.csScenarios/BatchRoleMigrationScenario.cs
dotnet run --project Examples/Core/IamRoles/IamRoles.csprojWhen you run the sample, you should see:
- a single grant flow
- a single revoke flow
- a batch migration flow
- blocked or approved mutations depending on policy input
- final statistics from the engine
This example is the clearest one to study if you care about policy controlled privilege management.