0

我从活动目录中获取用户和组,但是经过一些测试,我们发现memberOFmember示例 userA 不对齐是 groupW 的成员,但 groupW 没有将 userA 列为成员。为了解决这个问题,我们必须同时让成员和成员同步它们。

public class User
    {
       public string UserName { get; set; }
       public IList<string> MemberOf { get; set; } // list of group names
    }

public class Group
{
   public string Name { get; set; }
   public IList<string> Members { get; set; } // list of username
}

从活动目录我得到

IEnumerable<Group> allGroups
IEnumerable<User> allUsers

我怎样才能得到三角洲?

我应该有 2 个字典或查找 ..etc

  1. allGroups 列出在 User.MemberOF 字典中未找到的所有组,该字典应具有作为组名和用户值列表的键。

示例 groupA 用户 {A,B,C},GroupB 用户{A,C}

  1. allUsers 列出在 Group.member 字典中未找到的所有用户,键应为用户名,值列表为组。

示例 userA 组{x,y},userB 组{x,z}

编辑:另一个例子:假设这是从活动目录返回的 2 个 IEnumerable。

 `IEnumerable<Group> allGroups` contains
 - GroupA {"Mike","Jan","David"}
 - GroupB {"Kim","David","Jolan","Tim"}

    // where Groupx is the name of the group and between {""} is the list of member

    IEnumerable<User> allUsers contains

 - Mike {"GroupA","GroupB","GroupC"}
 - David {"GroupA","GroupB"}
 - Jolan {"GroupB","GroupC"}

在这个例子中,我们可以看到,当我们要求 LDAP 获取 groupA 的所有成员时,“Jolan”没有被列出。但是当我们要求获取“Jolan”是成员的所有组时,我们可以看到“GroupA”已列出。与“Mike”相同,他是 GroupB 和 GroupC 的成员。GroupC 未列出。在这种情况下,“大卫”具有正确的值。

还要注意,“Tim”列在 groupB 中,虽然他不在allUsers

结果应该是这样的

Dictionary<string,IList<string>> missingUsers;
Item 1 > key="Mike", Value={"GroupB","GroupC"}
Item 2 > Key="Jolan" , Value= {"GroupC"}

Dictionary<string,IList<string>> missingGroup;
item 1 > Key="GroupB",{"Tim"}

我希望它更清楚

4

1 回答 1

1

我不完全确定这是否是您要问的...我明天会根据您对我的评论的回复更改我的答案。:)

假设您想要一个包含不在该组中的用户列表的组字典,以及一个包含不属于该组的用户列表的用户字典,那么这里是它的代码......

    var groupsWithUsersNotInThem = new Dictionary<Group, List<User>>();
    var usersWithGroupsTheyArentIn = new Dictionary<User, List<Group>>();
    allUsers.ForEach(u =>
        {
            var groupsThisUserIsntIn = groups.Where(g => !g.Members.Contains(u.UserName)).ToList();
            if (groupsThisUserIsntIn.Count() > 0)
                usersWithGroupsTheyArentIn.Add(u, groupsThisUserIsntIn);
        });
    allGroups.ForEach(g =>
    {
        var usersNotInThisGroup = users.Where(u => !u.MemberOf.Contains(g.Name)).ToList();
        if (usersNotInThisGroup.Count() > 0)
            groupsWithUsersNotInThem.Add(g, usersNotInThisGroup);
    });

编辑:留下上面的代码以防万一

这是解决实际增量问题的新代码......它只找到其他列表不匹配的用户/组。

    var missingGroups = new Dictionary<String, List<String>>();
    var missingUsers = new Dictionary<String, List<String>>();
    allUsers.ForEach(u =>
    {
        // get the list where the group exists but this user isn't in it
        var groupsThisUserIsntIn = allGroups
            .Where(g => u.MemberOf.Contains(g.Name) && !g.Members.Contains(u.UserName))
            .Select(g => g.Name).ToList();
        // add in the groups this user says he belongs to but that aren't in allGroups
        groupsThisUserIsntIn.AddRange(u.MemberOf.Where(userGroupName => allGroups.All(g => g.Name != userGroupName)));
        if (groupsThisUserIsntIn.Count() > 0)
            missingUsers.Add(u.UserName, groupsThisUserIsntIn);
    });
    allGroups.ForEach(g =>
    {
        // get the list where the user exists but this group isn't in it
        var usersNotInThisGroup = allUsers
            .Where(u => g.Members.Contains(u.UserName) && !u.MemberOf.Contains(g.Name))
            .Select(u => u.UserName).ToList();
        // add in the users this group says it has but that aren't in allUsers 
        usersNotInThisGroup.AddRange(g.Members.Where(groupUserName => allUsers.All(u => u.UserName != groupUserName)));
        if (usersNotInThisGroup.Count() > 0)
            missingGroups.Add(g.Name, usersNotInThisGroup);
    });
于 2013-05-01T23:32:53.557 回答