Skip to content

Commit 9d1d71f

Browse files
committed
improve UserExistsInGroup
1 parent 39621cf commit 9d1d71f

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups/AddUserToGroups.cs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,44 +70,26 @@ public static Result AddUserToGroups([PropertyTab] Input input, [PropertyTab] Co
7070

7171
private static bool UserExistsInGroup(LdapConnection connection, string userDn, string groupDn, CancellationToken cancellationToken)
7272
{
73-
// Search for the user's groups
73+
cancellationToken.ThrowIfCancellationRequested();
74+
7475
ILdapSearchResults searchResults = connection.Search(
7576
groupDn,
7677
LdapConnection.ScopeSub,
7778
"(objectClass=*)",
7879
null,
7980
false);
8081

81-
// Check if the user is a member of the specified group
82-
while (searchResults.HasMore())
82+
if (searchResults.HasMore())
8383
{
84-
cancellationToken.ThrowIfCancellationRequested();
85-
LdapEntry entry;
8684
try
8785
{
88-
entry = searchResults.Next();
89-
}
90-
catch (LdapException)
91-
{
92-
continue;
86+
var entry = searchResults.Next();
87+
var memberAttr = entry.GetAttribute("member");
88+
return memberAttr.StringValueArray.Contains(userDn, StringComparer.OrdinalIgnoreCase);
9389
}
94-
95-
if (entry != null)
90+
catch (KeyNotFoundException)
9691
{
97-
LdapAttribute memberAttr;
98-
99-
try
100-
{
101-
memberAttr = entry.GetAttribute("member");
102-
}
103-
catch (KeyNotFoundException)
104-
{
105-
continue;
106-
}
107-
108-
var currentMembers = memberAttr.StringValueArray;
109-
if (currentMembers.Where(e => e == userDn).Any())
110-
return true;
92+
return false;
11193
}
11294
}
11395

0 commit comments

Comments
 (0)