public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            var email = context.HttpContext.User.Identity.Name;
            var user  = _database.Employee.SingleOrDefault(x => x.Email == email);

            if (email != null && user == null)
            {
                // The user is still authenticated under a previous email address,
                // but the email address in the database has since been updated.
                // Force them to log in again.
                await _loginService.LogOut();

                context.Result = new UnauthorizedResult();
            }
            else
            {
                _userContext.User        = user;
                _userContext.Permissions = new Permission[] { };

                if (user != null)
                {
                    var query = new Permissions.Query
                    {
                        EmployeeId = user.Id
                    };
                    _userContext.Permissions = await _mediator.Send(query);
                }
            }
        }
Exemplo n.º 2
0
        public async Task ListsAllPermissions(SliceFixture fixture)
        {
            // Arrange
            var sampleRole = new UserRole
            {
                Name = "Role1"
            };

            await fixture.InsertAsync(sampleRole);

            var permissions = new Permission[]
            {
                new Permission
                {
                    Action           = "SomeAction",
                    Controller       = "SomeController",
                    PermissionsRoles = new List <PermissionRole>
                    {
                        new PermissionRole
                        {
                            RoleId = sampleRole.Id
                        }
                    }
                },
                new Permission
                {
                    Action           = "SomeAction1",
                    Controller       = "SomeController1",
                    PermissionsRoles = new List <PermissionRole>()
                }
            };

            await fixture.InsertAsync(permissions);

            var query = new Permissions.Query();

            // Act
            var response = await fixture.SendAsync(query);

            // Assert
            var permissionsInDb = await fixture.ExecuteDbContextAsync(db => db.Permissions
                                                                      .Include(p => p.PermissionsRoles)
                                                                      .ToListAsync());

            // So I can compare by index
            response.OrderBy(r => r.Id);
            permissionsInDb.OrderBy(r => r.Id);

            response.ElementAt(0).Id.ShouldBe(permissionsInDb[0].Id);
            response.ElementAt(1).Id.ShouldBe(permissionsInDb[1].Id);
            response.ElementAt(0).Action.ShouldBe(permissionsInDb[0].Action);
            response.ElementAt(1).Action.ShouldBe(permissionsInDb[1].Action);
            response.ElementAt(0).Controller.ShouldBe(permissionsInDb[0].Controller);
            response.ElementAt(1).Controller.ShouldBe(permissionsInDb[1].Controller);
            response.ElementAt(0).PermissionsRolesCount.ShouldBe(permissionsInDb[0].PermissionsRoles.Count);
            response.ElementAt(1).PermissionsRolesCount.ShouldBe(permissionsInDb[1].PermissionsRoles.Count);
        }