public static void UpdatePortalUserCustomProperties(DirectoryEntry entry, dynamic content, SyncTree syncTree) { // sAMAccountName -> Name if (syncTree.Server.SyncUserName) { content.Name = entry.Properties[syncTree.UserNameProperty].Value.ToString(); // in case of AD users the content name and login name are the same content.LoginName = content.Name; } // user actions foreach (var propMapping in syncTree.Mappings) { if (propMapping.AdProperties.Count == 1) { if (propMapping.PortalProperties.Count == 1) { // 1 ADproperty + 1 portalproperty var portalProp = propMapping.PortalProperties[0]; var adProp = propMapping.AdProperties[0]; var adValue = GetEntryValue(entry, adProp); SetContentValue(content, portalProp, adValue, ADObjectType.User); // Email is a special case: if it is empty, the user cannot be synced, at least we log it here. if (string.CompareOrdinal(portalProp.Name, "Email") == 0 && string.IsNullOrEmpty(adValue)) { AdLog.LogWarning("Email is empty for user " + entry.Path); } } else { // 1 ADproperty + n portalproperty // split AD value (preserving spaces) and put them into portal properties var adProp = propMapping.AdProperties[0]; var adValues = GetEntryValue(entry, adProp).Split(new[] { propMapping.Separator }, StringSplitOptions.None); int index = 0; foreach (var portalProp in propMapping.PortalProperties) { var adValue = (index < adValues.Length) ? adValues[index] : null; SetContentValue(content, portalProp, adValue, ADObjectType.User); index++; } } } else { // 1 portalproperty + n ADproperty // concat AD property values and put it into the single portal property var portalProp = propMapping.PortalProperties[0]; var adValue = propMapping.ConcatAdPropValues(entry); SetContentValue(content, portalProp, adValue, ADObjectType.User); } } }
// ============================================================================== Instance methods /// <summary> /// Validates the configuration. Returns false only if it is absolutely /// not possible to execute ad sync. Othervise only logs warnings. /// </summary> public bool Validate() { if (this.Servers.Count == 0) { AdLog.LogWarning("No servers are configured."); } if (this.SyncTrees.Count == 0) { AdLog.LogWarning("No sync trees are configured."); } // validate server properties var invalidServers = new List <Server>(); foreach (var server in this.Servers) { if (string.IsNullOrEmpty(server.LdapServer)) { AdLog.LogWarning("LDAP server address is missing."); invalidServers.Add(server); } if (!server.VerifyConnection()) { AdLog.LogWarning("LDAP server connection failed."); invalidServers.Add(server); } } // remove incorrectly configured servers foreach (var server in invalidServers) { this.Servers.Remove(server); } // validate sync tree properties var invalidSyncTrees = new List <SyncTree>(); foreach (var syncTree in this.SyncTrees) { if (string.IsNullOrEmpty(syncTree.BaseDn)) { AdLog.LogWarning(string.Format("Sync tree {0} has no AD path (base DN) configured.", string.IsNullOrEmpty(syncTree.PortalPath) ? syncTree.BaseDn : syncTree.PortalPath)); invalidSyncTrees.Add(syncTree); } if (string.IsNullOrEmpty(syncTree.PortalPath)) { AdLog.LogWarning(string.Format("Sync tree {0} has no portal path configured.", syncTree.BaseDn)); invalidSyncTrees.Add(syncTree); } if (syncTree.Server == null) { AdLog.LogWarning(string.Format("Sync tree {0} has no valid server configured.", string.IsNullOrEmpty(syncTree.BaseDn) ? syncTree.PortalPath : syncTree.BaseDn)); invalidSyncTrees.Add(syncTree); } } // remove sync trees that are not possible to sync so that we do not have to deal with errors later foreach (var syncTree in invalidSyncTrees) { this.SyncTrees.Remove(syncTree); } if (this.Servers.Count == 0 || this.SyncTrees.Count == 0) { return(false); } return(true); }