Exemplo n.º 1
0
        public DataSet GetSortedListOfRolesGroupsById(Guid organizationId)
        {
            ClientDataSet.GroupsInstancesRolesDataTable table = GroupProvider.GetGroupsInstancesRoles(organizationId);
            table.Columns.Add("RoleName", typeof(string));
            table.Columns.Add("GroupName", typeof(string));

            foreach (ClientDataSet.GroupsInstancesRolesRow row in table.Rows)
            {
                ConfigurationDataSet.RoleRow roleRow = RoleProvider.GetRoleRow((Guid)row["RoleId"]);
                if (row != null)
                {
                    row["RoleName"] = roleRow.Name;
                }

                MasterDataSet.GroupMappingsRow[] groups = LdapInfoProvider.GetGroupMappings(organizationId).Select(string.Format(CultureInfo.InvariantCulture, "GroupId = '{0}' AND OrganizationId = '{1}'", row["GroupId"], organizationId)) as MasterDataSet.GroupMappingsRow[];
                if (groups.Length > 0)
                {
                    row["GroupName"] = groups[0].LdapGroupName;
                }
            }

            return(table.DataSet);
        }
Exemplo n.º 2
0
        public void RunADReplication(Guid organizationId, bool isRealReplication)
        {
            string processId = null;

            Bll.LdapProcess      ldapProcess = null;
            DomainUserCollection users       = null;
            DataTable            localLogins = null;
            DataTable            ldapLogins  = null;
            //DataTable localMappedLogins = null;
            DataTable  activeLMLogins   = null;
            DataTable  inactiveLMLogins = null;
            DataTable  ldapActiveLogins = null;
            DataColumn newColumn        = null;
            DataRow    row    = null;
            DataRow    newRow = null;

            MasterDataSet.OrganizationsLdapGroupsDataTable orgTable      = null;
            MasterDataSet.GroupMappingsDataTable           groupMappings = null;
            Bll.Handlers.LdapHandler ldapHendler = null;
            string logMessage = null;

            try
            {
                if (isRealReplication)
                {
                    processId = string.Format(CultureInfo.InvariantCulture, "RealADReplication_{0}", organizationId);
                }
                else
                {
                    processId = string.Format(CultureInfo.InvariantCulture, "TestADReplication_{0}", organizationId);
                }

                ldapProcess = LdapInfoProvider.LdapProcesses.Find(x => x.ProcessId == processId);
                if (ldapProcess != null)
                {
                    LdapInfoProvider.LdapProcesses.Remove(ldapProcess);
                }

                ldapProcess                          = new Bll.LdapProcess();
                ldapProcess.ProcessId                = processId;
                ldapProcess.ThreadStateType          = Bll.ThreadStateType.Running;
                ldapProcess.MessageError             = string.Empty;
                ldapProcess.MessageCreatedLogins     = string.Empty;
                ldapProcess.MessageActivatedLogins   = string.Empty;
                ldapProcess.MessageDeactivatedLogins = string.Empty;
                ldapProcess.DataCreatedLogins        = null;
                ldapProcess.DataActivatedLogins      = null;
                ldapProcess.DataDeactivatedLogins    = null;
                ldapProcess.Logs                     = new List <LdapProcessLog>();
                LdapInfoProvider.LdapProcesses.Add(ldapProcess);

                if (isRealReplication)
                {
                    ldapProcess.MessageDeactivatedLogins = string.Format(CultureInfo.InvariantCulture, Resources.OrganizationLdapSettingsControl_RealDeactivatedLogins_Text, 0);
                    ldapProcess.MessageActivatedLogins   = string.Format(CultureInfo.InvariantCulture, Resources.OrganizationLdapSettingsControl_RealActivatedLogins_Text, 0);
                    ldapProcess.MessageCreatedLogins     = string.Format(CultureInfo.InvariantCulture, Resources.OrganizationLdapSettingsControl_RealCreatedLogins_Text, 0);
                }
                else
                {
                    ldapProcess.MessageDeactivatedLogins = string.Format(CultureInfo.InvariantCulture, Resources.OrganizationLdapSettingsControl_TestDeactivatedLogins_Text, 0);
                    ldapProcess.MessageActivatedLogins   = string.Format(CultureInfo.InvariantCulture, Resources.OrganizationLdapSettingsControl_TestActivatedLogins_Text, 0);
                    ldapProcess.MessageCreatedLogins     = string.Format(CultureInfo.InvariantCulture, Resources.OrganizationLdapSettingsControl_TestCreatedLogins_Text, 0);
                }

                DateTime startDate = DateTime.UtcNow;
                logMessage = string.Format(CultureInfo.CurrentCulture, Resources.LdapProcessLog_ReplicationStarted);
                ldapProcess.Logs.Add(new LdapProcessLog()
                {
                    Date = startDate, Message = logMessage
                });
                LdapInfoProvider.InsertLdapLog(organizationId, false, logMessage);

                // Get all mapped ldap groups users
                users = LdapInfoProvider.GetMappedGroupsUsers(organizationId, ref ldapProcess);

                if (users != null)
                {
                    // Get all local users
                    localLogins = LoginProvider.Current.GetLoginsByOrganizationId(organizationId);
                    localLogins.Columns.Add("Name", typeof(string));
                    foreach (DataRow dataRow in localLogins.Rows)
                    {
                        dataRow["Name"] = string.Concat(dataRow["FirstName"], " ", dataRow["LastName"]);
                    }

                    ldapLogins = localLogins.Clone();
                    ldapLogins.Columns.Add("Email", typeof(string));
                    foreach (DomainUser user in users)
                    {
                        if (!string.IsNullOrEmpty(user.EmailAddress) || !string.IsNullOrEmpty(user.PrincipalName))
                        {
                            row                   = ldapLogins.NewRow();
                            row["LoginId"]        = Guid.NewGuid();
                            row["LoginName"]      = (string.IsNullOrEmpty(user.EmailAddress)) ? user.PrincipalName : user.EmailAddress;
                            row["Name"]           = string.Concat(user.FirstName, " ", user.LastName);
                            row["FirstName"]      = user.FirstName ?? string.Empty;
                            row["LastName"]       = user.LastName ?? string.Empty;
                            row["Email"]          = (string.IsNullOrEmpty(user.EmailAddress)) ? ((user.PrincipalName ?? string.Empty).Contains("@") ? user.PrincipalName : string.Empty) : user.EmailAddress;
                            row["LdapUserId"]     = user.ObjectGuid;
                            row["Active"]         = user.IsActive;
                            row["Password"]       = DEFAULT_PASSWORD;
                            row["ProfileUpdated"] = DateTime.Now;
                            row["Deleted"]        = false;
                            ldapLogins.Rows.Add(row);
                        }
                    }

                    // Get active mapped local users
                    activeLMLogins = localLogins.Clone();
                    foreach (DataRow activeLM in localLogins.Select("(Active = 1)"))
                    {
                        newRow           = activeLMLogins.NewRow();
                        newRow.ItemArray = activeLM.ItemArray;
                        activeLMLogins.Rows.Add(newRow);
                    }

                    LocalUsersDeactivate(activeLMLogins, ldapLogins, organizationId, ref ldapProcess, isRealReplication);

                    // Get inactive mapped local users
                    inactiveLMLogins = localLogins.Clone();
                    foreach (DataRow inactiveLM in localLogins.Select("(Active = 0)"))
                    {
                        newRow           = inactiveLMLogins.NewRow();
                        newRow.ItemArray = inactiveLM.ItemArray;
                        inactiveLMLogins.Rows.Add(newRow);
                    }

                    LocalUsersActivate(inactiveLMLogins, ldapLogins, organizationId, ref ldapProcess, isRealReplication);

                    // Get active ldap users
                    ldapActiveLogins = ldapLogins.Clone();
                    foreach (DataRow ldapActive in ldapLogins.Select("(Active = 1)"))
                    {
                        if (!string.IsNullOrEmpty(ldapActive["LoginName"] != null ? ldapActive["LoginName"].ToString() : string.Empty))
                        {
                            newRow           = ldapActiveLogins.NewRow();
                            newRow.ItemArray = ldapActive.ItemArray;
                            ldapActiveLogins.Rows.Add(newRow);
                        }
                    }
                    newColumn = new DataColumn("Processed", typeof(bool));
                    newColumn.DefaultValue = false;
                    ldapActiveLogins.Columns.Add(newColumn);

                    using (OrganizationsLdapGroupsTableAdapter adapter = new OrganizationsLdapGroupsTableAdapter())
                    {
                        orgTable = adapter.GetOrganizationsLdapGroupsByOrganizationId(organizationId);

                        if (orgTable.Rows.Count == 0)
                        {
                            ldapHendler = new Bll.Handlers.LdapHandler();
                            ldapHendler.ImportLdapGroups(organizationId);

                            orgTable = adapter.GetOrganizationsLdapGroupsByOrganizationId(organizationId);
                        }
                    }

                    groupMappings = LdapInfoProvider.GetGroupMappings(organizationId);

                    if (isRealReplication)
                    {
                        logMessage = string.Format(CultureInfo.CurrentCulture, Resources.LdapProcessLog_UpdatingUserAccounts, users.Count);
                        ldapProcess.Logs.Add(new LdapProcessLog()
                        {
                            Date = DateTime.UtcNow, Message = logMessage
                        });
                        LdapInfoProvider.InsertLdapLog(organizationId, false, logMessage);
                        LocalUsersCheckGroups(organizationId, activeLMLogins, users, orgTable, groupMappings);
                        logMessage = string.Format(CultureInfo.CurrentCulture, Resources.LdapProcessLog_UpdateFinished);
                        ldapProcess.Logs.Add(new LdapProcessLog()
                        {
                            Date = DateTime.UtcNow, Message = logMessage
                        });
                        LdapInfoProvider.InsertLdapLog(organizationId, false, logMessage);
                    }

                    LocalUsersCreate(localLogins, ldapActiveLogins, organizationId, ref ldapProcess, users, orgTable, groupMappings, isRealReplication);
                }

                ldapProcess.ThreadStateType = Bll.ThreadStateType.Finished;
                logMessage = string.Format(CultureInfo.CurrentCulture, Resources.LdapProcessLog_ReplicationFinished, Math.Round((DateTime.UtcNow - startDate).TotalMinutes, 1));
                ldapProcess.Logs.Add(new LdapProcessLog()
                {
                    Date = DateTime.UtcNow, Message = logMessage
                });
                LdapInfoProvider.InsertLdapLog(organizationId, false, logMessage);
            }
            catch (Exception ex)
            {
                ldapProcess = LdapInfoProvider.LdapProcesses.Find(x => x.ProcessId == processId);
                if (ldapProcess != null)
                {
                    ldapProcess.ProcessId                = processId;
                    ldapProcess.ThreadStateType          = Bll.ThreadStateType.Failed;
                    ldapProcess.MessageError             = string.Format(CultureInfo.InvariantCulture, "<br/>{0}", ex.ToString().Replace("\r\n", "<br/>"));
                    ldapProcess.MessageCreatedLogins     = string.Empty;
                    ldapProcess.MessageActivatedLogins   = string.Empty;
                    ldapProcess.MessageDeactivatedLogins = string.Empty;
                    ldapProcess.DataCreatedLogins        = null;
                    ldapProcess.DataActivatedLogins      = null;
                    ldapProcess.DataDeactivatedLogins    = null;

                    LdapInfoProvider.InsertLdapLog(organizationId, true, ldapProcess.MessageError);
                }
            }
            finally
            {
                processId   = null;
                ldapProcess = null;
                users       = null;
                if (localLogins != null)
                {
                    localLogins.Dispose();
                }
                if (ldapLogins != null)
                {
                    ldapLogins.Dispose();
                }
                //if (localMappedLogins != null) localMappedLogins.Dispose();
                if (activeLMLogins != null)
                {
                    activeLMLogins.Dispose();
                }
                if (inactiveLMLogins != null)
                {
                    inactiveLMLogins.Dispose();
                }
                if (ldapActiveLogins != null)
                {
                    ldapActiveLogins.Dispose();
                }
                if (newColumn != null)
                {
                    newColumn.Dispose();
                }
                if (orgTable != null)
                {
                    orgTable.Dispose();
                }
                row    = null;
                newRow = null;
                if (groupMappings != null)
                {
                    groupMappings.Dispose();
                }
                ldapHendler = null;
                logMessage  = null;
            }
        }