/// <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); }
/// <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... YafContext.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 = YafContext.Current.GetRepository <User>().Aspnet( pageBoardID, user.UserName, null, user.Email, user.ProviderUserKey, user.IsApproved); // get user groups... var groupTable = YafContext.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... foreach (var role in userRoles.Where(role => !GroupInGroupTable(role, groupTable))) { // add the role... YafContext.Current.GetRepository <User>().SetRole(pageBoardID, user.ProviderUserKey.ToString(), role); } // remove groups... foreach (var row in groupTable.AsEnumerable().Where(row => !userRoles.Contains(row["Name"].ToString()))) { // remove since there is no longer an association in the membership... YafContext.Current.GetRepository <UserGroup>().Save(userId, row["GroupID"], 0); } if (!isNewUser || userId <= 0) { return(userId); } try { var defaultNotificationSetting = YafContext.Current.Get <YafBoardSettings>().DefaultNotificationSetting; var defaultSendDigestEmail = YafContext.Current.Get <YafBoardSettings>().DefaultSendDigestEmail; // setup default notifications... var autoWatchTopicsEnabled = defaultNotificationSetting == UserNotificationSetting.TopicsIPostToOrSubscribeTo; // save the settings... YafContext.Current.GetRepository <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: {ex}"); } return(userId); }