Exemple #1
0
        public static DataTable SyncLogGetFields(int logId)
        {
            Ldap.CheckAccess();

            DataTable table = new DataTable();

            table.Columns.Add("UserId", typeof(int));
            table.Columns.Add("UserName", typeof(string));
            table.Columns.Add("FieldName", typeof(string));
            table.Columns.Add("NewValue", typeof(string));
            table.Columns.Add("OldValue", typeof(string));

            using (IDataReader reader = DbLdap.SyncLogGetFields(logId))
            {
                DataRow group      = null;
                int     lastUserId = -1;
                while (reader.Read())
                {
                    int userId = (int)reader["UserId"];
                    if (userId != lastUserId)
                    {
                        group           = table.NewRow();
                        group["UserId"] = userId;
                        table.Rows.Add(group);
                        lastUserId = userId;
                    }

                    string fieldName = reader["FieldName"].ToString();
                    string newValue  = reader["NewValue"].ToString();
                    string oldValue  = reader["OldValue"].ToString();

                    if (newValue != oldValue)
                    {
                        DataRow row = table.NewRow();
                        row["FieldName"] = fieldName;
                        row["NewValue"]  = newValue;
                        row["OldValue"]  = oldValue;
                        table.Rows.Add(row);
                    }

                    string userName = group["UserName"].ToString();
                    if (fieldName == "FirstName")
                    {
                        group["UserName"] = string.Format("{0}{1}{2}", newValue, (userName != null && userName.Length > 0) ? " " : "", userName);
                    }
                    else if (fieldName == "LastName")
                    {
                        group["UserName"] = string.Format("{2}{1}{0}", newValue, (userName != null && userName.Length > 0) ? " " : "", userName);
                    }
                }
            }

            return(table);
        }
Exemple #2
0
        public static void SyncLogDelete(ArrayList list)
        {
            Ldap.CheckAccess();

            using (DbTransaction tran = DbTransaction.Begin())
            {
                foreach (int logId in list)
                {
                    DbLdap.SyncLogDelete(logId);
                }

                tran.Commit();
            }
        }
Exemple #3
0
        public static LdapField Load(int fieldId)
        {
            Ldap.CheckAccess();

            LdapField field = null;

            using (IDataReader reader = DbLdap.FieldGet(fieldId))
            {
                if (reader.Read())
                {
                    field = new LdapField();
                    field.Load(reader);
                }
            }
            return(field);
        }
Exemple #4
0
        public static LdapSettings Load(int ldapId)
        {
            Ldap.CheckAccess();

            LdapSettings ldap = null;

            using (IDataReader reader = DbLdap.SettingsGet(ldapId))
            {
                if (reader.Read())
                {
                    ldap = new LdapSettings();

                    ldap.LdapId              = ldapId;
                    ldap.Title               = reader["Title"].ToString();
                    ldap.Domain              = reader["Domain"].ToString();
                    ldap.Username            = reader["Username"].ToString();
                    ldap.Password            = reader["Password"].ToString();
                    ldap.Filter              = reader["Filter"].ToString();
                    ldap.IbnKey              = reader["IbnKey"].ToString();
                    ldap.LdapKey             = reader["LdapKey"].ToString();
                    ldap.Activate            = (bool)reader["Activate"];
                    ldap.Deactivate          = (bool)reader["Deactivate"];
                    ldap.Autosync            = (bool)reader["Autosync"];
                    ldap.AutosyncStart       = (DateTime)reader["AutosyncStart"];
                    ldap.AutosyncInterval    = (int)reader["AutosyncInterval"];
                    ldap.LastSynchronization = DBCommon.NullToDateTime(reader["LastSynchronization"]);
                }
            }

            if (ldap != null)
            {
                using (IDataReader reader = DbLdap.FieldsGet(ldapId))
                {
                    while (reader.Read())
                    {
                        LdapField field = new LdapField();
                        field.Load(reader);
                        ldap.Fields.Add(field);
                    }
                }
            }

            return(ldap);
        }
Exemple #5
0
        // Public
        #region CreateUpdate()
        public static int CreateUpdate(int FieldId
                                       , int LdapId
                                       , bool BitField
                                       , string IbnName
                                       , string LdapName
                                       , int BitMask
                                       , bool Equal
                                       , int CompareTo
                                       )
        {
            Ldap.CheckAccess();

            return(DbLdap.FieldCreateUpdate(FieldId
                                            , LdapId
                                            , BitField
                                            , IbnName
                                            , LdapName
                                            , BitMask
                                            , Equal
                                            , CompareTo
                                            ));
        }
Exemple #6
0
        public ArrayList GetUsers()
        {
            ArrayList ret = new ArrayList();

            DirectoryEntry    root = new DirectoryEntry(string.Format("LDAP://{0}", m_domain), m_username, m_password);
            DirectorySearcher s    = new DirectorySearcher(root, m_filter);

            string ldapName;

            string[] ibnNames = UserInfo.PropertyNamesIbnAll;

            foreach (SearchResult result in s.FindAll())
            {
                UserInfo ui = new UserInfo();
                foreach (string ibnName in ibnNames)
                {
                    if (ibnName == UserInfo.IbnProperty.WindowsLogin.ToString())
                    {
                        ldapName = UserInfo.AdProperty.SamAccountName.ToString();
                    }
                    else
                    {
                        ldapName = m_fields[ibnName];
                    }

                    string sVal = Ldap.GetPropertyValue(result, ldapName);

                    if (ibnName == UserInfo.IbnProperty.WindowsLogin.ToString())
                    {
                        sVal = string.Format("{0}\\{1}", m_domain, sVal).ToLower();
                    }

                    ui[ibnName] = sVal;
                }
                ret.Add(ui);
            }
            return(ret);
        }
Exemple #7
0
        public static void Delete(int FieldId)
        {
            Ldap.CheckAccess();

            DbLdap.FieldDelete(FieldId);
        }
Exemple #8
0
        public static void SyncLogDeleteWithoutChanges()
        {
            Ldap.CheckAccess();

            DbLdap.SyncLogDeleteWithoutChanges();
        }
Exemple #9
0
        public static void SyncLogDelete(int logId)
        {
            Ldap.CheckAccess();

            DbLdap.SyncLogDelete(logId);
        }
Exemple #10
0
        /// <summary>
        /// Returns fields: LogId, LdapId, Dt, UserCount, Title.
        /// </summary>
        public static DataTable SyncLogGetList(int LogId)
        {
            Ldap.CheckAccess();

            return(DbLdap.SyncLogGetList(LogId, Security.CurrentUser.TimeZoneId));
        }
Exemple #11
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);
        }
Exemple #12
0
        /// <summary>
        /// Returns fields: LdapId, Title, Domain, Username, Password, Filter, IbnKey, LdapKey, Activate, Deactivate, Autosync, AutosyncStart, AutosyncInterval, LastSynchronization.
        /// </summary>
        public static IDataReader Get(int LdapId)
        {
            Ldap.CheckAccess();

            return(DbLdap.SettingsGet(LdapId));
        }
Exemple #13
0
        public static void Delete(int LdapId)
        {
            Ldap.CheckAccess();

            DbLdap.SettingsDelete(LdapId);
        }
Exemple #14
0
        public static int CreateUpdate(int LdapId
                                       , string Title
                                       , string Domain
                                       , string Username
                                       , string Password
                                       , string Filter
                                       , string IbnKey
                                       , string LdapKey
                                       , bool Activate
                                       , bool Deactivate
                                       , bool Autosync
                                       , DateTime AutosyncStart
                                       , int AutosyncInterval
                                       )
        {
            Ldap.CheckAccess();

            using (DbTransaction tran = DbTransaction.Begin())
            {
                bool create = LdapId < 0;

                LdapId = DbLdap.SettingsCreateUpdate(LdapId
                                                     , Title
                                                     , Domain
                                                     , Username
                                                     , Password
                                                     , Filter
                                                     , IbnKey
                                                     , LdapKey
                                                     , Activate
                                                     , Deactivate
                                                     , Autosync
                                                     , AutosyncStart
                                                     , AutosyncInterval
                                                     );

                if (create)
                {
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Login.ToString(), UserInfo.AdProperty.SamAccountName.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.FirstName.ToString(), UserInfo.AdProperty.GivenName.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.LastName.ToString(), UserInfo.AdProperty.Sn.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Email.ToString(), UserInfo.AdProperty.Mail.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.LdapUid.ToString(), UserInfo.AdProperty.ObjectSid.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.WindowsLogin.ToString(), UserInfo.AdProperty.SamAccountName.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Phone.ToString(), UserInfo.AdProperty.TelephoneNumber.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Mobile.ToString(), UserInfo.AdProperty.Mobile.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Fax.ToString(), UserInfo.AdProperty.FacsimileTelephoneNumber.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Location.ToString(), UserInfo.AdProperty.L.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Company.ToString(), UserInfo.AdProperty.Company.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.Department.ToString(), UserInfo.AdProperty.Department.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, false, UserInfo.IbnProperty.JobTitle.ToString(), UserInfo.AdProperty.Title.ToString(), 0, false, 0);
                    DbLdap.FieldCreateUpdate(-1, LdapId, true, UserInfo.IbnProperty.IsActive.ToString(), UserInfo.AdProperty.UserAccountControl.ToString(), 2, true, 0);
                }

                Ldap.UpdateNextSyncTime(LdapId);

                tran.Commit();
            }

            return(LdapId);
        }
Exemple #15
0
        public static void ProcessHandler(int handlerId, string argument, int hookId, int?objectId, Guid?objectUid, DateTime dateValue)
        {
            ObjectTypes objectType = ObjectTypes.UNDEFINED;
            DateTypes   dateType   = DateTypes.Undefined;

            if (handlerId == (int)DateTypeHandlers.SendReminder)
            {
                // Напоминать только о тех датах, которые еще не наступили.
                if (dateValue >= DateTime.UtcNow)
                {
                    bool sendReminder = false;

                    int userId = -1;
                    using (IDataReader reader = DbSchedule2.GetReminderSubscriptionByHookId(hookId))
                    {
                        if (reader.Read())
                        {
                            sendReminder = true;

                            if (reader["UserId"] != DBNull.Value)
                            {
                                userId = (int)reader["UserId"];

                                // O.R. [2010-04-01]: Don't process inactive user
                                if (User.GetUserActivity(userId) != User.UserActivity.Active)
                                {
                                    sendReminder = false;
                                }
                            }
                            objectType = (ObjectTypes)reader["ObjectTypeId"];
                            dateType   = (DateTypes)reader["DateTypeId"];
                        }
                    }

                    // Не напоминать о досрочно запущенных или досрочно завершённых объектах [2007-02-16]
                    if (sendReminder)
                    {
                        sendReminder = ConfirmNotification(objectType, dateType, objectId, objectUid);
                    }

                    if (sendReminder)
                    {
                        SubscriptionTypes subscriptionType = (SubscriptionTypes)int.Parse(argument, CultureInfo.InvariantCulture);

                        switch (subscriptionType)
                        {
                        case SubscriptionTypes.Global:
                            ProcessGlobalSubscription(hookId, dateType, objectType, objectId, objectUid);
                            break;

                        case SubscriptionTypes.Personal:
                            ProcessPersonalSubscription(hookId, dateType, userId, objectType, objectId, objectUid);
                            break;

                        case SubscriptionTypes.PersonalForObject:
                            ProcessPersonalSubscriptionForObject(hookId, dateType, userId, objectType, objectId, objectUid);
                            break;
                        }
                    }
                }
            }
            else if (handlerId == (int)DateTypeHandlers.RaiseSystemEvent)
            {
                bool raiseEvent = false;

                using (IDataReader reader = DbSchedule2.GetHook(hookId))
                {
                    if (reader.Read())
                    {
                        raiseEvent = true;

                        objectType = (ObjectTypes)reader["ObjectTypeId"];
                        dateType   = (DateTypes)reader["DateTypeId"];
                    }
                }

                if (raiseEvent)
                {
                    // Не уведомлять о досрочно запущенных или досрочно завершённых объектах [2008-07-29]
                    raiseEvent = ConfirmNotification(objectType, dateType, objectId, objectUid);

                    if (raiseEvent)
                    {
                        SystemEventTypes EventType = (SystemEventTypes)int.Parse(argument);
                        if (objectId.HasValue)
                        {
                            SystemEvents.AddSystemEvents(EventType, objectId.Value);
                        }
                        else if (objectUid.HasValue)
                        {
                            SystemEvents.AddSystemEvents(EventType, objectUid.Value);
                        }
                    }

                    if (objectType == ObjectTypes.Task)
                    {
                        Task.RecalculateAllStates(Task.GetProject(objectId.Value));
                    }
                    else if (objectType == ObjectTypes.ToDo)
                    {
                        ToDo.RecalculateState(objectId.Value);
                    }
                    else if (objectType == ObjectTypes.CalendarEntry)
                    {
                        CalendarEntry.RecalculateState(objectId.Value);
                    }
                }
            }
            else if (handlerId == (int)DateTypeHandlers.BatchAlert)
            {
                if (objectId.HasValue)
                {
                    Alerts2.SendBatch(objectId.Value, dateValue);
                }
            }
            else if (handlerId == (int)DateTypeHandlers.LdapSync)
            {
                if (objectId.HasValue)
                {
                    Ldap.Synchronize(objectId.Value, dateValue);
                }
            }
        }