private void Attach(Func <DbContextMultiClass> createDbContext)
 {
     using (DbContextMultiClass context = createDbContext()) {
         context.Add(new DbContextObject1()
         {
             ItemCount = 1
         });
         context.Add(new DbContextObject1()
         {
             ItemCount = 2
         });
         context.SaveChanges();
     }
     using (var context = createDbContext()) {
         context.ChangeTracker.AutoDetectChangesEnabled = false;
         var entry1 = context.Attach(new DbContextObject1()
         {
             ID = 1, ItemCount = 4
         });
         Assert.AreEqual(4, entry1.Entity.ItemCount);
         var entry2 = context.Attach(new DbContextObject1()
         {
             ID = 2, ItemCount = 5
         });
         Assert.AreEqual(5, entry2.Entity.ItemCount);
         context.SaveChanges();
     }
     using (DbContextMultiClass context = createDbContext()) {
         Assert.AreEqual(1, context.dbContextDbSet1.Single(p => p.ItemCount == 1).ItemCount);
         Assert.AreEqual(2, context.dbContextDbSet1.Single(p => p.ItemCount == 2).ItemCount);
         Assert.AreEqual(2, context.dbContextDbSet1.Count());
     }
 }
 private static void RemoveMultiClass(Func <DbContextMultiClass> createDbContext)
 {
     using (DbContextMultiClass context = createDbContext()) {
         context.Add(new DbContextObject1()
         {
             ItemName = "1"
         });
         context.Add(new DbContextObject2()
         {
             User = "******"
         });
         context.Add(new DbContextObject3()
         {
             Notes = "3"
         });
         context.SaveChanges();
     }
     using (DbContextMultiClass context = createDbContext()) {
         var itemDbContextObject1 = context.dbContextDbSet1.Single();
         var itemDbContextObject2 = context.dbContextDbSet2.Single();
         var itemDbContextObject3 = context.dbContextDbSet3.Single();
         DbContextObject1.Count = 0;
         context.Remove(itemDbContextObject1);
         context.Remove(itemDbContextObject2);
         context.Remove(itemDbContextObject3);
         context.SaveChanges();
         Assert.AreEqual(0, DbContextObject1.Count);
     }
     using (DbContextMultiClass context = createDbContext()) {
         Assert.AreEqual(0, context.dbContextDbSet1.Count());
         Assert.AreEqual(0, context.dbContextDbSet2.Count());
         Assert.AreEqual(0, context.dbContextDbSet3.Count());
     }
 }
        public void IntKeyPropertyExplicitInitialize(Func <DbContextMultiClass> createDbContext)
        {
            using (DbContextMultiClass context = createDbContext()) {
                context.ResetDatabase();
                DbContextObject4 obj = new DbContextObject4()
                {
                    ID = 4
                };
                context.Add(obj);
                Assert.AreEqual(4, obj.ID);
                context.SaveChanges();
                Assert.AreEqual(4, obj.ID);
            }
            using (DbContextMultiClass context = createDbContext()) {
                Assert.IsNotNull(context.dbContextDbSet1.Where(o => o.ID == 4));
            }

            using (DbContextMultiClass context = createDbContext()) {
                context.Add(new DbContextObject4()
                {
                    ID = 5
                });
                try {
                    context.Add(new DbContextObject4()
                    {
                        ID = 5
                    });
                    Assert.Fail();
                }
                catch (NUnit.Framework.AssertionException) { }
                catch {
                    //System.InvalidOperationException : The instance of entity type 'DevExpress.EntityFramework.DbContextDataStore.Tests.DbContextObject4' cannot be tracked because another instance of this type with the same key is already being tracked. For new entities consider using an IIdentityGenerator to generate unique key values.;
                }
            }
        }
示例#4
0
        public void ReadObjectAllowPermission()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = new DbContextObject1();
                obj1.DecimalItem = 10;
                obj1.Description = "Good description";
                DbContextObject1 obj2 = new DbContextObject1();
                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());

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

                Assert.AreEqual(1, dbContextMultiClass.dbContextDbSet1.Count());
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();
                Assert.IsNull(obj1.Description);
                Assert.AreEqual(10, obj1.DecimalItem);
            }
        }
示例#5
0
        public void ReadMemberAllowPermission()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = new DbContextObject1();
                obj1.DecimalItem = 10;
                obj1.Description = "Good description";
                DbContextObject1 obj2 = new DbContextObject1();
                obj2.DecimalItem = 20;
                obj2.Description = "Not good description";

                dbContextMultiClass.Add(obj1);
                dbContextMultiClass.Add(obj2);
                dbContextMultiClass.SaveChanges();
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                dbContextMultiClass.PermissionsContainer.SetPermissionPolicy(PermissionPolicy.DenyAllByDefault);
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > criteria = (db, obj) => obj.Description != "Good description";
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Read, OperationState.Deny, "DecimalItem", criteria);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > typeCriteria = (db, obj) => true;
                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Read, OperationState.Allow, typeCriteria);

                var query = from d in dbContextMultiClass.dbContextDbSet1
                            orderby d.ID
                            select d.DecimalItem;

                Assert.AreEqual(2, query.Count());

                Decimal obj1Decimal = query.First();
                Assert.AreEqual(10, obj1Decimal);

                Decimal obj2Decimal = query.Last();
                Assert.AreEqual(0, obj2Decimal);   // doesn't work now
            }
        }
        private static void UpdateMultiClass(Func <DbContextMultiClass> createDbContext)
        {
            using (DbContextMultiClass context = createDbContext()) {
                context.Add(new DbContextObject1()
                {
                    ItemName = "1"
                });
                context.Add(new DbContextObject2()
                {
                    User = "******"
                });
                context.Add(new DbContextObject3()
                {
                    Notes = "3"
                });
                context.SaveChanges();
            }
            using (DbContextMultiClass context = createDbContext()) {
                foreach (var item in context.dbContextDbSet1)
                {
                    item.ItemName += "+";
                }
                foreach (var item in context.dbContextDbSet2)
                {
                    item.User += "+";
                }
                foreach (var item in context.dbContextDbSet3)
                {
                    item.Notes += "+";
                }
                context.SaveChanges();
            }
            using (DbContextMultiClass context = createDbContext()) {
                DbContextObject1.Count = 0;
                foreach (var item in context.dbContextDbSet1)
                {
                    Assert.AreEqual(item.ItemName.Length - 1, item.ItemName.IndexOf("+"));
                }

                foreach (var item in context.dbContextDbSet2)
                {
                    Assert.AreEqual(item.User.Length - 1, item.User.IndexOf("+"));
                }
                foreach (var item in context.dbContextDbSet3)
                {
                    Assert.AreEqual(item.Notes.Length - 1, item.Notes.IndexOf("+"));
                }
//                Assert.AreEqual(DbContextObject1.Count, 1);
            }
        }
示例#7
0
        public void WriteMembersMultiplePermissions()
        {
            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> > goodCriteria = (db, obj) => obj.ItemCount > 3;
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Write, OperationState.Allow, "ItemCount", goodCriteria);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > goodCriteria2 = (db, obj) => obj.ItemCount < 9;
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Write, OperationState.Allow, "ItemCount", goodCriteria2);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > badCriteria = (db, obj) => obj.ItemCount == 8;
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Write, OperationState.Deny, "ItemCount", badCriteria);

                obj1.ItemCount = 8;
                SecurityTestHelper.FailSaveChanges(dbContextMultiClass);
                Assert.AreEqual(0, obj1.DecimalItem);

                obj1.ItemCount = 6;
                dbContextMultiClass.SaveChanges();
            }
        }
示例#8
0
        public void WriteMemberDenyPermission()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                dbContextMultiClass.Add(new DbContextObject1());
                dbContextMultiClass.SaveChanges();
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();

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

                obj1.Description = "Good description";
                obj1.DecimalItem = 20;

                dbContextMultiClass.SaveChanges();

                obj1.Description = "Not good description";
                obj1.DecimalItem = 10;

                SecurityTestHelper.FailSaveChanges(dbContextMultiClass);

                obj1.Description = "Good description";
                obj1.DecimalItem = 10;
                dbContextMultiClass.SaveChanges();
            }
        }
示例#9
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());
            }
        }
示例#10
0
        public void ReadObjectMultiplePermissions()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = new DbContextObject1();
                obj1.ItemCount   = 5;
                obj1.DecimalItem = 10;
                DbContextObject1 obj2 = new DbContextObject1();
                obj2.ItemCount   = 8;
                obj2.DecimalItem = 20;
                DbContextObject1 obj3 = new DbContextObject1();
                obj3.ItemCount   = 10;
                obj3.DecimalItem = 30;

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

                dbContextMultiClass.PermissionsContainer.SetPermissionPolicy(PermissionPolicy.DenyAllByDefault);
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > goodCriteria = (db, obj) => obj.ItemCount > 3;
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Read, OperationState.Allow, "DecimalItem", goodCriteria);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > goodCriteria2 = (db, obj) => obj.ItemCount < 9;
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Read, OperationState.Allow, "DecimalItem", goodCriteria2);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > badCriteria = (db, obj) => obj.ItemCount == 8;
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Read, OperationState.Deny, "DecimalItem", badCriteria);

                IEnumerable <DbContextObject1> objects = dbContextMultiClass.dbContextDbSet1.OrderBy(p => p.ID).AsEnumerable();
                Assert.AreEqual(2, objects.Count());

                DbContextObject1 obj1 = objects.ElementAt(0);
                Assert.AreEqual(0, obj1.ItemCount);
                Assert.AreEqual(10, obj1.DecimalItem);

                DbContextObject1 obj2 = objects.ElementAt(1);
                Assert.AreEqual(0, obj2.ItemCount);
                Assert.AreEqual(30, obj2.DecimalItem);
            }
        }
        public void IntKeyPropertyAutoGenerate(Func <DbContextMultiClass> createDbContext)
        {
            using (DbContextMultiClass context = createDbContext()) {
                DbContextObject1 obj1 = new DbContextObject1()
                {
                };
                context.Add(obj1);
                int installID = obj1.ID;
                Assert.IsTrue(installID != 0); //with Native test is passed is started only this test but failed when entire fixture is started: Expected: -1 But was:  -22
                context.SaveChanges();
                // TODO: fix?
                // Assert.AreEqual(installID, obj1.ID); //what is generated and added to
                installID = obj1.ID;
                Assert.IsTrue(installID != 0);
            }
            int installID2;
            int installID3;

            using (DbContextMultiClass context = createDbContext()) {
                DbContextObject1 obj2 = new DbContextObject1();
                DbContextObject1 obj3 = new DbContextObject1();
                context.Add(obj2);
                context.Add(obj3);
                installID2 = obj2.ID;
                installID3 = obj3.ID;
                Assert.IsTrue(installID2 != 0); //with Native test is passed is started only this test but failed when entire fixture is started: Expected: -2 But was:  -22
                Assert.IsTrue(installID3 != 0); //with Native test is passed is started only this test but failed when entire fixture is started: Expected: -3 But was:  -22
                context.SaveChanges();
                // for sql server
                installID2 = obj2.ID;
                installID3 = obj3.ID;
                Assert.IsTrue(
                    ((obj2.ID == installID2) && (obj3.ID == installID3))
                    ||
                    ((obj2.ID == installID3) && (obj3.ID == installID2)), "obj2.ID=" + installID2.ToString() + ", obj3.ID=" + installID3.ToString());
            }
            using (DbContextMultiClass context = createDbContext()) {
                Assert.IsNotNull(context.dbContextDbSet1.Where(o => o.ID == installID2).Single());
                Assert.IsNotNull(context.dbContextDbSet1.Where(o => o.ID == installID3).Single());
            }
        }
示例#12
0
        public void ReadBlockedMembersFromISecurityEntityObject()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                dbContextMultiClass.Database.EnsureCreated();

                DbContextISecurityEntityObject obj1 = new DbContextISecurityEntityObject();
                obj1.DecimalItem = 10;
                obj1.Description = "Good description";
                DbContextISecurityEntityObject obj2 = new DbContextISecurityEntityObject();
                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.dbContextISecurityEntityDbSet.Count());

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

                Assert.AreEqual(2, dbContextMultiClass.dbContextISecurityEntityDbSet.Count());
                DbContextISecurityEntityObject obj1 = dbContextMultiClass.dbContextISecurityEntityDbSet.First(obj => obj.Description == "Good description");
                Assert.AreEqual("Good description", obj1.Description);
                Assert.AreEqual(10, obj1.DecimalItem);

                IEnumerable <string> obj1BlockedMembers = obj1.BlockedMembers;
                Assert.IsNotNull(obj1BlockedMembers);
                Assert.AreEqual(0, obj1BlockedMembers.Count());

                DbContextISecurityEntityObject obj2 = dbContextMultiClass.dbContextISecurityEntityDbSet.First(obj => obj.Description == "Not good description");
                Assert.AreEqual("Not good description", obj2.Description);
                Assert.AreEqual(0, obj2.DecimalItem);

                IEnumerable <string> obj2BlockedMembers = obj2.BlockedMembers;
                Assert.IsNotNull(obj2BlockedMembers);
                Assert.AreEqual(1, obj2BlockedMembers.Count());
                Assert.AreEqual("DecimalItem", obj2BlockedMembers.First());
            }
        }
 public void ReadType()
 {
     using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
         dbContextMultiClass.ResetDatabase();
         dbContextMultiClass.Add(new DbContextObject1());
         dbContextMultiClass.SaveChanges();
     }
     using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
         Assert.IsNotNull(dbContextMultiClass.dbContextDbSet1.FirstOrDefault());
         dbContextMultiClass.PermissionsContainer.SetTypePermission <DbContextObject1>(SecurityOperation.Read, OperationState.Deny);
         Assert.IsNull(dbContextMultiClass.dbContextDbSet1.FirstOrDefault());
     }
 }
 public void CreateType()
 {
     using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
         dbContextMultiClass.ResetDatabase();
         dbContextMultiClass.PermissionsContainer.SetTypePermission <DbContextObject1>(SecurityOperation.Create, OperationState.Deny);
         dbContextMultiClass.Add(new DbContextObject1());
         try {
             dbContextMultiClass.SaveChanges();
             Assert.Fail("Fail");
         }
         catch (Exception e) {
             Assert.AreNotEqual("Fail", e.Message);
         }
     }
 }
示例#15
0
        public void RollbackDeletedObjectDatabaseValuesDenyPolicy()
        {
            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> > deleteCriteria = (db, obj) => obj.Description == "Good description";
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > writeCriteria  = (db, obj) => true;

                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Delete, OperationState.Allow, deleteCriteria);
                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Write, OperationState.Allow, writeCriteria);
                dbContextMultiClass.PermissionsContainer.SetTypePermission(typeof(DbContextObject1), SecurityOperation.Read, OperationState.Allow);
                obj1.Description = "Not good description";
                dbContextMultiClass.SaveChanges();

                dbContextMultiClass.Remove(obj1);

                SecurityTestHelper.FailSaveChanges(dbContextMultiClass);
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();
                obj1.Description = "Good description";

                dbContextMultiClass.SaveChanges();
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();

                dbContextMultiClass.PermissionsContainer.SetPermissionPolicy(PermissionPolicy.DenyAllByDefault);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > deleteCriteria = (db, obj) => obj.Description == "Good description";
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > writeCriteria  = (db, obj) => true;
                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Delete, OperationState.Allow, deleteCriteria);
                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Write, OperationState.Allow, writeCriteria);
                dbContextMultiClass.PermissionsContainer.SetTypePermission(typeof(DbContextObject1), SecurityOperation.Read, OperationState.Allow);

                dbContextMultiClass.Remove(obj1);

                dbContextMultiClass.SaveChanges();
            }
        }
示例#16
0
        public void IsGrantedObjectAndMember()
        {
            using (DbContextMultiClass dbContext = new DbContextMultiClass()) {
                DbContextObject1 dbContextObject1_1 = new DbContextObject1();
                dbContextObject1_1.ItemName = "1";
                dbContext.Add(dbContextObject1_1);
                dbContext.SaveChanges();

                dbContext.PermissionsContainer.SetPermissionPolicy(PermissionPolicy.AllowAllByDefault);
                dbContext.PermissionsContainer.AddObjectPermission <DbContextMultiClass, DbContextObject1>(
                    SecurityOperation.Read,
                    OperationState.Deny,
                    (p, d) => d.ItemName == "1");

                Assert.AreEqual(dbContext.dbContextDbSet1.Count(), 0);
            }
        }
示例#17
0
        public void RollbackMultipleMembersAllowPolicy()
        {
            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.AllowAllByDefault);
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > criteria = (db, obj) => obj.Description != "Good description";
                dbContextMultiClass.PermissionsContainer.AddMemberPermission(SecurityOperation.Write, OperationState.Deny, "Description", criteria);
                // dbContextMultiClass.Security.AddMemberPermission(SecurityOperation.Write, OperationState.Deny, "DecimalItem", criteria);

                obj1.Description = "Good description";
                obj1.DecimalItem = 10;

                Assert.AreEqual(EntityState.Modified, dbContextMultiClass.Entry(obj1).State);
                Assert.IsTrue(dbContextMultiClass.Entry(obj1).Property("Description").IsModified);
                Assert.IsTrue(dbContextMultiClass.Entry(obj1).Property("DecimalItem").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("DecimalItem").IsModified);
                Assert.IsFalse(dbContextMultiClass.Entry(obj1).Property("ItemCount").IsModified);

                obj1.Description = "Not good description";
                obj1.DecimalItem = 20;

                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("DecimalItem").IsModified);
                Assert.IsFalse(dbContextMultiClass.Entry(obj1).Property("ItemCount").IsModified);

                Assert.AreEqual("Good description", obj1.Description);
                Assert.AreEqual(10, obj1.DecimalItem);
            }
        }
 public void WriteType()
 {
     using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
         dbContextMultiClass.ResetDatabase();
         dbContextMultiClass.Add(new DbContextObject1());
         dbContextMultiClass.SaveChanges();
     }
     using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
         dbContextMultiClass.PermissionsContainer.SetTypePermission <DbContextObject1>(SecurityOperation.Write, OperationState.Deny);
         DbContextObject1 obj = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();
         obj.ItemCount = 100;
         try {
             dbContextMultiClass.SaveChanges();
             Assert.Fail("Fail");
         }
         catch (Exception e) {
             Assert.AreNotEqual("Fail", e.Message);
         }
     }
 }
示例#19
0
        public void WriteMemberAllowPermission()
        {
            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);

                obj1.Description = "Good description";
                dbContextMultiClass.SaveChanges();

                obj1.Description = "Not good description";
                SecurityTestHelper.FailSaveChanges(dbContextMultiClass);

                obj1.Description = "Good description";
                dbContextMultiClass.SaveChanges();
            }
        }