Skip to content

Commit 4745104

Browse files
Merge pull request #42 from FrendsPlatform/FSPES-17-Skip-User-Issue
Fixed LDAP exception when adding duplicate users with skip option ena…
2 parents 2770052 + 9d1d71f commit 4745104

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

Frends.LDAP.AddUserToGroups/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [1.2.0] - 2025-11-20
4+
### Fixed
5+
- Fixed LDAP exception when adding duplicate users with skip option enabled.
6+
37
## [1.1.0] - 2025-08-13
48
### Fixed
59
- Fixed KeyNotFoundException when checking empty groups for existing users.

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

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public static Result AddUserToGroups([PropertyTab] Input input, [PropertyTab] Co
5151
}
5252
catch (LdapException ex)
5353
{
54+
if (ex.ResultCode == LdapException.AttributeOrValueExists && input.UserExistsAction == UserExistsAction.Skip)
55+
{
56+
return new Result(false, "User already exists in the group, skipped as requested.", input.UserDistinguishedName, input.GroupDistinguishedName);
57+
}
5458
throw new Exception($"AddUserToGroups LDAP error: {ex.Message}");
5559
}
5660
catch (Exception ex)
@@ -66,44 +70,26 @@ public static Result AddUserToGroups([PropertyTab] Input input, [PropertyTab] Co
6670

6771
private static bool UserExistsInGroup(LdapConnection connection, string userDn, string groupDn, CancellationToken cancellationToken)
6872
{
69-
// Search for the user's groups
73+
cancellationToken.ThrowIfCancellationRequested();
74+
7075
ILdapSearchResults searchResults = connection.Search(
7176
groupDn,
7277
LdapConnection.ScopeSub,
7378
"(objectClass=*)",
7479
null,
7580
false);
7681

77-
// Check if the user is a member of the specified group
78-
while (searchResults.HasMore())
82+
if (searchResults.HasMore())
7983
{
80-
cancellationToken.ThrowIfCancellationRequested();
81-
LdapEntry entry;
8284
try
8385
{
84-
entry = searchResults.Next();
85-
}
86-
catch (LdapException)
87-
{
88-
continue;
86+
var entry = searchResults.Next();
87+
var memberAttr = entry.GetAttribute("member");
88+
return memberAttr.StringValueArray.Contains(userDn, StringComparer.OrdinalIgnoreCase);
8989
}
90-
91-
if (entry != null)
90+
catch (KeyNotFoundException)
9291
{
93-
LdapAttribute memberAttr;
94-
95-
try
96-
{
97-
memberAttr = entry.GetAttribute("member");
98-
}
99-
catch (KeyNotFoundException)
100-
{
101-
continue;
102-
}
103-
104-
var currentMembers = memberAttr.StringValueArray;
105-
if (currentMembers.Where(e => e == userDn).Any())
106-
return true;
92+
return false;
10793
}
10894
}
10995

Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net6.0</TargetFrameworks>
5-
<Version>1.1.0</Version>
5+
<Version>1.2.0</Version>
66
<Authors>Frends</Authors>
77
<Copyright>Frends</Copyright>
88
<Company>Frends</Company>

0 commit comments

Comments
 (0)