private void SyncClaimRequirements( IDynamicPolicyDbContext db, List <ClaimRequirement> requiredClaims, AuthorizationPolicyEntity entity) { if (requiredClaims.Count == 0) { entity.RequiredClaims.Clear(); } else { if (entity.RequiredClaims.Count > 0) { for (int i = 0; i < entity.RequiredClaims.Count; i++) { if (!requiredClaims.HasClaim(entity.RequiredClaims[i].ClaimName)) { db.ClaimRequirements.Remove(entity.RequiredClaims[i]); entity.RequiredClaims.RemoveAt(i); } } } foreach (var c in requiredClaims) { var found = entity.RequiredClaims.FindClaim(c.ClaimName); if (found != null) { //c.SyncTo(found); SyncClaimAllowedValues(db, c, found); } else { var newClaim = new ClaimRequirementEntity(); newClaim.ClaimName = c.ClaimName; newClaim.Policy = entity; // c.SyncTo(newClaim); SyncClaimAllowedValues(db, c, newClaim); entity.RequiredClaims.Add(newClaim); db.ClaimRequirements.Add(newClaim); } } } }
private void SyncRoles( IDynamicPolicyDbContext db, List <string> allowedRoleStrings, //List<AllowedRoleEntity> allowedRoles, AuthorizationPolicyEntity entity) { if (allowedRoleStrings.Count == 0) { entity.AllowedRoles.Clear(); } else { if (entity.AllowedRoles.Count > 0) { for (int i = 0; i < entity.AllowedRoles.Count; i++) { if (!allowedRoleStrings.Contains(entity.AllowedRoles[i].AllowedRole)) { db.AllowedRoles.Remove(entity.AllowedRoles[i]); entity.AllowedRoles.RemoveAt(i); } } } foreach (var s in allowedRoleStrings) { if (!entity.AllowedRoles.HasRole(s)) { var r = new AllowedRoleEntity(); r.AllowedRole = s; r.Policy = entity; entity.AllowedRoles.Add(r); db.AllowedRoles.Add(r); } } } }
private static void SyncSchemes( IDynamicPolicyDbContext db, List <string> authSchemeStrings, AuthorizationPolicyEntity entity) { if (authSchemeStrings.Count == 0) { entity.AuthenticationSchemes.Clear(); } else { if (entity.AuthenticationSchemes.Count > 0) { for (int i = 0; i < entity.AuthenticationSchemes.Count; i++) { if (!authSchemeStrings.Contains(entity.AuthenticationSchemes[i].AuthenticationScheme)) { db.AuthenticationSchemes.Remove(entity.AuthenticationSchemes[i]); entity.AuthenticationSchemes.RemoveAt(i); } } } foreach (var s in authSchemeStrings) { if (!entity.AuthenticationSchemes.HasScheme(s)) { var r = new AuthenticationSchemeEntity(); r.AuthenticationScheme = s; r.Policy = entity; entity.AuthenticationSchemes.Add(r); db.AuthenticationSchemes.Add(r); } } } }
private void SyncClaimAllowedValues( IDynamicPolicyDbContext db, ClaimRequirement claim, ClaimRequirementEntity entity) { if (claim.AllowedValues.Count == 0) { entity.AllowedValues.Clear(); } else { if (entity.AllowedValues.Count > 0) { for (int i = 0; i < entity.AllowedValues.Count; i++) { if (!claim.AllowedValues.Contains(entity.AllowedValues[i].AllowedValue)) { db.AllowedClaimValues.Remove(entity.AllowedValues[i]); entity.AllowedValues.RemoveAt(i); } } } foreach (var s in claim.AllowedValues) { if (!entity.AllowedValues.HasRequiredValue(s)) { var r = new AllowedClaimValueEntity(); r.AllowedValue = s; r.ClaimRequirement = entity; entity.AllowedValues.Add(r); db.AllowedClaimValues.Add(r); } } } }