Example #1
0
        private async Task <IEnumerable <RoleManagerViewModel> > GetUserRolesList(CancellationToken cancellationToken)
        {
            var users         = new List <RoleManagerViewModel>();
            var readonlyUsers = (await _roleManager.GetUserIdsInRoleAsync(RoleNameConstants.UserRole, cancellationToken)).ToList();

            var adminUsers = (await _roleManager.GetUserIdsInRoleAsync(RoleNameConstants.AdminRole, cancellationToken)).ToList();

            readonlyUsers.AddRange(adminUsers);


            if (_contextService.IsSuperUser())
            {
                var superAdminUsers = (await _roleManager.GetUserIdsInRoleAsync(RoleNameConstants.SuperAdminRole, cancellationToken)).ToList();
                readonlyUsers.AddRange(superAdminUsers);
            }


            foreach (var user in readonlyUsers.Distinct())
            {
                var x = await _userManager.FindByIdAsync(user);

                users.Add(new RoleManagerViewModel()
                {
                    Email             = x.UserName,
                    IsInUserRole      = await _roleManager.IsInRoleAsync(x, RoleNameConstants.UserRole, cancellationToken),
                    IsInEditorRole    = await _roleManager.IsInRoleAsync(x, RoleNameConstants.AdminRole, cancellationToken),
                    IsInSuperUserRole = await _roleManager.IsInRoleAsync(x, RoleNameConstants.SuperAdminRole, cancellationToken),
                    Id = x.Id
                });
            }

            return(users.Where(t => t.Email != User.Identity.Name)); // filter out the current logged in user from the list;
        }
        public async Task DynamoRoleUsersStore_GetUserIdsInRole_GetsUsers()
        {
            var user1    = new DynamoIdentityUser(TestUtils.RandomString(10));
            var user2    = new DynamoIdentityUser(TestUtils.RandomString(10));
            var roleName = TestUtils.RandomString(10);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.AddToRoleAsync(user1, roleName, CancellationToken.None);

                Assert.True(await roleStore.IsInRoleAsync(user1, roleName, CancellationToken.None));

                await roleStore.AddToRoleAsync(user2, roleName, CancellationToken.None);

                Assert.True(await roleStore.IsInRoleAsync(user2, roleName, CancellationToken.None));

                // ACT
                var result = await roleStore.GetUserIdsInRoleAsync(roleName, CancellationToken.None);

                // ASSERT
                Assert.Contains(user1.Id, result);
                Assert.Contains(user2.Id, result);
                Assert.Equal(2, result.Count);
            }
        }
        public async Task DynamoRoleUsersStore_RemoveFromRole_RemovesUserFromRole()
        {
            var user     = new DynamoIdentityUser(TestUtils.RandomString(10));
            var roleName = TestUtils.RandomString(10);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.AddToRoleAsync(user, roleName, CancellationToken.None);

                var result = await roleStore.IsInRoleAsync(user, roleName, CancellationToken.None);

                Assert.True(result);

                // ACT
                await roleStore.RemoveFromRoleAsync(user, roleName, CancellationToken.None);

                // ASSERT
                var result2 = await roleStore.IsInRoleAsync(user, roleName, CancellationToken.None);

                Assert.False(result2);
            }
        }
        public async Task DynamoRoleUsersStore_AddToRole_HandlesDuplicateRoleUserEntry()
        {
            var user     = new DynamoIdentityUser(TestUtils.RandomString(10));
            var roleName = TestUtils.RandomString(10);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.AddToRoleAsync(user, roleName, CancellationToken.None);

                var result = await roleStore.IsInRoleAsync(user, roleName, CancellationToken.None);

                Assert.True(result);

                // ACT
                await roleStore.AddToRoleAsync(user, roleName, CancellationToken.None);

                // ASSERT
                var roles = await roleStore.GetRolesAsync(user, CancellationToken.None);

                Assert.Equal(1, roles.Count);
            }
        }