我从活动目录中获取用户和组,但经过一些测试后我们发现memberOF
未与member
对齐示例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个词典或查找..等等
示例groupA用户{A,B,C},GroupB用户{A,C}
示例userA分组{x,y},userB groups {x,z}
编辑:另一个例子:假设这是从Active目录返回的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"}
我希望它更清楚
答案 0 :(得分: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);
});
编辑:保留上面的代码,以防它有用
以下是解决实际delta问题的新代码...它只查找其他列表不匹配的用户/组。
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);
});