public bool Check(ModifyPolicyContext context, GuardianKernel kernel) { var isSuccess = true; var permissions = kernel.Permissions; var generalPermissions = permissions.GetGeneralPermissions(context.EntityTypeName, context.AccessType); var rowLevelPermissions = permissions.GetRowLevelPermissions( context.EntityTypeName, context.EntityRowKey, context.AccessType); var columnLevelRestrictions = permissions.GetColumnLevelRestrictions(context.EntityTypeName, context.AccessType); if (generalPermissions.Any() == false && rowLevelPermissions.Any() == false) { isSuccess = false; return(isSuccess); } foreach (var propName in context.ModifiedProperties) { var restrictionExist = columnLevelRestrictions.Any(x => x.PropertyName == propName); if (restrictionExist) { isSuccess = false; // If Restriction exist it means that our policy faild break; } } return(isSuccess); }
public bool Check(RetrievePolicyContext context, GuardianKernel kernel) { var isSuccess = true; context.Entity.ProtectedProperties = new List <string>(); context.Entity.ProtectionResult = ProtectionResults.Allow; var permissions = kernel.Permissions; var generalPermissions = permissions.GetGeneralPermissions(context.EntityTypeName, AccessTypes.Get); var rowLevelPermissions = permissions.GetRowLevelPermissions( context.EntityTypeName, context.EntityRowKey, AccessTypes.Get); var columnLevelRestrictions = permissions.GetColumnLevelRestrictions(context.EntityTypeName, AccessTypes.Get); if (generalPermissions.Any() == false && rowLevelPermissions.Any() == false) { context.Entity.ProtectionResult = ProtectionResults.Deny; isSuccess = false; return(isSuccess); } foreach (var columnRestriction in columnLevelRestrictions) { context.Entity.ProtectedProperties.Add(columnRestriction.PropertyName); } return(isSuccess); }
public DbContextHooker(DbContext context, GuardianKernel kernel) { Ensure.NotNull(context, nameof(context)); Ensure.NotNull(kernel, nameof(kernel)); _context = context; _kernel = kernel; }
public static DbContext GuardBy(this DbContext dbContext, GuardianKernel kernel) { var contextHooker = new DbContextHooker(dbContext, kernel); contextHooker.RegisterHooks(); return(dbContext); }
public DefaultModifyProtector(GuardianKernel kernel) { if (kernel == null) { throw new ArgumentNullException(nameof(kernel)); } _kernel = kernel; }
public void TryValidate_WithNullQueryGuard_ShouldTrowExceptions() { var kernel = new GuardianKernel(); kernel.QueryGuard = null; Assert.Throws <ArgumentNullException>(() => { kernel.TryValidate(); }); }
/// <summary> /// Guards the <see cref="DbContext"/> by <see cref="GuardianKernel"/> . /// </summary> /// <param name="dbContext">The database context to Guard.</param> /// <param name="kernel">The guardian kernel.</param> public static DbContext UseGuardian(this DbContext dbContext, GuardianKernel kernel) { kernel.TryValidate(); var contextHooker = new DbContextHooker(dbContext, kernel); contextHooker.RegisterHooks(); return(dbContext); }
public DbContextHooker(DbContext context, GuardianKernel kernel) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (kernel == null) { throw new ArgumentNullException(nameof(kernel)); } _context = context; _kernel = kernel; }
/// <summary> /// Adds policy to GuardianKernel. /// </summary> /// <param name="kernel">The kernel.</param> /// <param name="policy">The policy.</param> /// <returns></returns> public static GuardianKernel UsePolicy(this GuardianKernel kernel, ISubmitPolicy policy) { kernel.SubmitPolicies.Add(policy); return(kernel); }
/// <summary> /// Adds policy to GuardianKernel. /// </summary> /// <param name="kernel">The kernel.</param> /// <param name="policy">The policy.</param> /// <returns></returns> public static GuardianKernel UsePolicy(this GuardianKernel kernel, IQueryPolicy policy) { kernel.QueryPolicies.Add(policy); return(kernel); }
public void SubmitPolicyCollection_ShouldBeInitialized() { var kernel = new GuardianKernel(); Assert.NotEqual(null, kernel.SubmitPolicies); }
public void EnableGuards_ShouldBeEnabled() { var kernel = new GuardianKernel(); Assert.Equal(true, kernel.EnableGuards); }
public void TryValidate_ShouldSuccess() { var kernel = new GuardianKernel(); kernel.TryValidate(); }
public DefaultRetrievePolicyTests() { _kernel = new GuardianKernel(); _policy = new DefaultRetrievePolicy(); }
public DefaultModifyPolicyTests() { _kernel = new GuardianKernel(); _policy = new DefaultModifyPolicy(); }