public async Task DeleteMembers(string roleName, IEnumerable <string> usersId) { NullExpressionsExtensions.ThrowIfNull(roleName, () => new RoleNotFoundException("Role is null")); NullExpressionsExtensions.ThrowIfNull(usersId, nameof(usersId)); AppRole role = await RoleManager.FindByNameAsync(roleName); if (role == null) { throw new RoleNotFoundException("There isn't role named " + roleName); } foreach (var user in usersId) { IdentityResult result = await IdentityManager.RemoveFromRoleAsync(user, role.Name); result.CheckResult(); } }
public async Task AddMembers(string roleName, IEnumerable <string> usersId) { NullExpressionsExtensions.ThrowIfNull(roleName, nameof(roleName)); NullExpressionsExtensions.ThrowIfNull(usersId, nameof(usersId)); if (usersId == null || !usersId.Any()) { return; } AppRole role = await RoleManager.FindByNameAsync(roleName); if (role.Users.ToList().Select(x => x.UserId).Intersect(usersId).Any()) { throw new UsersAreRecurringException("Users are recurring, no one were added."); } foreach (var user in usersId) { IdentityResult result = await IdentityManager.AddToRoleAsync(user, role.Name); result.CheckResult(); } }