예제 #1
0
        protected EntityFrameworkCore.Data.Scope CreateScope(AuthorizationsTestContext context, string scopeName, params string[] parentScopeNames)
        {
            var scope = new EntityFrameworkCore.Data.Scope
            {
                Name           = scopeName,
                Description    = scopeName,
                CreationBy     = context.CurrentUserId,
                ModificationBy = context.CurrentUserId
            };

            context.Scopes().Add(scope);

            foreach (var parentScopeName in parentScopeNames)
            {
                var parentScope =
                    context.ChangeTracker
                    .Entries <EntityFrameworkCore.Data.Scope>()
                    .Select(e => e.Entity)
                    .First(s => s.Name == parentScopeName);

                context.ScopeHierarchies().Add(new EntityFrameworkCore.Data.ScopeHierarchy
                {
                    Child  = scope,
                    Parent = parentScope
                });
            }

            return(scope);
        }
예제 #2
0
 //------> Scopes
 //
 //                           +-+                                +-+
 //                           |A|                                |B|
 //                           +-+                                +-+
 //                            ^                                  ^
 //                            |                                  |
 //       +-----+-----+-----+--+--+-----+-----+-----+             |
 //       |     |     |     |     |     |     |     |             |
 //      +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+           +-+
 //      |C|   |D|   |E|   |F|   |G|   |H|   |I|   |J|           |V|
 //      +-+   +-+   +-+   +-+   +-+   +-+   +-+   +-+           +-+
 //       ^     ^     ^     ^     ^     ^     ^     ^             ^
 //       |     |     |     |     |     |     |     |             |
 //       +--+--+     +--+--+     +--+--+  +--+--+--+             |
 //          |           |           |     |     |                |
 //          |           |           |     |     +----------------+
 //          |           |           |     |     |
 //          |          +-+         +-+    |    +-+
 //          |          |K|         |L|    |    |M|
 //          |          +-+         +-+    |    +-+
 //          |           ^           ^     |     ^
 //          |           |           |     |     |
 //          |         +-+-+       +-+-+---+   +-+-+
 //          |         |   |       |   |       |   |
 //         +-+       +-+ +-+     +-+ +-+     +-+ +-+
 //         |N|       |O| |P|     |Q| |R|     |S| |T|
 //         +-+       +-+ +-+     +-+ +-+     +-+ +-+
 //                        ^       ^
 //                        |       |
 //                        +---+---+
 //                            |
 //                           +-+
 //                           |U|
 //                           +-+
 //
 //------> Authorizations
 // E | Role1: [ Right1, Right2 ]
 // F | Role2: [ Right3 ]
 // C | Role3: [ Right4 ]
 // L | Role4: [ Right5 ]
 // B | Role5: [ Right6 ]
 private void CreateTestScopeTree(AuthorizationsTestContext context)
 {
     this.testScopes["A"] = this.CreateScope(context, "A");
     this.testScopes["B"] = this.CreateScope(context, "B");
     this.testScopes["C"] = this.CreateScope(context, "C", "A");
     this.testScopes["D"] = this.CreateScope(context, "D", "A");
     this.testScopes["E"] = this.CreateScope(context, "E", "A");
     this.testScopes["F"] = this.CreateScope(context, "F", "A");
     this.testScopes["G"] = this.CreateScope(context, "G", "A");
     this.testScopes["H"] = this.CreateScope(context, "H", "A");
     this.testScopes["I"] = this.CreateScope(context, "I", "A");
     this.testScopes["J"] = this.CreateScope(context, "J", "A");
     this.testScopes["V"] = this.CreateScope(context, "V", "B");
     this.testScopes["N"] = this.CreateScope(context, "N", "C", "D");
     this.testScopes["K"] = this.CreateScope(context, "K", "E", "F");
     this.testScopes["L"] = this.CreateScope(context, "L", "G", "H");
     this.testScopes["M"] = this.CreateScope(context, "M", "I", "J", "V");
     this.testScopes["O"] = this.CreateScope(context, "O", "K");
     this.testScopes["P"] = this.CreateScope(context, "P", "K");
     this.testScopes["Q"] = this.CreateScope(context, "Q", "L", "I", "J");
     this.testScopes["R"] = this.CreateScope(context, "R", "L", "I", "J");
     this.testScopes["S"] = this.CreateScope(context, "S", "M");
     this.testScopes["R"] = this.CreateScope(context, "T", "M");
     this.testScopes["U"] = this.CreateScope(context, "U", "P", "Q");
 }
예제 #3
0
        protected EntityFrameworkCore.Data.RoleRight AddRightToRole(AuthorizationsTestContext context, EntityFrameworkCore.Data.Right right, EntityFrameworkCore.Data.Role role)
        {
            var roleRight = new EntityFrameworkCore.Data.RoleRight
            {
                Right = right,
                Role  = role
            };

            context.RoleRights().Add(roleRight);

            return(roleRight);
        }
예제 #4
0
        protected EntityFrameworkCore.Data.Right CreateRight(AuthorizationsTestContext context, string rightName)
        {
            var right = new EntityFrameworkCore.Data.Right
            {
                Name           = rightName,
                CreationBy     = context.CurrentUserId,
                ModificationBy = context.CurrentUserId
            };

            context.Rights().Add(right);

            return(right);
        }
예제 #5
0
        public EntityFrameworkCore.Data.Role CreateRole(AuthorizationsTestContext context, string roleName)
        {
            var role = new EntityFrameworkCore.Data.Role
            {
                Name           = roleName,
                CreationBy     = context.CurrentUserId,
                ModificationBy = context.CurrentUserId
            };

            context.Roles().Add(role);

            return(role);
        }
예제 #6
0
        protected EntityFrameworkCore.Data.Membership AddPrincipalToGroup(AuthorizationsTestContext context, Guid principalId, EntityFrameworkCore.Data.Group group)
        {
            var memberShip = new EntityFrameworkCore.Data.Membership
            {
                PrincipalId    = principalId,
                Group          = group,
                CreationBy     = context.CurrentUserId,
                ModificationBy = context.CurrentUserId
            };

            context.Memberships().Add(memberShip);

            return(memberShip);
        }
예제 #7
0
        protected EntityFrameworkCore.Data.Authorization CreateAuthorization(AuthorizationsTestContext context, Guid principalId, EntityFrameworkCore.Data.Role role, EntityFrameworkCore.Data.Scope scope)
        {
            var authorization = new EntityFrameworkCore.Data.Authorization
            {
                PrincipalId    = principalId,
                Role           = role,
                Scope          = scope,
                CreationBy     = context.CurrentUserId,
                ModificationBy = context.CurrentUserId
            };

            context.Authorizations().Add(authorization);

            return(authorization);
        }
예제 #8
0
        private async Task InitAuthorizationsAsync(AuthorizationsTestContext context, AuthorizationsTarget authorizationsTarget = AuthorizationsTarget.CurrentUser)
        {
            var right1 = this.CreateRight(context, "right1");
            var right2 = this.CreateRight(context, "right2");
            var role1  = this.CreateRole(context, "role1");
            var right3 = this.CreateRight(context, "right3");
            var role2  = this.CreateRole(context, "role2");
            var right4 = this.CreateRight(context, "right4");
            var role3  = this.CreateRole(context, "role3");
            var right5 = this.CreateRight(context, "right5");
            var role4  = this.CreateRole(context, "role4");
            var right6 = this.CreateRight(context, "right6");
            var role5  = this.CreateRole(context, "role5");

            this.AddRightToRole(context, right1, role1);
            this.AddRightToRole(context, right2, role1);
            this.AddRightToRole(context, right3, role2);
            this.AddRightToRole(context, right4, role3);
            this.AddRightToRole(context, right5, role4);
            this.AddRightToRole(context, right6, role5);

            this.CreateTestScopeTree(context);

            var principalId = context.CurrentUserId;

            if (authorizationsTarget != AuthorizationsTarget.CurrentUser)
            {
                var groupParent = this.CreateGroup(context, "groupParent");
                var groupChild  = this.CreateGroup(context, "groupChild");
                this.AddPrincipalToGroup(context, groupChild.Id, groupParent);
                this.AddPrincipalToGroup(context, context.CurrentUserId, groupChild);

                principalId = authorizationsTarget == AuthorizationsTarget.ChildGroup ? groupChild.Id : groupParent.Id;
            }

            this.CreateAuthorization(context, principalId, role1, testScopes["E"]);
            this.CreateAuthorization(context, principalId, role2, testScopes["F"]);
            this.CreateAuthorization(context, principalId, role3, testScopes["C"]);
            this.CreateAuthorization(context, principalId, role4, testScopes["L"]);
            this.CreateAuthorization(context, principalId, role5, testScopes["B"]);

            await context.SaveChangesAsync();
        }
예제 #9
0
        protected EntityFrameworkCore.Data.Group CreateGroup(AuthorizationsTestContext context, string groupName)
        {
            var principal = new EntityFrameworkCore.Data.Principal
            {
                Id             = Guid.NewGuid(),
                CreationBy     = context.CurrentUserId,
                ModificationBy = context.CurrentUserId
            };

            context.Principals().Add(principal);
            var group = new EntityFrameworkCore.Data.Group
            {
                Id             = principal.Id,
                Name           = groupName,
                CreationBy     = context.CurrentUserId,
                ModificationBy = context.CurrentUserId
            };

            context.Groups().Add(group);

            return(group);
        }
예제 #10
0
 public PrincipalIdProvider(AuthorizationsTestContext context)
 {
     this.context = context;
 }