コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public DbContextHooker(DbContext context, GuardianKernel kernel)
        {
            Ensure.NotNull(context, nameof(context));
            Ensure.NotNull(kernel, nameof(kernel));

            _context = context;
            _kernel  = kernel;
        }
コード例 #4
0
        public static DbContext GuardBy(this DbContext dbContext, GuardianKernel kernel)
        {
            var contextHooker = new DbContextHooker(dbContext, kernel);

            contextHooker.RegisterHooks();

            return(dbContext);
        }
コード例 #5
0
        public DefaultModifyProtector(GuardianKernel kernel)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException(nameof(kernel));
            }

            _kernel = kernel;
        }
コード例 #6
0
        public void TryValidate_WithNullQueryGuard_ShouldTrowExceptions()
        {
            var kernel = new GuardianKernel();

            kernel.QueryGuard = null;
            Assert.Throws <ArgumentNullException>(() =>
            {
                kernel.TryValidate();
            });
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        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;
        }
コード例 #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);
 }
コード例 #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);
 }
コード例 #11
0
        public void SubmitPolicyCollection_ShouldBeInitialized()
        {
            var kernel = new GuardianKernel();

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

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

            kernel.TryValidate();
        }
コード例 #14
0
 public DefaultRetrievePolicyTests()
 {
     _kernel = new GuardianKernel();
     _policy = new DefaultRetrievePolicy();
 }
コード例 #15
0
 public DefaultModifyPolicyTests()
 {
     _kernel = new GuardianKernel();
     _policy = new DefaultModifyPolicy();
 }