Modifying a Forum Post as an Admin through the front end changes the Forum Poster's Name

Matthew Cunliffe asked on June 27, 2017 15:21

Logging into Kentico as a Forum Administrator and then selecting the option in the forum front end to edit a post. Once the post is updated, the name of the poster changes to that of the forum administrator rather than remaining as the original poster's name.

Is this by design or a defect.

Kentico 8.0.24.

Recent Answers


Trevor Fayas answered on June 27, 2017 20:23

It's probably just going off of the default Kentico behavior, if there is a "ModifiedByUser" field specified for the class type then it will use whomever updated it, in other words, you.

If you want to change this, you can look to create a Hook for that object's update, and after update (or try to catch the before) set the "Modified" user to the Created User's ID.

https://docs.kentico.com/k8/custom-development/handling-global-events

https://docs.kentico.com/k8/custom-development/handling-global-events/reference-global-system-events

Specifically the <name>Info.TYPEINFO.Events.Update.Before/After events, i believe you want ForumPostInfo.TYPEINFO.Events

Try the below

using CMS.Base;
using CMS.DocumentEngine;
using CMS.Helpers;
using CMS.Membership;

[CustomDocumentEvents]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom handlers.
    /// </summary>
    private class CustomDocumentEventsAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// The system executes the Init method of the CMSModuleLoader attributes when the application starts.
        /// </summary>
        public override void Init()
        {
            CMS.Forums.ForumPostInfo.TYPEINFO.Events.Update.Before += Update_Before;
        }

        private void Update_Before(object sender, CMS.DataEngine.ObjectEventArgs e)
        {
            // Ensure the original post user id remains.
            int postID = ValidationHelper.GetInteger(e.Object.GetValue("PostId"), -1);
            var originalPost = CMS.Forums.ForumPostInfoProvider.GetForumPostInfo(postID);
            int newUserID = ValidationHelper.GetInteger(e.Object.GetValue("PostUserID"), -1);
            int originalUserID = originalPost.PostUserID;
            if (originalUserID > 0 && originalUserID != newUserID)
            {
                e.Object.SetValue("PostUserID", originalUserID);
            }
        }
    }
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.