Exemple #1
0
        public void AddCommandTest(bool isAdmin)
        {
            using (var modelContext = new TestModelContext()) {
                var userId = 100;
                var now    = DateTime.Now;
                var entity = new DivisionInfo {
                    DivisionID = 1
                };
                var securityContext = new TestSecurityContext(userId, isAdmin);
                var command         = new AddCommand <DivisionInfo> (modelContext, securityContext);

                command.Add(entity, now);

                var entityLoaded = modelContext
                                   .QueryWhere <DivisionInfo> (d => d.DivisionID == entity.DivisionID)
                                   .SingleOrDefault();

                Assert.Equal(isAdmin, null != entityLoaded);

                if (entityLoaded != null)
                {
                    Assert.Equal(now, entityLoaded.CreatedOnDate);
                    Assert.Equal(now, entityLoaded.LastModifiedOnDate);
                    Assert.Equal(userId, entityLoaded.CreatedByUserId);
                    Assert.Equal(userId, entityLoaded.LastModifiedByUserId);
                }
            }
        }
        private Dictionary <PermissionTypeBase, int> PermissionQueryWithLinq_GetRelatedPermissions(
            TestSecurityContext ctx, int entityId, int identityId, PermissionLevel level)
        {
            var counters        = new int[PermissionTypeBase.PermissionCount];
            var permissionTypes = PermissionTypeBase.GetPermissionTypes();

            void CountBits(ulong bits)
            {
                var mask = 1uL;
                var b    = bits;

                foreach (var pt in permissionTypes)
                {
                    if ((b & mask) > 0)
                    {
                        counters[pt.Index]++;
                    }
                    mask = mask << 1;
                }
            }

            var identities = new[] { identityId };

            foreach (var change in SecurityQuery.Subtree(ctx)
                     .GetPermissionChanges(entityId, identities, BreakOptions.StopAtParentBreak)
                     .Where(x => x.EntryType == EntryType.Normal))
            {
                switch (level)
                {
                case PermissionLevel.Allowed:
                    CountBits(change.ChangedBits.AllowBits);
                    break;

                case PermissionLevel.Denied:
                    CountBits(change.ChangedBits.DenyBits);
                    break;

                case PermissionLevel.AllowedOrDenied:
                    CountBits(change.ChangedBits.AllowBits);
                    CountBits(change.ChangedBits.DenyBits);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(level), level, null);
                }
            }

            var result = new Dictionary <PermissionTypeBase, int>();

            for (var i = 0; i < PermissionTypeBase.PermissionCount; i++)
            {
                result.Add(PermissionTypeBase.GetPermissionTypeByIndex(i), counters[i]);
            }

            return(result);
        }
 /* ============================================================================= Tools */
 private void AddPermissionsForIdentityTests(TestSecurityContext ctx)
 {
     ctx.CreateAclEditor()
     // additions for easy checking of differences between parent-chain and the subtree
     .Allow(Id("E1"), Id("U4"), false, PermissionType.Custom04)
     .Allow(Id("E38"), Id("U5"), false, PermissionType.Custom04)
     // additions for validating local permissions.
     .Allow(Id("E1"), Id("G6"), true, PermissionType.Custom04)
     .Allow(Id("E38"), Id("G7"), true, PermissionType.Custom04)
     .Apply();
 }
        public void HandleQuery()
        {
            var securityContext = new TestSecurityContext();
            var query           = new SimpleQuery();

            IQueryResponse queryResponse = null;

            queryResponse = _cqrsLogicHandler.Handle(securityContext, query);

            Assert.IsNotNull(queryResponse);
        }
        public void HandleEvent()
        {
            var securityContext = new TestSecurityContext();
            var @event          = new SimpleEvent();

            // Reset handle switch
            _simpleEventHandler.DidHandle = false;

            _cqrsLogicHandler.Handle(securityContext, @event);

            Assert.IsTrue(_simpleEventHandler.DidHandle);
        }
        public void HandleCommand()
        {
            var securityContext = new TestSecurityContext();
            var command         = new SimpleCommand();

            // Reset handle switch
            _simpleCommandHandler.DidHandle = false;

            var commandResponse = _cqrsLogicHandler.Handle(securityContext, command);

            Assert.IsTrue(_simpleCommandHandler.DidHandle);
        }
        public void DeleteCommandTest(bool isAdmin)
        {
            using (var modelContext = new TestModelContext()) {
                var entity = new DivisionInfo {
                    DivisionID = 1
                };
                modelContext.Add(entity);

                var securityContext = new TestSecurityContext(1, isAdmin);
                var command         = new DeleteCommand <DivisionInfo> (modelContext, securityContext);

                command.Delete(entity);

                Assert.Equal(isAdmin, null == modelContext
                             .QueryOne <DivisionInfo> (d => d.DivisionID == entity.DivisionID)
                             .SingleOrDefault());
            }
        }
        private void AddPermissionsForCategorySelectionTests(TestSecurityContext ctx)
        {
            ctx.CreateAclEditor()
            // additions for easy checking of differences between parent-chain and the subtree
            .Allow(Id("E1"), Id("U4"), false, PermissionType.Custom04)
            .Allow(Id("E38"), Id("U5"), false, PermissionType.Custom04)
            // additions for validating local permissions.
            .Allow(Id("E1"), Id("G6"), false, PermissionType.Custom04)
            .Allow(Id("E38"), Id("G7"), false, PermissionType.Custom04)
            .Apply();

            // add some sharing related entries
            ctx.CreateAclEditor(EntryType.Sharing)
            .Allow(Id("E4"), Id("U6"), false, PermissionType.Custom04)
            .Allow(Id("E39"), Id("U7"), false, PermissionType.Custom04)
            .Allow(Id("E4"), Id("G8"), false, PermissionType.Custom04)
            .Allow(Id("E39"), Id("G9"), false, PermissionType.Custom04)
            .Apply();
        }
        private void AddPermissionsForIdentityByPermissionTests(TestSecurityContext ctx)
        {
            var p1 = PermissionType.Custom11;
            var p2 = PermissionType.Custom12;

            ctx.CreateAclEditor()
            .Allow(Id("E1"), Id("U1"), false, p1)
            .Allow(Id("E1"), Id("U2"), false, p2)
            .Allow(Id("E1"), Id("U3"), false, p1, p2)
            .Allow(Id("E1"), Id("G1"), false, p1)
            .Allow(Id("E1"), Id("G2"), false, p2)
            .Allow(Id("E1"), Id("G3"), false, p1, p2)
            .Allow(Id("E38"), Id("U4"), false, p1)
            .Allow(Id("E38"), Id("U5"), false, p2)
            .Allow(Id("E38"), Id("U6"), false, p1, p2)
            .Allow(Id("E38"), Id("G4"), false, p1)
            .Allow(Id("E38"), Id("G5"), false, p2)
            .Allow(Id("E38"), Id("G6"), false, p1, p2)
            .Apply();
        }
Exemple #10
0
        private static void RunMOVE(ProgramArguments arguments)
        {
            var entities    = SystemStartTests.CreateTestEntities();
            var groups      = SystemStartTests.CreateTestGroups();
            var memberships = Tools.CreateInMemoryMembershipTable(groups);
            var aces        = SystemStartTests.CreateTestAces();

            aces.Add(new StoredAce {
                EntityId = Id("E1"), IdentityId = Id("U3"), LocalOnly = false, AllowBits = 0x100000000, DenyBits = 0x000
            });

            var storage = new DatabaseStorage {
                Aces = aces, Memberships = memberships, Entities = entities, Messages = new List <Tuple <int, DateTime, byte[]> >()
            };

            StartTheSystem(new MemoryDataProvider(storage));

            var ctx = new TestSecurityContext(TestUser.User3);
            var ok  = ctx.HasPermission(52, PermissionType.Custom01);

            started = DateTime.UtcNow;

            Enumerable.Range(1, 4).Select(x => Task.Run(() => MOVEExercise(x))).ToArray();
        }
        private Dictionary <PermissionTypeBase, int> PermissionQueryWithLinq_GetRelatedPermissions_OLD(
            TestSecurityContext ctx, int entityId, int identityId, PermissionLevel level)
        {
            var counters        = new int[PermissionTypeBase.PermissionCount];
            var permissionTypes = PermissionTypeBase.GetPermissionTypes();

            void CountBits(ulong bits)
            {
                var mask = 1uL;
                var b    = bits;

                foreach (var pt in permissionTypes)
                {
                    if ((b & mask) > 0)
                    {
                        counters[pt.Index]++;
                    }
                    mask = mask << 1;
                }
            }

            //var identities = new TestSecurityContext(new TestUser { Id = identityId }).GetGroups();
            //identities.Add(identityId);
            var identities = new[] { identityId };

            var aces = SecurityQuery.ParentChain(ctx).GetEntities(entityId, BreakOptions.StopAtParentBreak)
                       .Where(e => e.Acl != null)                              // relevant entities
                       .SelectMany(e => e.Acl.Entries)                         // join
                       .Where(e => identities.Contains(e.IdentityId) &&        // identity filter
                              !e.LocalOnly &&                                  // local only entry is not affected on the parent chain
                              e.EntryType == EntryType.Normal)                 // only the normal entries are relevant
                       .Union(SecurityQuery.Subtree(ctx).GetEntities(entityId) // do not stop at breaks
                              .Where(e => e.Acl != null)                       // relevant entities
                              .SelectMany(e => e.Acl.Entries)                  // join
                              .Where(e => identities.Contains(e.IdentityId) && // identity filter
                                     e.EntryType == EntryType.Normal)          // only the normal entries are relevant
                              );

            foreach (var ace in aces)
            {
                switch (level)
                {
                case PermissionLevel.Allowed:
                    CountBits(ace.AllowBits);
                    break;

                case PermissionLevel.Denied:
                    CountBits(ace.DenyBits);
                    break;

                case PermissionLevel.AllowedOrDenied:
                    CountBits(ace.AllowBits);
                    CountBits(ace.DenyBits);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(level), level, null);
                }
            }

            var result = new Dictionary <PermissionTypeBase, int>();

            for (var i = 0; i < PermissionTypeBase.PermissionCount; i++)
            {
                result.Add(PermissionTypeBase.GetPermissionTypeByIndex(i), counters[i]);
            }

            return(result);
        }
Exemple #12
0
        private static TestEntity CreateEntity(Dictionary <int, TestEntity> repository, TestSecurityContext context, string name, string parentName, TestUser owner)
        {
            var entity = new TestEntity
            {
                Id      = GetId(name),
                Name    = name,
                OwnerId = owner == null ? default(int) : owner.Id,
                Parent  = parentName == null ? null : repository[GetId(parentName)],
            };

            repository.Add(entity.Id, entity);
            context.CreateSecurityEntity(entity);
            return(entity);
        }
Exemple #13
0
        internal static Dictionary <int, TestEntity> CreateRepository(TestSecurityContext context)
        {
            TestEntity e;
            var        u1         = TestUser.User1;
            var        repository = new Dictionary <int, TestEntity>();

            e = CreateEntity(repository, context, "E1", null, u1);
            {
                e = CreateEntity(repository, context, "E2", "E1", u1);
                {
                    e = CreateEntity(repository, context, "E5", "E2", u1);
                    {
                        e = CreateEntity(repository, context, "E14", "E5", u1);
                        {
                            e = CreateEntity(repository, context, "E50", "E14", u1);
                            {
                                e = CreateEntity(repository, context, "E51", "E50", u1);
                                {
                                    e = CreateEntity(repository, context, "E52", "E51", u1);
                                }
                                e = CreateEntity(repository, context, "E53", "E50", u1);
                            }
                        }
                        e = CreateEntity(repository, context, "E15", "E5", u1);
                    }
                    e = CreateEntity(repository, context, "E6", "E2", u1);
                    {
                        e = CreateEntity(repository, context, "E16", "E6", u1);
                        e = CreateEntity(repository, context, "E17", "E6", u1);
                    }
                    e = CreateEntity(repository, context, "E7", "E2", u1);
                    {
                        e = CreateEntity(repository, context, "E18", "E7", u1);
                        e = CreateEntity(repository, context, "E19", "E7", u1);
                    }
                }
                e = CreateEntity(repository, context, "E3", "E1", u1);
                {
                    e = CreateEntity(repository, context, "E8", "E3", u1);
                    {
                        e = CreateEntity(repository, context, "E20", "E8", u1);
                        e = CreateEntity(repository, context, "E21", "E8", u1);
                        {
                            e = CreateEntity(repository, context, "E22", "E21", u1);
                            e = CreateEntity(repository, context, "E23", "E21", u1);
                            e = CreateEntity(repository, context, "E24", "E21", u1);
                            e = CreateEntity(repository, context, "E25", "E21", u1);
                            e = CreateEntity(repository, context, "E26", "E21", u1);
                            e = CreateEntity(repository, context, "E27", "E21", u1);
                            e = CreateEntity(repository, context, "E28", "E21", u1);
                            e = CreateEntity(repository, context, "E29", "E21", u1);
                        }
                    }
                    e = CreateEntity(repository, context, "E9", "E3", u1);
                    e = CreateEntity(repository, context, "E10", "E3", u1);
                }
                e = CreateEntity(repository, context, "E4", "E1", u1);
                {
                    e = CreateEntity(repository, context, "E11", "E4", u1);
                    e = CreateEntity(repository, context, "E12", "E4", u1);
                    {
                        e = CreateEntity(repository, context, "E30", "E12", u1);
                        {
                            e = CreateEntity(repository, context, "E31", "E30", u1);
                            {
                                e = CreateEntity(repository, context, "E33", "E31", u1);
                                e = CreateEntity(repository, context, "E34", "E31", u1);
                                {
                                    e = CreateEntity(repository, context, "E40", "E34", u1);
                                    e = CreateEntity(repository, context, "E43", "E34", u1);
                                    {
                                        e = CreateEntity(repository, context, "E44", "E43", u1);
                                        e = CreateEntity(repository, context, "E45", "E43", u1);
                                        e = CreateEntity(repository, context, "E46", "E43", u1);
                                        e = CreateEntity(repository, context, "E47", "E43", u1);
                                        e = CreateEntity(repository, context, "E48", "E43", u1);
                                        e = CreateEntity(repository, context, "E49", "E43", u1);
                                    }
                                }
                            }
                            e = CreateEntity(repository, context, "E32", "E30", u1);
                            {
                                e = CreateEntity(repository, context, "E35", "E32", u1);
                                {
                                    e = CreateEntity(repository, context, "E41", "E35", u1);
                                    {
                                        e = CreateEntity(repository, context, "E42", "E41", u1);
                                    }
                                }
                                e = CreateEntity(repository, context, "E36", "E32", u1);
                                {
                                    e = CreateEntity(repository, context, "E37", "E36", u1);
                                    {
                                        e = CreateEntity(repository, context, "E38", "E37", u1);
                                        e = CreateEntity(repository, context, "E39", "E37", u1);
                                    }
                                }
                            }
                        }
                    }
                    e = CreateEntity(repository, context, "E13", "E4", u1);
                }
            }
            return(repository);
        }