Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tony Bolton 83 posts 109 karma points
    Apr 16, 2012 @ 21:48
    Tony Bolton
    0

    Notifications failing due to no user context

    Hi,

    This one has got me stumped, and I'm not sure if it's a bug with the core or if it's deliberate.  Hopefully someone will be able to advise which as I think it's the former!  This is for 4.7.1 and I haven't checked if the code is the same in 4.7.2 as yet - so that's my next task - but wanted to get this down before I do just in case.

    There's a user control within a standard umbraco page template within the site which adds a new document thru SendToPublish. A user has been created to handle the document creation for tracking purposes, which has an id of 5.  All good so far.

    When you first start the site up, it works fine and the nofication emails go out.  This'll go on for x period of time.  Eventually though, it gives up and fails to email the notification, with umbracoLog stating that there is a null reference exception:

    Error in notification: System.NullReferenceException: Object reference not set to an instance of an object.     at umbraco.cms.businesslogic.workflow.Notification.sendNotification(User performingUser, User mailingUser, Document documentObject, IAction Action) in C:\Documents and Settings\Administrator\My Documents\Downloads\umbraco-92d7d98ea3fe\umbraco_92d7d98ea3fe\umbraco\cms\businesslogic\workflow\Notification.cs:line 129     at umbraco.cms.businesslogic.workflow.Notification.GetNotifications(CMSNode Node, User user, IAction Action) in C:\Documents and Settings\Administrator\My Documents\Downloads\umbraco-92d7d98ea3fe\umbraco_92d7d98ea3fe\umbraco\cms\businesslogic\workflow\Notification.cs:line 62


    Tracking the issue through the source, ultimately it's these 2 sections causing the problem.  The first is in umbraco.BusinessLogic.Actions.Action where it attempts to find the current user:

    // Run notification
                // Find current user
                User u;
                try
                {
                    u = User.GetCurrent();
                }
                catch
                {
                    u = User.GetUser(0);
                }
                Notification.GetNotifications(d, u, action);


    With the actual cause of the problem happening within umbraco.BusinessLogic.User:

    public static User GetCurrent()
            {
                if (umbraco.BasePages.BasePage.umbracoUserContextID != "")
                    return BusinessLogic.User.GetUser(umbraco.BasePages.BasePage.GetUserId(umbraco.BasePages.BasePage.umbracoUserContextID));
                else
                    return null;
    
            }


    The problem stems from User.GetCurrent() returning a null value because 'umbraco.BasePages.BasePage.umbracoUserContextID is empty, which I presume is fine as the work to add the new document is being done in the backend via a non-logged in user id for the back office.

    Even so, with the umbracoUserContextID being empty, the code for Action.cs implies that a null reference is perfectly valid, yet there's a try/catch wrapped around it to just get the default user Admin (0):

     

    try
    {
       u = User.GetCurrent();
    }
    catch
    {
       u = User.GetUser(0);
    }

     

    The problem is, the null does not cause an exception in this block, therefore the u variable is always coming back as null - causing the subsequent line call to Notification.GetNotifications to fail, as u is passed through that ultimately to the method sendNotification in Notification.cs as the variable 'performingUser', which in a later line is just assumed to be populated (see bold):

    string[] bodyVars = {
       mailingUser.Name, ui.Text(Action.Alias), documentObject.Text, performingUser.Name,
       HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + ":" + 
       HttpContext.Current.Request.Url.Port.ToString() +
       IOHelper.ResolveUrl(SystemDirectories.Umbraco),
       documentObject.Id.ToString(), summary.ToString(), <snip>


    This then causes the error, preventing the notication being emailed out to any of the subscribers.

    I'm reasonably confident it's a bug, as if you login as a user to the back office, then run the form in the front-end, it works and sends out the notifications, as the cookie for the umbraco context is populated thus the call to User.GetCurrent returns a value.  As the user control is being called from 'normal' users with no back-office access, I'd expect SendToPublish to work.

    Hope someone can help clear this up - either I'm very wrong (most likely!) or it's something I've done wrong during the call to SendToPublish.

    Thanks in advance!

    Tony

     

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 16, 2012 @ 22:05
    Tom Fulton
    1

    Hi Tony,

    I've not looked into the issue but I did see this changeset get comitted a few weeks ago, wonder if it might help:  http://umbraco.codeplex.com/SourceControl/changeset/changes/5260a0b7cded#

    -Tom

  • Tony Bolton 83 posts 109 karma points
    Apr 16, 2012 @ 22:08
    Tony Bolton
    0

    Aha - cheers Tom.  That's interesting as I'm not using distributed calls.  I'll take a gander in more detail later - spent last 2 hours tracking that bug down and completely spent!

    Thanks for taking a look - really appreciated :)

    (and glad I wasn't going mad!)

  • Tony Bolton 83 posts 109 karma points
    Apr 16, 2012 @ 22:16
    Tony Bolton
    0

    Couldn't resist taking another look.  You're dead right - that changeset onwards up through 4.7.2 will fix the problem.  It's strange how it mentions distributed calls though, as the site is definitely not configured to use them according to umbracoSettings.config.

    At least this solves the immediate problem - and it's not my code that stopped them getting the notifications ;)  Happy Days!

    Thanks again, great stuff.

  • Simon steed 374 posts 686 karma points
    Nov 16, 2012 @ 10:43
    Simon steed
    0

    Hi Guys

    I'm having a similar issue related to this but when updating the cache for a newly created document after publishing. What i'm experiencing is:

     Document docNew = Document.MakeNew(type, MemberCategoryItem, author, parentNodeID);
                    if (docNew != null)
                    {
                        docNew.Save();
                        docNew.Publish(author);
                        umbraco.library.UpdateDocumentCache(docNew.Id); // update published xml
                        return docNew.Id;
                    }

     When the UpdateDocumentCache call is made, this calls on it's travels the Action Handler to get the current user then usually throws an exception in the following code:

    public static User GetCurrent()
            {
                    if (umbraco.BasePages.BasePage.umbracoUserContextID != "")
                        return BusinessLogic.User.GetUser(umbraco.BasePages.BasePage.GetUserId(umbraco.BasePages.BasePage.umbracoUserContextID));
                    else
                        return null;
            }

     

    umbraco.BasePages.BasePage.umbracoUserContextID is occasionally null (I say occasionally as it's usually ok) and as a result bombs out. The fix i've applied for the time being is to modidy GetCurrent as follows:

    public static User GetCurrent()
            {
                try
                {
                    if (umbraco.BasePages.BasePage.umbracoUserContextID != "")
                        return BusinessLogic.User.GetUser(umbraco.BasePages.BasePage.GetUserId(umbraco.BasePages.BasePage.umbracoUserContextID));
                    else
                        return null;
                }
                catch
                {
                    return User.GetUser(0);
                }
            }

     

    So we always return a valid user which then allows everything to run smoothly.

    Seems like this needs more investigation as to why umbraco.BasePages.BasePage.umbracoUserContextID is null in the first place, if anyone has any suggestions i'll gladly take them on board :-)

    Si 

Please Sign in or register to post replies

Write your reply to:

Draft