public void FilterNoPermissions()
        {
            using (var container = new RhetosTestContainer())
            {
                var repositories = container.Resolve<Common.DomRepository>();
                var itemsRepository = repositories.TestRowPermissions.RPRulesItem;
                var groupsRepository = repositories.TestRowPermissions.RPRulesGroup;
                itemsRepository.Delete(itemsRepository.All());
                groupsRepository.Delete(groupsRepository.All());

                var g1 = new RPRulesGroup { ID = Guid.NewGuid(), Name = "g1" };
                var g2 = new RPRulesGroup { ID = Guid.NewGuid(), Name = "g2" };
                var i1 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i1", GroupID = g1.ID };
                var i2 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i2", GroupID = g1.ID };
                var i3 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i3", GroupID = g2.ID };
                var i4 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i4", GroupID = g2.ID };

                groupsRepository.Insert(new[] { g1, g2 });
                itemsRepository.Insert(new[] { i1, i2, i3, i4 });

                var allowedItems = itemsRepository.Filter(itemsRepository.Query(), new Common.RowPermissionsReadItems());
                Console.WriteLine(itemsRepository.Query().Expression.ToString());
                Console.WriteLine(allowedItems.Expression.ToString());
                Assert.AreEqual("", TestUtility.DumpSorted(allowedItems, item => item.Name));
                Assert.AreEqual("Common.Queryable.TestRowPermissions_RPRulesItem[]", allowedItems.Expression.ToString(), "No need for query, an empty array should be returned.");
            }
        }
        public void FilterOptimizeAllowedAll()
        {
            using (var container = new RhetosTestContainer())
            {
                var currentUserName  = container.Resolve <IUserInfo>().UserName;
                var repositories     = container.Resolve <Common.DomRepository>();
                var itemsRepository  = repositories.TestRowPermissions.RPRulesItem;
                var groupsRepository = repositories.TestRowPermissions.RPRulesGroup;
                itemsRepository.Delete(itemsRepository.Query());
                groupsRepository.Delete(groupsRepository.Query());

                var g1 = new RPRulesGroup {
                    ID = Guid.NewGuid(), Name = "g1"
                };
                var g2 = new RPRulesGroup {
                    ID = Guid.NewGuid(), Name = "g2"
                };
                var i1 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i1", GroupID = g1.ID
                };
                var i2 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i2", GroupID = g1.ID
                };
                var i3 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i3", GroupID = g2.ID
                };
                var i4 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i4", GroupID = g2.ID
                };

                groupsRepository.Insert(new[] { g1, g2 });
                itemsRepository.Insert(new[] { i1, i2, i3, i4 });

                repositories.TestRowPermissions.RpRulesAllowGroup.Insert(new[] {
                    new TestRowPermissions.RpRulesAllowGroup {
                        UserName = currentUserName, GroupID = g1.ID
                    },
                    new TestRowPermissions.RpRulesAllowGroup {
                        UserName = currentUserName, GroupID = g2.ID
                    }
                });

                repositories.TestRowPermissions.RpRulesAllowItem.Insert(new[] {
                    new TestRowPermissions.RpRulesAllowItem {
                        UserName = currentUserName, ItemID = i1.ID
                    },
                    new TestRowPermissions.RpRulesAllowItem {
                        UserName = currentUserName, ItemID = i2.ID
                    }
                });

                var allowedItems = itemsRepository.Filter(itemsRepository.Query(), new Common.RowPermissionsReadItems());
                Console.WriteLine(itemsRepository.Query().Expression.ToString());
                Console.WriteLine(allowedItems.Expression.ToString());
                Assert.AreEqual("i1, i2, i3, i4", TestUtility.DumpSorted(allowedItems, item => item.Name));
                Assert.AreEqual(itemsRepository.Query().Expression.ToString(), allowedItems.Expression.ToString(), "'AllowedAllGroups' rule should result with an optimized query without the 'where' part.");
            }
        }
        public void FilterWithPermissions()
        {
            using (var scope = TestScope.Create())
            {
                var currentUserName  = scope.Resolve <IUserInfo>().UserName;
                var repositories     = scope.Resolve <Common.DomRepository>();
                var itemsRepository  = repositories.TestRowPermissions.RPRulesItem;
                var groupsRepository = repositories.TestRowPermissions.RPRulesGroup;
                itemsRepository.Delete(itemsRepository.Query());
                groupsRepository.Delete(groupsRepository.Query());

                var g1 = new RPRulesGroup {
                    ID = Guid.NewGuid(), Name = "g1"
                };
                var g2 = new RPRulesGroup {
                    ID = Guid.NewGuid(), Name = "g2"
                };
                var i1 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i1", GroupID = g1.ID
                };
                var i2 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i2", GroupID = g1.ID
                };
                var i3 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i3", GroupID = g2.ID
                };
                var i4 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i4", GroupID = g2.ID
                };

                groupsRepository.Insert(new[] { g1, g2 });
                itemsRepository.Insert(new[] { i1, i2, i3, i4 });

                repositories.TestRowPermissions.RpRulesAllowGroup.Insert(new[] {
                    new TestRowPermissions.RpRulesAllowGroup {
                        UserName = currentUserName, GroupID = g2.ID
                    }
                });

                repositories.TestRowPermissions.RpRulesAllowItem.Insert(new[] {
                    new TestRowPermissions.RpRulesAllowItem {
                        UserName = currentUserName, ItemID = i2.ID
                    }
                });

                repositories.TestRowPermissions.RpRulesDenyItem.Insert(new[] {
                    new TestRowPermissions.RpRulesDenyItem {
                        UserName = currentUserName, ItemID = i3.ID
                    }
                });

                var allowedItems = itemsRepository.Filter(itemsRepository.Query(), new Common.RowPermissionsReadItems());
                Console.WriteLine(itemsRepository.Query().Expression.ToString());
                Console.WriteLine(allowedItems.Expression.ToString());
                Assert.AreEqual("i2, i4", TestUtility.DumpSorted(allowedItems, item => item.Name));
            }
        }
        public void FilterNoPermissions()
        {
            using (var container = new RhetosTestContainer())
            {
                var repositories     = container.Resolve <Common.DomRepository>();
                var itemsRepository  = repositories.TestRowPermissions.RPRulesItem;
                var groupsRepository = repositories.TestRowPermissions.RPRulesGroup;
                itemsRepository.Delete(itemsRepository.Query());
                groupsRepository.Delete(groupsRepository.Query());

                var g1 = new RPRulesGroup {
                    ID = Guid.NewGuid(), Name = "g1"
                };
                var g2 = new RPRulesGroup {
                    ID = Guid.NewGuid(), Name = "g2"
                };
                var i1 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i1", GroupID = g1.ID
                };
                var i2 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i2", GroupID = g1.ID
                };
                var i3 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i3", GroupID = g2.ID
                };
                var i4 = new RPRulesItem {
                    ID = Guid.NewGuid(), Name = "i4", GroupID = g2.ID
                };

                groupsRepository.Insert(new[] { g1, g2 });
                itemsRepository.Insert(new[] { i1, i2, i3, i4 });

                var allowedItems = itemsRepository.Filter(itemsRepository.Query(), new Common.RowPermissionsReadItems());
                Console.WriteLine(itemsRepository.Query().Expression.ToString());
                Console.WriteLine(allowedItems.Expression.ToString());
                Assert.AreEqual("", TestUtility.DumpSorted(allowedItems, item => item.Name));
                Assert.AreEqual("Common.Queryable.TestRowPermissions_RPRulesItem[]", allowedItems.Expression.ToString(), "No need for query, an empty array should be returned.");
            }
        }
        public void FilterWithPermissions()
        {
            using (var container = new RhetosTestContainer())
            {
                var currentUserName = container.Resolve<IUserInfo>().UserName;
                var repositories = container.Resolve<Common.DomRepository>();
                var itemsRepository = repositories.TestRowPermissions.RPRulesItem;
                var groupsRepository = repositories.TestRowPermissions.RPRulesGroup;
                itemsRepository.Delete(itemsRepository.All());
                groupsRepository.Delete(groupsRepository.All());

                var g1 = new RPRulesGroup { ID = Guid.NewGuid(), Name = "g1" };
                var g2 = new RPRulesGroup { ID = Guid.NewGuid(), Name = "g2" };
                var i1 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i1", GroupID = g1.ID };
                var i2 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i2", GroupID = g1.ID };
                var i3 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i3", GroupID = g2.ID };
                var i4 = new RPRulesItem { ID = Guid.NewGuid(), Name = "i4", GroupID = g2.ID };

                groupsRepository.Insert(new[] { g1, g2 });
                itemsRepository.Insert(new[] { i1, i2, i3, i4 });

                repositories.TestRowPermissions.RpRulesAllowGroup.Insert(new[] {
                    new TestRowPermissions.RpRulesAllowGroup { UserName = currentUserName, GroupID = g2.ID } });

                repositories.TestRowPermissions.RpRulesAllowItem.Insert(new[] {
                    new TestRowPermissions.RpRulesAllowItem { UserName = currentUserName, ItemID = i2.ID } });

                repositories.TestRowPermissions.RpRulesDenyItem.Insert(new[] {
                    new TestRowPermissions.RpRulesDenyItem { UserName = currentUserName, ItemID = i3.ID } });

                var allowedItems = itemsRepository.Filter(itemsRepository.Query(), new Common.RowPermissionsReadItems());
                Console.WriteLine(itemsRepository.Query().Expression.ToString());
                Console.WriteLine(allowedItems.Expression.ToString());
                Assert.AreEqual("i2, i4", TestUtility.DumpSorted(allowedItems, item => item.Name));
            }
        }