public Guid CreateLocalUser(IUser user, string groupId) { Guid loginId = Guid.Empty; if (user != null) { if (!string.IsNullOrEmpty((string.IsNullOrEmpty(user.EmailAddress) == true) ? user.LdapUserPrinciple : user.EmailAddress)) { string localGroupId = LdapInfoProvider.GetAppGroupsByLdapGroups(user.OrganizationId, groupId); loginId = UserProvider.AddUserToOrganization((string.IsNullOrEmpty(user.EmailAddress) == true) ? user.LdapUserPrinciple : user.EmailAddress, user.EmailAddress, user.FirstName, user.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, localGroupId, user.OrganizationId, user.Password, false, false); LoginProvider.Current.UpdateUserLdapInfo(user.OrganizationId, loginId, user.FirstName, user.LastName, user.LdapDomain, user.LdapDomainFull, user.LdapUserAlias, user.LdapUserPrinciple, user.UserSid, user.UserId, user.LdapOUPath); } } return(loginId); }
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; } }