示例#1
0
        public void ReadBlockedMembersFromObjectWithMultipleBlockedMembers()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                dbContextMultiClass.Database.EnsureCreated();

                DbContextObject1 obj1 = new DbContextObject1();
                obj1.ItemName    = "Object 1 name";
                obj1.DecimalItem = 10;
                obj1.Description = "Good description";
                DbContextObject1 obj2 = new DbContextObject1();
                obj2.ItemName    = "Object 2 name";
                obj2.DecimalItem = 20;
                obj2.Description = "Not good description";

                dbContextMultiClass.Add(obj1);
                dbContextMultiClass.Add(obj2);
                dbContextMultiClass.SaveChanges();
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                Assert.AreEqual(2, dbContextMultiClass.dbContextDbSet1.Count());

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > badCriteria = (db, obj) => obj.Description == "Not good description";
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Read, OperationState.Deny, "DecimalItem", badCriteria);
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Read, OperationState.Deny, "ItemName", badCriteria);

                Assert.AreEqual(2, dbContextMultiClass.dbContextDbSet1.Count());
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.First(obj => obj.Description == "Good description");
                Assert.AreEqual("Good description", obj1.Description);
                Assert.AreEqual("Object 1 name", obj1.ItemName);
                Assert.AreEqual(10, obj1.DecimalItem);

                IList <string> obj1BlockedMembers = dbContextMultiClass.Entry(obj1).GetBlockedMembers();
                Assert.IsNotNull(obj1BlockedMembers);
                Assert.AreEqual(0, obj1BlockedMembers.Count());

                DbContextObject1 obj2 = dbContextMultiClass.dbContextDbSet1.First(obj => obj.Description == "Not good description");
                Assert.AreEqual("Not good description", obj2.Description);
                Assert.AreEqual(null, obj2.ItemName);
                Assert.AreEqual(0, obj2.DecimalItem);

                IList <string> obj2BlockedMembers = dbContextMultiClass.Entry(obj2).GetBlockedMembers();
                Assert.IsNotNull(obj2BlockedMembers);
                Assert.AreEqual(2, obj2BlockedMembers.Count());
                Assert.AreEqual("DecimalItem", obj2BlockedMembers.First());
                Assert.AreEqual("ItemName", obj2BlockedMembers.Last());
            }
        }
        private void Entry(Func <DbContextMultiClass> createDbContext)
        {
            var itemMold = new DbContextObject1()
            {
                ItemName = "1"
            };

            using (DbContextMultiClass context = createDbContext()) {
                var item = context.Entry(itemMold);
                Assert.AreNotEqual(item, null);
            }
        }
示例#3
0
        public void RollbackMemberDenyPolicy()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                dbContextMultiClass.Add(new DbContextObject1());
                dbContextMultiClass.SaveChanges();
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();

                dbContextMultiClass.PermissionsContainer.SetPermissionPolicy(PermissionPolicy.DenyAllByDefault);
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > criteria = (db, obj) => obj.Description == "Good description";
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Write, OperationState.Allow, "Description", criteria);
                dbContextMultiClass.PermissionsContainer.SetTypePermission <DbContextObject1>(SecurityOperation.Read, OperationState.Allow);
                obj1.Description = "Good description";

                Assert.AreEqual(EntityState.Modified, dbContextMultiClass.Entry(obj1).State);
                Assert.IsTrue(dbContextMultiClass.Entry(obj1).Property("Description").IsModified);
                Assert.IsFalse(dbContextMultiClass.Entry(obj1).Property("ItemCount").IsModified);

                dbContextMultiClass.SaveChanges();

                Assert.AreEqual(EntityState.Unchanged, dbContextMultiClass.Entry(obj1).State);
                Assert.IsFalse(dbContextMultiClass.Entry(obj1).Property("Description").IsModified);
                Assert.IsFalse(dbContextMultiClass.Entry(obj1).Property("ItemCount").IsModified);

                obj1.Description = "Not good description";

                SecurityTestHelper.FailSaveChanges(dbContextMultiClass);

                Assert.AreEqual(EntityState.Unchanged, dbContextMultiClass.Entry(obj1).State);
                Assert.IsFalse(dbContextMultiClass.Entry(obj1).Property("Description").IsModified);
                Assert.IsFalse(dbContextMultiClass.Entry(obj1).Property("ItemCount").IsModified);

                Assert.AreEqual("Good description", obj1.Description);
            }
        }