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;
        }
Exemple #4
0
        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;
        }
Exemple #6
0
        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;
        }
Exemple #9
0
 /// <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);
 }
Exemple #10
0
 /// <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);
 }
Exemple #11
0
        public void SubmitPolicyCollection_ShouldBeInitialized()
        {
            var kernel = new GuardianKernel();

            Assert.NotEqual(null, kernel.SubmitPolicies);
        }
Exemple #12
0
        public void EnableGuards_ShouldBeEnabled()
        {
            var kernel = new GuardianKernel();

            Assert.Equal(true, kernel.EnableGuards);
        }
Exemple #13
0
        public void TryValidate_ShouldSuccess()
        {
            var kernel = new GuardianKernel();

            kernel.TryValidate();
        }
Exemple #14
0
 public DefaultRetrievePolicyTests()
 {
     _kernel = new GuardianKernel();
     _policy = new DefaultRetrievePolicy();
 }
 public DefaultModifyPolicyTests()
 {
     _kernel = new GuardianKernel();
     _policy = new DefaultModifyPolicy();
 }