Beispiel #1
0
        internal static void UpdateNextSyncTime(int ldapId)
        {
            try
            {
                LdapSettings ldap = LdapSettings.Load(ldapId);
                if (ldap.Autosync)
                {
                    // Calculate next sync time
                    DateTime now = ldap.LastSynchronization;
                    if (now == DateTime.MinValue)
                    {
                        now = DateTime.UtcNow;
                    }

                    DateTime next = ldap.AutosyncStart;
                    while (next < now)
                    {
                        next = next.AddHours(ldap.AutosyncInterval);
                    }

                    // Add next sync time to schedule
                    Schedule.AddDateTypeValue(DateTypes.LdapSynchronization, ldapId, next);
                }
                else
                {
                    Schedule.DeleteDateTypeValue(DateTypes.LdapSynchronization, ldapId);
                }
            }
            catch (Exception ex)
            {
                Log.WriteError(ex.ToString());
            }
        }
Beispiel #2
0
        public static int Synchronize(int LdapId)
        {
            Ldap.CheckAccess();

            LdapSettings ldap = LdapSettings.Load(LdapId);

            Hashtable users        = new Hashtable();
            ArrayList updatedUsers = new ArrayList();

            using (IDataReader reader = User.GetLdap())
            {
                while (reader.Read())
                {
                    UserInfo ui = new UserInfo();
                    ui.Load(reader);
                    users[ui[ldap.IbnKey]] = ui;
                }
            }

            if (users.Count > 0)
            {
                using (DirectoryEntry root = new DirectoryEntry(string.Format("LDAP://{0}", ldap.Domain), ldap.Username, ldap.Password))
                {
                    root.RefreshCache();
                    DirectorySearcher searcher = new DirectorySearcher(root, ldap.Filter);

                    foreach (SearchResult result in searcher.FindAll())
                    {
                        UserInfo ui = users[GetPropertyValue(result, ldap.LdapKey)] as UserInfo;
                        if (ui != null)
                        {
                            foreach (LdapField field in ldap.Fields)
                            {
                                string sVal = GetPropertyValue(result, field.LdapName);

                                if (field.BitField)
                                {
                                    if (sVal.Length < 1)
                                    {
                                        continue;
                                    }

                                    int iVal = int.Parse(sVal) & field.BitMask;
                                    if (field.Equal)
                                    {
                                        sVal = (iVal == field.CompareTo).ToString();
                                    }
                                    else
                                    {
                                        sVal = (iVal != field.CompareTo).ToString();
                                    }
                                }

                                if (field.IbnName == UserInfo.IbnProperty.WindowsLogin.ToString())
                                {
                                    sVal = string.Format("{0}\\{1}", ldap.Domain, sVal).ToLower();
                                }

                                if (ui[field.IbnName] != sVal && (sVal.Length > 0 || field.IbnName != UserInfo.IbnProperty.Email.ToString()))
                                {
                                    ui.UpdatedValues[field.IbnName] = sVal;

                                    if (!updatedUsers.Contains(ui))
                                    {
                                        updatedUsers.Add(ui);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            ldap.LastSynchronization = DateTime.UtcNow;
            DbLdap.SettingsUpdateLastSynchronization(ldap.LdapId, ldap.LastSynchronization);

            int logId;

            using (DbTransaction tran = DbTransaction.Begin())
            {
                logId = DbLdap.SyncLogCreate(ldap.LdapId, ldap.LastSynchronization, updatedUsers.Count);
                foreach (UserInfo ui in updatedUsers)
                {
                    bool wasActive = bool.Parse(ui.IsActive);

                    // Save changes to log
                    foreach (string name in UserInfo.PropertyNamesIbnAll)
                    {
                        string oldVal = ui[name];
                        string newVal = ui.UpdatedValues[name];
                        if (newVal != null || name == UserInfo.IbnProperty.FirstName.ToString() || name == UserInfo.IbnProperty.LastName.ToString())
                        {
                            if (newVal == null)
                            {
                                newVal = oldVal;
                            }

                            DbLdap.SyncFieldCreate(logId, ui.UserId, name, oldVal, newVal);
                        }

                        // Replace old values with new ones
                        if (newVal != null)
                        {
                            ui[name] = newVal;
                        }
                    }

                    try
                    {
                        // Update main database
                        DBUser.UpdateMain2(ui.OriginalId, ui.Login, ui.FirstName, ui.LastName, ui.Email);

                        // Update portal database
                        DBUser.Update(ui.UserId, ui.Login, ui.FirstName, ui.LastName, ui.Email, ui.WindowsLogin, ui.LdapUid);
                        DBUser.UpdateProfile(ui.UserId, ui.Phone, ui.Fax, ui.Mobile, ui.JobTitle, ui.Department, ui.Company, ui.Location);

                        // Update activity
                        bool isActive = bool.Parse(ui.IsActive);
                        if (isActive != wasActive && (ldap.Activate && isActive || ldap.Deactivate && !isActive))
                        {
                            User.UpdateActivity(ui.UserId, isActive);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteError(ex.ToString());
                    }
                }

                tran.Commit();
            }
            return(logId);
        }