public async Task ComputeGroupMembershipAsync(PerformContext performContext) { Logger.Info(performContext, "Getting the groups from Active Directory..."); var groups = _activeDirectoryManager.GetAllGroups().ToArray(); foreach (var group in groups) { performContext?.Cancel(); var groupMembers = _activeDirectoryManager.GetGroupMembers(group.Id); var userGroups = PortalService.GetAllGroupUsers(group.Id).ToArray(); var newMembers = groupMembers.Except(userGroups, _licenseUserGroupEqualityComparer).ToArray(); foreach (var newMember in newMembers) { performContext?.Cancel(); await PortalService.AddUserGroupAsync( LicenseUserGroup.Create(group.Id, newMember.UserId)); performContext?.WriteSuccessLine($"+ {group.Name} {newMember.UserId}"); Logger.Info($"User: {newMember.UserId} was added to Group: {group.Id} {group.Name}"); } var staleMembers = userGroups.Except(groupMembers, _licenseUserGroupEqualityComparer).ToArray(); foreach (var staleMember in staleMembers) { performContext?.Cancel(); await PortalService.DeleteUserGroupAsync( staleMember); performContext?.WriteWarnLine($"- {group.Name} {staleMember.UserId}"); Logger.Info($"User: {staleMember.UserId} was removed from Group: {group.Id} {group.Name}"); } if (!newMembers.Any() && !staleMembers.Any()) { Logger.Info($"Group: {group.Id} No changes have been made."); } else { Logger.Info($"Group: {group.Id} Added: {newMembers.Length} Removed: {staleMembers.Length}"); } } }