private static void CreateYAFUser(string sUsername, string sPassword, string email) { if (!UserMembershipHelper.UserExists(sUsername, email)) { YafMembershipProvider mb = (YafMembershipProvider)System.Web.Security.Membership.Providers["YafMembershipProvider"]; int?forumUserID = 0; if (!mb.ValidateUser(sUsername, sPassword)) { MembershipCreateStatus status; MembershipUser forumUser = mb.CreateUser(sUsername, sPassword, email, "question", "answer", true, null, out status); // create the user in the YAF DB as well as sync roles... forumUserID = RoleMembershipHelper.CreateForumUser(forumUser, 1); RoleMembershipHelper.SetupUserRoles(1, sUsername); RoleMembershipHelper.AddUserToRole(sUsername, "Registered"); // create empty profile just so they have one YafUserProfile userProfile = YafUserProfile.GetProfile(sUsername); userProfile.Homepage = "fwd.com"; // setup their inital profile information userProfile.Save(); } else { return; } } }
/// <summary> /// Import the User From the Current Table Row /// </summary> /// <param name="row"> /// The row with the User Information. /// </param> /// <param name="importCount"> /// The import Count. /// </param> /// <returns> /// Returns the Imported User Count. /// </returns> private int ImportUser(DataRow row, int importCount) { // Also Check if the Email is unique and exists if (this.Get <IAspNetUsersHelper>().GetUserByEmail((string)row["Email"]) != null) { return(importCount); } var provider = new YafMembershipProvider(); var pass = provider.GeneratePassword(); var securityAnswer = provider.GeneratePassword(); // create empty profile just so they have one var userProfile = new ProfileInfo(); // Add Profile Fields to User List Table. if (row.Table.Columns.Contains("RealName") && ((string)row["RealName"]).IsSet()) { userProfile.RealName = (string)row["RealName"]; } if (row.Table.Columns.Contains("Blog") && ((string)row["Blog"]).IsSet()) { userProfile.Blog = (string)row["Blog"]; } if (row.Table.Columns.Contains("Gender") && ((string)row["Gender"]).IsSet()) { int.TryParse((string)row["Gender"], out var gender); userProfile.Gender = gender; } if (row.Table.Columns.Contains("Birthday") && ((string)row["Birthday"]).IsSet()) { DateTime.TryParse((string)row["Birthday"], out var userBirthdate); if (userBirthdate > DateTimeHelper.SqlDbMinTime()) { userProfile.Birthday = userBirthdate; } } if (row.Table.Columns.Contains("GoogleId") && ((string)row["GoogleId"]).IsSet()) { userProfile.GoogleId = (string)row["GoogleId"]; } if (row.Table.Columns.Contains("Location") && ((string)row["Location"]).IsSet()) { userProfile.Location = (string)row["Location"]; } if (row.Table.Columns.Contains("Country") && ((string)row["Country"]).IsSet()) { userProfile.Country = (string)row["Country"]; } if (row.Table.Columns.Contains("Region") && ((string)row["Region"]).IsSet()) { userProfile.Region = (string)row["Region"]; } if (row.Table.Columns.Contains("City") && ((string)row["City"]).IsSet()) { userProfile.City = (string)row["City"]; } if (row.Table.Columns.Contains("Interests") && ((string)row["Interests"]).IsSet()) { userProfile.Interests = (string)row["Interests"]; } if (row.Table.Columns.Contains("Homepage") && ((string)row["Homepage"]).IsSet()) { userProfile.Homepage = (string)row["Homepage"]; } if (row.Table.Columns.Contains("Skype") && ((string)row["Skype"]).IsSet()) { userProfile.Skype = (string)row["Skype"]; } if (row.Table.Columns.Contains("ICQe") && ((string)row["ICQ"]).IsSet()) { userProfile.ICQ = (string)row["ICQ"]; } if (row.Table.Columns.Contains("XMPP") && ((string)row["XMPP"]).IsSet()) { userProfile.XMPP = (string)row["XMPP"]; } if (row.Table.Columns.Contains("Occupation") && ((string)row["Occupation"]).IsSet()) { userProfile.Occupation = (string)row["Occupation"]; } if (row.Table.Columns.Contains("Twitter") && ((string)row["Twitter"]).IsSet()) { userProfile.Twitter = (string)row["Twitter"]; } if (row.Table.Columns.Contains("TwitterId") && ((string)row["TwitterId"]).IsSet()) { userProfile.TwitterId = (string)row["TwitterId"]; } if (row.Table.Columns.Contains("Facebook") && ((string)row["Facebook"]).IsSet()) { userProfile.Facebook = (string)row["Facebook"]; } if (row.Table.Columns.Contains("FacebookId") && ((string)row["FacebookId"]).IsSet()) { userProfile.FacebookId = (string)row["FacebookId"]; } var user = new AspNetUsers { Id = Guid.NewGuid().ToString(), ApplicationId = this.Get <BoardSettings>().ApplicationId, UserName = (string)row["Name"], LoweredUserName = (string)row["Name"], Email = (string)row["Email"], IsApproved = true, Profile_Birthday = userProfile.Birthday, Profile_Blog = userProfile.Blog, Profile_Gender = userProfile.Gender, Profile_GoogleId = userProfile.GoogleId, Profile_Homepage = userProfile.Homepage, Profile_ICQ = userProfile.ICQ, Profile_Facebook = userProfile.Facebook, Profile_FacebookId = userProfile.FacebookId, Profile_Twitter = userProfile.Twitter, Profile_TwitterId = userProfile.TwitterId, Profile_Interests = userProfile.Interests, Profile_Location = userProfile.Location, Profile_Country = userProfile.Country, Profile_Region = userProfile.Region, Profile_City = userProfile.City, Profile_Occupation = userProfile.Occupation, Profile_RealName = userProfile.RealName, Profile_Skype = userProfile.Skype, Profile_XMPP = userProfile.XMPP, Profile_LastSyncedWithDNN = userProfile.LastSyncedWithDNN }; this.Get <IAspNetUsersHelper>().Create(user, pass); // setup initial roles (if any) for this user AspNetRolesHelper.SetupUserRoles(BoardContext.Current.PageBoardID, user); // create the user in the YAF DB as well as sync roles... var userID = AspNetRolesHelper.CreateForumUser(user, BoardContext.Current.PageBoardID); if (userID == null) { // something is seriously wrong here -- redirect to failure... return(importCount); } // send user register notification to the new users this.Get <ISendNotification>().SendRegistrationNotificationToUser( user, pass, securityAnswer, "NOTIFICATION_ON_REGISTER"); // save the time zone... var userId = this.Get <IAspNetUsersHelper>().GetUserIDFromProviderUserKey(user.Id); var timeZone = 0; if (row.Table.Columns.Contains("Timezone") && ((string)row["Timezone"]).IsSet()) { int.TryParse((string)row["Timezone"], out timeZone); } var autoWatchTopicsEnabled = this.Get <BoardSettings>().DefaultNotificationSetting == UserNotificationSetting.TopicsIPostToOrSubscribeTo; this.GetRepository <User>().Save( userId, BoardContext.Current.PageBoardID, row["Name"], row.Table.Columns.Contains("DisplayName") ? row["DisplayName"] : null, row["Email"], timeZone, row.Table.Columns.Contains("LanguageFile") ? row["LanguageFile"] : null, row.Table.Columns.Contains("Culture") ? row["Culture"] : null, row.Table.Columns.Contains("ThemeFile") ? row["ThemeFile"] : null, false); // save the settings... this.GetRepository <User>().SaveNotification( userId, true, autoWatchTopicsEnabled, this.Get <BoardSettings>().DefaultNotificationSetting.ToInt(), this.Get <BoardSettings>().DefaultSendDigestEmail); importCount++; return(importCount); }