Exemplo n.º 1
0
        private static void LocalUsersCreate(DataTable localUsers, DataTable ldapUsers, Guid organizationId, ref Bll.LdapProcess ldapProcess, DomainUserCollection users, MasterDataSet.OrganizationsLdapGroupsDataTable orgTable, MasterDataSet.GroupMappingsDataTable groupMappings, bool isRealReplication)
        {
            int       count    = 0;
            DataTable newTable = null;

            DataRow[]     drm = null;
            StringBuilder sb  = null;

            DataRow[]           drm2 = null;
            Collection <string> altEmails;
            string     ldapGroupIds  = null;
            string     localGroupIds = null;
            Guid       loginId       = Guid.Empty;
            IUser      user          = null;
            DataRow    newRow        = null;
            DomainUser ldapUser      = null;
            string     email         = null;
            string     logMessage    = null;

            try
            {
                count    = 0;
                newTable = ldapUsers.Clone();

                foreach (DataRow dr in ldapUsers.Rows)
                {
                    try
                    {
                        if ((bool)dr["Processed"])
                        {
                            continue;
                        }

                        drm = localUsers.Select(string.Format(CultureInfo.InvariantCulture, "(LdapUserId='{0}') OR (LoginName='{1}')", dr["LdapUserId"].ToString().Replace("'", "''"), dr["LoginName"].ToString().Replace("'", "''")));
                        if (drm.Length == 0)
                        {
                            drm2      = null;
                            sb        = new StringBuilder();
                            altEmails = new Collection <string>();
                            for (int i = 0; i < users.Count; i++)
                            {
                                ldapUser = users[i];
                                if (ldapUser.ObjectGuid == (Guid)dr["LdapUserId"])
                                {
                                    if (ldapUser.AltEmails != null)
                                    {
                                        foreach (string altEmail in ldapUser.AltEmails)
                                        {
                                            if (altEmail.Contains("@"))
                                            {
                                                email = (altEmail.ToUpper(CultureInfo.InvariantCulture).Contains("SMTP")) ? altEmail.Remove(0, 5).ToString() : altEmail;
                                                if (string.Compare(ldapUser.EmailAddress, email, StringComparison.InvariantCultureIgnoreCase) != 0)
                                                {
                                                    altEmails.Add(email);
                                                    sb.AppendFormat(CultureInfo.InvariantCulture, " OR (LoginName='{0}')", (altEmail.ToUpper(CultureInfo.InvariantCulture).Contains("SMTP")) ? altEmail.Remove(0, 5).ToString().Replace("'", "''") : altEmail.Replace("'", "''"));
                                                }
                                            }
                                        }
                                    }

                                    if (sb.Length > 0)
                                    {
                                        sb.Remove(0, 4);
                                        drm2 = localUsers.Select(sb.ToString());
                                    }

                                    if (drm2 == null || drm2.Length == 0)
                                    {
                                        if (isRealReplication)
                                        {
                                            sb = new StringBuilder();

                                            foreach (string groupDN in ldapUser.MemberOfGroups)
                                            {
                                                for (int j = 0; j < orgTable.Rows.Count; j++)
                                                {
                                                    if (groupDN == orgTable.Rows[j]["DistinguishedName"].ToString())
                                                    {
                                                        sb.AppendFormat(CultureInfo.InvariantCulture, ",{0}", orgTable.Rows[j]["ObjectGUID"]);
                                                    }
                                                }
                                            }

                                            if (EmailProvider.IsEmailExists((string)dr["LoginName"]))
                                            {
                                                EmailProvider.DeleteEmails(Guid.Empty, (string)dr["LoginName"]);
                                            }

                                            ldapGroupIds  = (sb.Length > 0) ? sb.Remove(0, 1).ToString() : string.Empty;
                                            localGroupIds = LdapInfoProvider.GetAppGroupsByLdapGroups(organizationId, ldapGroupIds);
                                            loginId       = UserProvider.AddUserToOrganization((string)dr["LoginName"], ((dr["Email"]).GetType() == typeof(System.DBNull)) ? string.Empty : (string)dr["Email"], ((dr["FirstName"]).GetType() == typeof(System.DBNull)) ? string.Empty : (string)dr["FirstName"], ((dr["LastName"]).GetType() == typeof(System.DBNull)) ? string.Empty : (string)dr["LastName"], string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, localGroupIds, organizationId, DEFAULT_PASSWORD, false, false);
                                            UserProvider.RaiseUserInserted(loginId, organizationId, null, Bll.Support.ConvertStringToGuidList(localGroupIds));

                                            user = LdapInfoProvider.GetLdapUser(organizationId, ldapUser);
                                            if (user != null)
                                            {
                                                LoginProvider.Current.UpdateUserLdapInfo(organizationId, loginId, user.FirstName, user.LastName, user.LdapDomain, user.LdapDomainFull, user.LdapUserAlias, user.LdapUserPrinciple, user.UserSid, user.UserId, user.LdapOUPath);
                                                foreach (string altEmail in altEmails)
                                                {
                                                    if (!EmailProvider.IsEmailExists(altEmail))
                                                    {
                                                        EmailProvider.InsertEmail(altEmail, loginId);
                                                    }
                                                }
                                            }
                                        }

                                        dr["Processed"] = true;

                                        newRow           = newTable.NewRow();
                                        newRow.ItemArray = dr.ItemArray;
                                        newTable.Rows.Add(newRow);
                                        count++;

                                        ldapProcess.MessageCreatedLogins = string.Format(CultureInfo.InvariantCulture, isRealReplication ? Resources.OrganizationLdapSettingsControl_RealCreatedLogins_Text : Resources.OrganizationLdapSettingsControl_TestCreatedLogins_Text, count);
                                    }
                                    break;
                                }
                            }
                        }
                        else
                        {
                            dr["Processed"] = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        dr["Processed"] = true;
                        LdapInfoProvider.InsertLdapLog(organizationId, true, string.Format(CultureInfo.InvariantCulture, "<br/>{0}", ex.ToString().Replace("\r\n", "<br/>")));
                    }
                }

                ldapProcess.MessageCreatedLogins = string.Format(CultureInfo.InvariantCulture, isRealReplication ? Resources.OrganizationLdapSettingsControl_RealCreatedLogins_Text : Resources.OrganizationLdapSettingsControl_TestCreatedLogins_Text, count);

                if (count > 0)
                {
                    newTable.DefaultView.Sort     = "LoginName";
                    ldapProcess.DataCreatedLogins = Micajah.Common.Bll.Support.TrimDataView(newTable.DefaultView, 25);
                }
                logMessage = string.Format(CultureInfo.CurrentCulture, isRealReplication ? Resources.LdapProcessLog_RealReplicationUsersCreated : Resources.LdapProcessLog_TestReplicationUsersCreated, count);
                ldapProcess.Logs.Add(new LdapProcessLog()
                {
                    Date = DateTime.UtcNow, Message = logMessage
                });
                LdapInfoProvider.InsertLdapLog(organizationId, false, logMessage);
            }
            finally
            {
                if (newTable != null)
                {
                    newTable.Dispose();
                }
                drm = null;
                if (sb != null)
                {
                    sb.Remove(0, sb.Length);
                    sb = null;
                }
                altEmails     = null;
                drm2          = null;
                ldapGroupIds  = null;
                localGroupIds = null;
                loginId       = Guid.Empty;
                user          = null;
                newRow        = null;
                ldapUser      = null;
                email         = null;
                logMessage    = null;
            }
        }
Exemplo n.º 2
0
        private static void LocalUsersCheckGroups(Guid organizationId, DataTable localMappedLogins, DomainUserCollection users, MasterDataSet.OrganizationsLdapGroupsDataTable orgTable, MasterDataSet.GroupMappingsDataTable groupMappings)
        {
            DataView ldapGroups = null;

            ClientDataSet.UserRow userRow       = null;
            StringBuilder         newUserGroups = null;
            User          ldapUser       = null;
            DomainUser    ldapDomainUser = null;
            LoginProvider provider       = null;
            String        groupId        = null;
            Guid          ldapUserId     = Guid.Empty;
            StringBuilder sb             = null;
            String        email          = null;

            try
            {
                for (int i = 0; i < users.Count; i++)
                {
                    ldapDomainUser = users[i];
                    sb             = new StringBuilder();
                    sb.Append("(");
                    sb.AppendFormat("(LdapUserId='{0}') ", ldapDomainUser.ObjectGuid);
                    if (!string.IsNullOrEmpty(ldapDomainUser.PrincipalName))
                    {
                        sb.AppendFormat(" OR (LoginName = '{0}')", ldapDomainUser.PrincipalName.Replace("'", "''"));
                    }
                    else
                    {
                        ldapDomainUser.PrincipalName = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(ldapDomainUser.EmailAddress))
                    {
                        sb.AppendFormat(" OR (LoginName = '{0}')", ldapDomainUser.EmailAddress.Replace("'", "''"));
                    }
                    else
                    {
                        ldapDomainUser.EmailAddress = string.Empty;
                    }

                    sb.Append(")");

                    foreach (DataRow dr in localMappedLogins.Select(sb.ToString()))
                    {
                        try
                        {
                            newUserGroups = new StringBuilder(string.Empty);

                            try { ldapUserId = (Guid)dr["LdapUserId"]; }
                            catch { ldapUserId = Guid.Empty; }

                            if (ldapDomainUser.ObjectGuid == ldapUserId ||
                                ldapDomainUser.PrincipalName.ToLower(CultureInfo.InvariantCulture) == ((string)dr["LoginName"]).ToLower(CultureInfo.InvariantCulture) ||
                                (ldapDomainUser.EmailAddress != null && ldapDomainUser.EmailAddress.ToLower(CultureInfo.InvariantCulture) == ((string)dr["LoginName"]).ToLower(CultureInfo.InvariantCulture)))
                            {
                                foreach (string groupDN in ldapDomainUser.MemberOfGroups)
                                {
                                    for (int j = 0; j < orgTable.Rows.Count; j++)
                                    {
                                        if (groupDN == orgTable.Rows[j]["DistinguishedName"].ToString())
                                        {
                                            for (int k = 0; k < groupMappings.Rows.Count; k++)
                                            {
                                                if ((Guid)orgTable.Rows[j]["ObjectGUID"] == (Guid)groupMappings.Rows[k]["LdapGroupId"])
                                                {
                                                    if (!string.Concat(",", newUserGroups.ToString(), ",").Contains(string.Concat(",", (Guid)groupMappings.Rows[k]["GroupId"], ",")))
                                                    {
                                                        newUserGroups.AppendFormat(",{0}", (Guid)groupMappings.Rows[k]["GroupId"]);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                userRow = UserProvider.GetUserRow((Guid)dr["LoginId"], organizationId, true);
                                if (userRow == null)
                                {
                                    continue;
                                }

                                UserProvider.UpdateUser((Guid)dr["LoginId"], newUserGroups.ToString(), organizationId, false);

                                // Check user email
                                ldapUser = (User)LdapInfoProvider.GetLdapUser(organizationId, ldapDomainUser);
                                if (ldapUser != null)
                                {
                                    LoginProvider.Current.UpdateUserLdapInfo(organizationId, (Guid)dr["LoginId"], ldapUser.FirstName, ldapUser.LastName, ldapUser.LdapDomain, ldapUser.LdapDomainFull, ldapUser.LdapUserAlias, ldapUser.LdapUserPrinciple, ldapUser.UserSid, ldapUser.UserId, ldapUser.LdapOUPath);

                                    if (string.IsNullOrEmpty(ldapUser.EmailAddress) == false && string.Compare(userRow.Email, ldapUser.EmailAddress, StringComparison.CurrentCultureIgnoreCase) != 0)
                                    {
                                        provider = new LoginProvider();

                                        if (!provider.LoginNameExists(ldapUser.EmailAddress))
                                        {
                                            email = userRow.Email;
                                            UserProvider.UpdateUser(userRow.UserId, ldapUser.EmailAddress, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, groupId, organizationId, false);
                                        }

                                        if (provider != null)
                                        {
                                            provider = null;
                                        }
                                    }
                                }

                                UserProvider.RaiseUserUpdated(userRow.UserId, organizationId, Bll.Support.ConvertStringToGuidList(newUserGroups.ToString()), email);
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            LdapInfoProvider.InsertLdapLog(organizationId, true, string.Format(CultureInfo.InvariantCulture, "<br/>{0}", ex.ToString().Replace("\r\n", "<br/>")));
                        }
                    }
                }
            }
            finally
            {
                if (ldapGroups != null)
                {
                    ldapGroups.Dispose();
                }
                if (newUserGroups != null)
                {
                    newUserGroups.Remove(0, newUserGroups.Length);
                    newUserGroups = null;
                }
                if (sb != null)
                {
                    sb.Remove(0, sb.Length);
                    sb = null;
                }
                ldapUser = null;
                userRow  = null;
                provider = null;
                groupId  = null;
                email    = null;
            }
        }
Exemplo n.º 3
0
        protected void LdapProcessReconnectUserToLdap(object arg)
        {
            UserContext userContext = null;
            string      processId   = null;

            Bll.LdapProcess   ldapProcess = null;
            ApplicationLogger log         = null;
            LdapIntegration   ldap        = null;
            string            loginName   = null;

            LdapAdapter.IUser ldapUser = null;
            try
            {
                userContext = arg as UserContext;
                if (userContext != null)
                {
                    processId = string.Format("ReconnectUserToLdap_{0}_{1}", userContext.OrganizationId, this.UserId);

                    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.Message         = string.Empty;
                    ldapProcess.Data            = null;
                    LdapInfoProvider.LdapProcesses.Add(ldapProcess);

                    log       = new ApplicationLogger();
                    ldap      = new LdapIntegration(log);
                    loginName = LoginProvider.Current.GetLoginName(this.UserId);
                    ldapUser  = LdapInfoProvider.GetLdapUser(userContext.OrganizationId, loginName);
                    if (ldapUser != null)
                    {
                        ldap.CreateUserEmails(LdapInfoProvider.GetLdapUserAltEmails(userContext.OrganizationId, ldapUser.UserId, false), ldapUser);
                        LoginProvider.Current.UpdateUserLdapInfo(userContext.OrganizationId, this.UserId, ldapUser.FirstName, ldapUser.LastName, ldapUser.LdapDomain, ldapUser.LdapDomainFull, ldapUser.LdapUserAlias, ldapUser.LdapUserPrinciple, ldapUser.UserSid, ldapUser.UserId, ldapUser.LdapOUPath);
                    }
                    ldapProcess.ThreadStateType = Bll.ThreadStateType.Finished;
                }
            }
            catch (Exception ex)
            {
                string error = string.Format(CultureInfo.InvariantCulture, "<br/>{0}", ex.ToString().Replace("\r\n", "<br/>"));
                LdapInfoProvider.InsertLdapLog(userContext.OrganizationId, true, error);

                ldapProcess = LdapInfoProvider.LdapProcesses.Find(x => x.ProcessId == processId);
                if (ldapProcess != null)
                {
                    ldapProcess.ProcessId       = processId;
                    ldapProcess.ThreadStateType = Bll.ThreadStateType.Failed;
                    ldapProcess.MessageError    = error;
                    ldapProcess.Message         = string.Empty;
                    ldapProcess.Data            = null;
                }
            }
            finally
            {
                userContext = null;
                processId   = null;
                ldapProcess = null;
                log         = null;
                loginName   = null;
                ldapUser    = null;
            }
        }