/// <summary> /// Handle single user. /// </summary> /// <param name="entry">User LDAP object.</param> private void HandleUser(SearchResultEntry entry) { // Create CMS object from LDAP object var user = new User( LdapHelper.GetObjectGuid(entry), LdapHelper.GetAttributeString(entry.Attributes["name"], true), LdapHelper.IsUserEnabled(entry), UserBindings.Select(k => new KeyValuePair <string, string>(k.Cms, LdapHelper.GetAttributeString(entry.Attributes[k.Ldap]))).ToList()); // Find existing object in LDAP replica var existing = Replica.Users.FirstOrDefault(u => u.Guid == user.Guid); if (LdapHelper.IsDeleted(entry)) { if (existing != null) { // Remove user Sender.RemoveUser(existing); Replica.Users.Remove(existing); } } else if (existing != null) { // Check if any attribute has changed var userXml = Sender.GetUser(user.Guid); if (!string.IsNullOrEmpty(userXml)) { bool userChanged = User.InternalBindings.Any( b => RestHelper.GetAttributeFromReponse(userXml, b.Value) != ((b.Key == "userAccountControl" ? LdapHelper.IsUserEnabled(entry).ToString().ToLowerInvariant() : LdapHelper.GetAttributeString(entry.Attributes[b.Key], b.Key == "name")) ?? string.Empty)); userChanged |= UserBindings.Any( b => RestHelper.GetAttributeFromReponse(userXml, b.Cms) != (LdapHelper.GetAttributeString(entry.Attributes[b.Ldap]) ?? string.Empty)); if (userChanged) { // Modify user Sender.ModifyUser(user); } } } else { // Add user long?userId = Sender.AddUser(user); if (userId != null) { user.Id = userId.Value; user.DistinguishedName = entry.DistinguishedName; Replica.Users.Add(user); } } }