Exemplo n.º 1
0
        /// <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);
            }

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

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

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

                return(userId);
            }

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

            userId = BoardContext.Current.GetRepository <User>().Aspnet(
                pageBoardID,
                user.UserName,
                null,
                user.Email,
                user.ProviderUserKey,
                user.IsApproved);

            // get user groups...
            var groupTable = BoardContext.Current.GetRepository <Group>().MemberAsDataTable(pageBoardID, userId);
            var userRoles  = GetRolesForUser(user.UserName);

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

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

            // add groups...
            userRoles.Where(role => !GroupInGroupTable(role, groupTable)).ForEach(
                role => BoardContext.Current.GetRepository <User>().SetRole(
                    pageBoardID,
                    user.ProviderUserKey.ToString(),
                    role));

            // remove groups...remove since there is no longer an association in the membership...
            groupTable.AsEnumerable().Where(row => !userRoles.Contains(row["Name"].ToString())).ForEach(
                row => BoardContext.Current.GetRepository <UserGroup>().Save(userId, row["GroupID"], 0));

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

            try
            {
                var defaultNotificationSetting = BoardContext.Current.Get <BoardSettings>().DefaultNotificationSetting;

                var defaultSendDigestEmail = BoardContext.Current.Get <BoardSettings>().DefaultSendDigestEmail;

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

                // save the settings...
                BoardContext.Current.GetRepository <User>().SaveNotification(
                    userId,
                    true,
                    autoWatchTopicsEnabled,
                    defaultNotificationSetting.ToInt(),
                    defaultSendDigestEmail);
            }
            catch (Exception ex)
            {
                BoardContext.Current.Get <ILogger>().Log(
                    userId,
                    "UpdateForumUser",
                    $"Failed to save default notifications for new user: {ex}");
            }

            return(userId);
        }
Exemplo 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))
 {
 }