Esempio n. 1
0
        /// <summary>
        /// The handle.
        /// </summary>
        /// <param name="event">
        /// The event.
        /// </param>
        /// <exception cref="ApplicationException">Failed to find guest user.</exception>
        /// <exception cref="ApplicationException">Failed to create new user.</exception>
        /// <exception cref="ApplicationException">Unable to find the Guest User!</exception>
        public void Handle([NotNull] InitPageLoadEvent @event)
        {
            try
            {
                object userKey = null;

                if (BoardContext.Current.User != null)
                {
                    userKey = BoardContext.Current.User.Id;
                }

                var     tries = 0;
                DataRow pageRow;
                var     forumPage = this.Get <HttpRequestBase>().QueryString.ToString();
                var     location  = this.Get <HttpRequestBase>().FilePath;

                // resources are not handled by ActiveLocation control so far.
                if (location.Contains("resource.ashx"))
                {
                    forumPage = string.Empty;
                    location  = string.Empty;
                }

                do
                {
                    pageRow = this.GetRepository <ActiveAccess>().PageLoadAsDataRow(
                        this.Get <HttpSessionStateBase>().SessionID,
                        BoardContext.Current.PageBoardID,
                        userKey,
                        this.Get <HttpRequestBase>().GetUserRealIPAddress(),
                        location,
                        forumPage,
                        (string)@event.Data.Browser,
                        (string)@event.Data.Platform,
                        (int?)@event.Data.CategoryID,
                        (int?)@event.Data.ForumID,
                        (int?)@event.Data.TopicID,
                        (int?)@event.Data.MessageID,
                        (bool)@event.Data.IsSearchEngine,
                        (bool)@event.Data.IsMobileDevice,
                        (bool)@event.Data.DontTrack);

                    // if the user doesn't exist...
                    if (userKey != null && pageRow == null)
                    {
                        // create the user...
                        if (
                            !AspNetRolesHelper.DidCreateForumUser(
                                BoardContext.Current.User, BoardContext.Current.PageBoardID))
                        {
                            throw new ApplicationException("Failed to create new user.");
                        }
                    }

                    if (tries++ < 2)
                    {
                        continue;
                    }

                    if (userKey != null && pageRow == null)
                    {
                        // probably no permissions, use guest user instead...
                        userKey = null;
                        continue;
                    }

                    // fail...
                    break;
                }while (pageRow == null && userKey != null);

                if (pageRow == null)
                {
                    throw new ApplicationException("Unable to find the Guest User!");
                }

                // add all loaded page data into our data dictionary...
                @event.DataDictionary.AddRange(pageRow.ToDictionary());

                // clear active users list
                if (@event.DataDictionary["ActiveUpdate"].ToType <bool>())
                {
                    // purge the cache if something has changed...
                    this.DataCache.Remove(Constants.Cache.UsersOnlineStatus);
                }
            }
            catch (Exception x)
            {
#if !DEBUG
                // log the exception...
                this.Logger.Fatal(
                    x,
                    "Failure Initializing User/Page (URL: {0}).",
                    this.Get <HttpRequestBase>().Url.ToString());

                // log the user out...
                // FormsAuthentication.SignOut();
                if (BoardContext.Current.ForumPageType != ForumPages.Info)
                {
                    // show a failure notice since something is probably up with membership...
                    BuildLink.RedirectInfoPage(InfoMessage.Failure);
                }
                else
                {
                    // totally failing... just re-throw the exception...
                    throw;
                }
#else
                // re-throw exception...
                throw;
#endif
            }
        }