/// <summary>
        /// The migrate users from <paramref name="dataTable"/>.
        /// </summary>
        /// <param name="approved">
        /// The approved.
        /// </param>
        /// <param name="dataTable">
        /// The dataTable.
        /// </param>
        private static void MigrateUsersFromDataTable(bool approved, [NotNull] DataTable dataTable)
        {
            // is this the Yaf membership provider?
            bool isYafProvider = YafContext.Current.Get <MembershipProvider>().Name == "YafMembershipProvider";
            bool isLegacyYafDB = dataTable.Columns.Contains("Location");

            var userRows = dataTable.AsEnumerable();

            Parallel.ForEach(
                userRows,
                row =>
            {
                // skip the guest user
                if (row.Field <bool>("IsGuest"))
                {
                    return;
                }

                // validate that name and email are available...
                if (row["Name"].IsNullOrEmptyDBField() || row["Email"].IsNullOrEmptyDBField())
                {
                    return;
                }

                string name  = row.Field <string>("Name").Trim();
                string email = row.Field <string>("Email").ToLower().Trim();

                // clean up the name by removing commas...
                name = name.Replace(",", string.Empty);

                // verify this user & email are not empty
                if (!name.IsSet() || !email.IsSet())
                {
                    return;
                }

                MembershipUser user = UserMembershipHelper.GetUser(name, false);

                if (user == null)
                {
                    MembershipCreateStatus status = MigrateCreateUser(
                        name,
                        email,
                        "Your email in all lower case",
                        email,
                        approved,
                        out user);

                    if (status != MembershipCreateStatus.Success)
                    {
                        YafContext.Current.Get <ILogger>()
                        .Log(0, "MigrateUsers", "Failed to create user {0}: {1}".FormatWith(name, status));
                    }
                    else
                    {
                        // update the YAF table with the ProviderKey -- update the provider table if this is the YAF provider...
                        LegacyDb.user_migrate(row["UserID"], user.ProviderUserKey, isYafProvider);

                        user.Comment = "Migrated from YetAnotherForum.NET";

                        YafContext.Current.Get <MembershipProvider>().UpdateUser(user);

                        if (!isYafProvider)
                        {
                            /* Email generated password to user
                             * System.Text.StringBuilder msg = new System.Text.StringBuilder();
                             * msg.AppendFormat( "Hello {0}.\r\n\r\n", name );
                             * msg.AppendFormat( "Here is your new password: {0}\r\n\r\n", password );
                             * msg.AppendFormat( "Visit {0} at {1}", ForumName, ForumURL );
                             *
                             * YAF.Classes.Data.DB.mail_create( ForumEmail, user.Email, "Forum Upgrade", msg.ToString() );
                             */
                        }
                    }

                    if (isLegacyYafDB)
                    {
                        // copy profile data over...
                        YafUserProfile userProfile = YafUserProfile.GetProfile(name);
                        if (dataTable.Columns.Contains("AIM") && !row["AIM"].IsNullOrEmptyDBField())
                        {
                            userProfile.AIM = row["AIM"].ToString();
                        }

                        if (dataTable.Columns.Contains("YIM") && !row["YIM"].IsNullOrEmptyDBField())
                        {
                            userProfile.YIM = row["YIM"].ToString();
                        }

                        if (dataTable.Columns.Contains("MSN") && !row["MSN"].IsNullOrEmptyDBField())
                        {
                            userProfile.MSN = row["MSN"].ToString();
                        }

                        if (dataTable.Columns.Contains("ICQ") && !row["ICQ"].IsNullOrEmptyDBField())
                        {
                            userProfile.ICQ = row["ICQ"].ToString();
                        }

                        if (dataTable.Columns.Contains("RealName") && !row["RealName"].IsNullOrEmptyDBField())
                        {
                            userProfile.RealName = row["RealName"].ToString();
                        }

                        if (dataTable.Columns.Contains("Occupation") &&
                            !row["Occupation"].IsNullOrEmptyDBField())
                        {
                            userProfile.Occupation = row["Occupation"].ToString();
                        }

                        if (dataTable.Columns.Contains("Location") && !row["Location"].IsNullOrEmptyDBField())
                        {
                            userProfile.Location = row["Location"].ToString();
                        }

                        if (dataTable.Columns.Contains("Homepage") && !row["Homepage"].IsNullOrEmptyDBField())
                        {
                            userProfile.Homepage = row["Homepage"].ToString();
                        }

                        if (dataTable.Columns.Contains("Interests") && !row["Interests"].IsNullOrEmptyDBField())
                        {
                            userProfile.Interests = row["Interests"].ToString();
                        }

                        if (dataTable.Columns.Contains("Weblog") && !row["Weblog"].IsNullOrEmptyDBField())
                        {
                            userProfile.Blog = row["Weblog"].ToString();
                        }

                        if (dataTable.Columns.Contains("Gender") && !row["Gender"].IsNullOrEmptyDBField())
                        {
                            userProfile.Gender = row["Gender"].ToType <int>();
                        }

                        userProfile.Save();
                    }
                }
                else
                {
                    // just update the link just in case...
                    LegacyDb.user_migrate(row["UserID"], user.ProviderUserKey, false);
                }

                // setup roles for this user...
                using (DataTable dtGroups = LegacyDb.usergroup_list(row["UserID"]))
                {
                    foreach (DataRow rowGroup in dtGroups.Rows)
                    {
                        AddUserToRole(user.UserName, rowGroup["Name"].ToString());
                    }
                }
            });
        }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CombinedUserDataHelper"/> class.
 /// </summary>
 /// <param name="membershipUser">
 /// The membership user.
 /// </param>
 public CombinedUserDataHelper(MembershipUser membershipUser)
     : this(membershipUser, UserMembershipHelper.GetUserIDFromProviderUserKey(membershipUser.ProviderUserKey))
 {
 }
        /// <summary>
        /// Updates the information in the YAF DB from the ASP.NET Membership user information.
        /// Called once per session for a user to sync up the data
        /// </summary>
        /// <param name="user">Current Membership User</param>
        /// <param name="pageBoardID">Current BoardID</param>
        /// <param name="roles">The DNN user roles.</param>
        /// <returns>
        /// The update forum user.
        /// </returns>
        public static int?UpdateForumUser([NotNull] MembershipUser user, int pageBoardID, string[] roles = null)
        {
            if (user == null)
            {
                // Check to make sure its not a guest
                return(null);
            }

            int userId = UserMembershipHelper.GetUserIDFromProviderUserKey(user.ProviderUserKey);

            if (userId == UserMembershipHelper.GuestUserId)
            {
                return(userId);
            }

            if (user.ProviderUserKey == null)
            {
                // problem -- log and move on...
                YafContext.Current.Get <ILogger>()
                .Log(
                    userId,
                    "UpdateForumUser",
                    "Null User Provider Key for UserName {0}. Please check your provider key settings for your ASP.NET membership provider."
                    .FormatWith(user.UserName));

                return(userId);
            }

            // is this a new user?
            bool isNewUser = userId <= 0;

            userId = LegacyDb.user_aspnet(
                pageBoardID,
                user.UserName,
                null,
                user.Email,
                user.ProviderUserKey,
                user.IsApproved);

            // get user groups...
            DataTable groupTable = LegacyDb.group_member(pageBoardID, userId);

            string[] userRoles = GetRolesForUser(user.UserName);

            if (Config.IsDotNetNuke && roles != null)
            {
                userRoles = roles;
            }

            if (Config.IsMojoPortal)
            {
                string roles1 = userRoles.Where(t => !string.IsNullOrEmpty(t))
                                .Aggregate(string.Empty, (current, t) => current.Trim() + "," + t.Trim());
                userRoles = roles1.Trim(',').Split(',');
            }

            // add groups...
            foreach (string role in userRoles.Where(role => !GroupInGroupTable(role, groupTable)))
            {
                // add the role...
                LegacyDb.user_setrole(pageBoardID, user.ProviderUserKey, role);
            }

            // remove groups...
            foreach (DataRow row in
                     groupTable.AsEnumerable().Where(row => !userRoles.Contains(row["Name"].ToString())))
            {
                // remove since there is no longer an association in the membership...
                LegacyDb.usergroup_save(userId, row["GroupID"], 0);
            }

            if (!isNewUser || userId <= 0)
            {
                return(userId);
            }

            try
            {
                UserNotificationSetting defaultNotificationSetting =
                    YafContext.Current.Get <YafBoardSettings>().DefaultNotificationSetting;

                bool defaultSendDigestEmail = YafContext.Current.Get <YafBoardSettings>().DefaultSendDigestEmail;

                // setup default notifications...
                bool autoWatchTopicsEnabled = defaultNotificationSetting
                                              == UserNotificationSetting.TopicsIPostToOrSubscribeTo;

                // save the settings...
                LegacyDb.user_savenotification(
                    userId,
                    true,
                    autoWatchTopicsEnabled,
                    defaultNotificationSetting,
                    defaultSendDigestEmail);
            }
            catch (Exception ex)
            {
                YafContext.Current.Get <ILogger>()
                .Log(
                    userId,
                    "UpdateForumUser",
                    "Failed to save default notifications for new user: {0}".FormatWith(ex));
            }

            return(userId);
        }