Exemplo n.º 1
0
        public void UpdateTermForModClassId_WithBlankTerm_ThrowsException()
        {
            var creator = Mocks.DynamicMock<IDnaDataReaderCreator>();


            Mocks.ReplayAll();

            var target = new Term { Value = "" };
            try
            {
                target.UpdateTermForModClassId(creator, 1, 1);
            }
            catch (Exception e)
            {
                Assert.AreEqual("Term value cannot be empty.", e.Message);
            }
            creator.AssertWasNotCalled(x => x.CreateDnaDataReader("addtermsfilterterm"));

        }
Exemplo n.º 2
0
        public void UpdateTermForModClassId_WithoutModclassId_ThrowsException()
        {
            var creator = Mocks.DynamicMock<IDnaDataReaderCreator>();
            //creator.Stub(x => x.CreateDnaDataReader("addtermsfilterterm")).Return(reader);

            Mocks.ReplayAll();

            var target = new Term{Value="term"}; 
            int modClassId = 0;

            try
            {
                target.UpdateTermForModClassId(creator, modClassId, 1);
            }
            catch (Exception e)
            {
                Assert.AreEqual("ModClassId cannot be 0.", e.Message);
            }
            creator.AssertWasNotCalled(x => x.CreateDnaDataReader("addtermsfilterterm"));
            
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a comment for the given comment forum id
        /// </summary>
        /// <param name="commentForum"></param>
        /// <param name="comment">The comment to add</param>
        /// <returns>The created comment object</returns>
        public CommentInfo CreateComment(Forum commentForum, CommentInfo comment)
        {
            ISite site = SiteList.GetSite(commentForum.SiteName);
            bool ignoreModeration;
            bool forceModeration;
            var notes = string.Empty;
            string profanityxml = string.Empty;
            
            List<Term> terms = null;

            ValidateComment(commentForum, comment, site, out ignoreModeration, out forceModeration, out notes, out terms);

            if (terms != null && terms.Count > 0)
            {
                profanityxml = new Term().GetProfanityXML(terms);
            }

            //create unique comment hash
            Guid guid = DnaHasher.GenerateCommentHashValue(comment.text, commentForum.Id, CallingUser.UserID);
            //add comment to db
            try
            {
                using (IDnaDataReader reader = CreateReader("commentcreate"))
                {
                    reader.AddParameter("commentforumid", commentForum.Id);
                    reader.AddParameter("userid", CallingUser.UserID);
                    if (commentForum.isContactForm)
                    {
                        reader.AddParameter("content", CONTACT_POST_TEXT);
                    }
                    else
                    {
                        reader.AddParameter("content", comment.text);
                    }
                    reader.AddParameter("hash", guid);
                    reader.AddParameter("forcemoderation", forceModeration);
                    //reader.AddParameter("forcepremoderation", (commentForum.ModerationServiceGroup == ModerationStatus.ForumStatus.PreMod?1:0));
                    reader.AddParameter("ignoremoderation", ignoreModeration);
                    reader.AddParameter("isnotable", CallingUser.IsUserA(UserTypes.Notable));
                    reader.AddParameter("applyprocesspremodexpirytime", comment.ApplyProcessPremodExpiryTime);

                    if (CallingUser.UserID != commentForum.NotSignedInUserId)
                    {//dont include as this is data protection
                        reader.AddParameter("ipaddress", IpAddress);
                        reader.AddParameter("bbcuid", BbcUid);
                    }

                    if (CallingUser.UserID == commentForum.NotSignedInUserId && comment.User != null && !String.IsNullOrEmpty(comment.User.DisplayName))
                    {//add display name for not signed in comment
                        reader.AddParameter("nickname", comment.User.DisplayName);
                    }
                    reader.AddIntReturnValue();
                    reader.AddParameter("poststyle", (int) comment.PostStyle);
                    if (!String.IsNullOrEmpty(notes))
                    {
                        reader.AddParameter("modnotes", notes);
                    }

                    if (false == string.IsNullOrEmpty(profanityxml))
                    {
                        reader.AddParameter("profanityxml", profanityxml);
                    }

                    reader.Execute();

                    if (reader.HasRows && reader.Read())
                    {
//all good - create comment
                        comment.PreModPostingsModId = reader.GetInt32NullAsZero("PreModPostingModId");
                        comment.IsPreModerated = (reader.GetInt32NullAsZero("IsPreModerated") == 1);
                        comment.hidden = (comment.IsPreModerated
                                              ? CommentStatus.Hidden.Hidden_AwaitingPreModeration
                                              : CommentStatus.Hidden.NotHidden);
                        comment.text = CommentInfo.FormatComment(comment.text, comment.PostStyle, comment.hidden, CallingUser.IsUserA(UserTypes.Editor));
                        var displayName = CallingUser.UserName;
                        if (CallingUser.UserID == commentForum.NotSignedInUserId && comment.User != null && !String.IsNullOrEmpty(comment.User.DisplayName))
                        {//add display name for not signed in comment
                            displayName = comment.User.DisplayName;
                        }
                        comment.User = UserReadByCallingUser(site);
                        comment.User.DisplayName = displayName;
                        comment.Created = new DateTimeHelper(DateTime.Now);

                        if (reader.GetInt32NullAsZero("postid") != 0)
                        {
// no id as it is may be pre moderated
                            comment.ID = reader.GetInt32NullAsZero("postid");
                            var replacement = new Dictionary<string, string>();
                            replacement.Add("sitename", site.SiteName);
                            replacement.Add("postid", comment.ID.ToString());
                            comment.ComplaintUri = UriDiscoverability.GetUriWithReplacments(BasePath,
                                                                                            SiteList.GetSiteOptionValueString(site.SiteID, "General", "ComplaintUrl")
                                                                                            , replacement);

                            replacement = new Dictionary<string, string>();
                            replacement.Add("commentforumid", commentForum.Id);
                            replacement.Add("sitename", site.SiteName);

                            UriDiscoverability.UriType uriType = UriDiscoverability.UriType.CommentForumById;
                            if (commentForum.isContactForm)
                            {
                                uriType = UriDiscoverability.UriType.ContactFormById;

                                // We now need to store the comment in the encrypted thread entries table.
                                using (IDnaDataReader contactDataReader = CreateReader("addencryptedcontactdetails"))
                                {
                                    contactDataReader.AddParameter("postid", comment.ID);
                                    contactDataReader.AddParameter("text", comment.text);
                                    contactDataReader.Execute();
                                }
                            }

                            comment.ForumUri = UriDiscoverability.GetUriWithReplacments(BasePath, uriType, replacement);
                        }
                        else
                        {
                            comment.ID = 0;
                        }
                    }
                    else
                    {
                        int returnValue;
                        reader.TryGetIntReturnValue(out returnValue);
                        ParseCreateCommentSpError(returnValue);
                    }
                }
            }
            catch (ApiException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ApiException(ex.Message, ex.InnerException);
            }
            //return new comment complete with id etc
            return comment;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a Reply comment to the given comment thread id
        /// </summary>
        /// <param name="commentForum">The forum containing the comment to post the reply to</param>
        /// <param name="threadId">The thread to post to</param>
        /// <param name="comment">The comment to add</param>
        /// <returns>The created comment object</returns>
        public CommentInfo CommentReplyCreate(Forum commentForum, int threadId, CommentInfo comment)
        {
            var site = SiteList.GetSite(commentForum.SiteName);
            bool ignoreModeration;
            bool forceModeration;
            var notes = string.Empty;
            string profanityxml = string.Empty;

            List<Term> terms = null;

            ValidateComment(commentForum, comment, site, out ignoreModeration, out forceModeration, out notes, out terms);

            if (terms != null && terms.Count > 0)
            {
                profanityxml = new Term().GetProfanityXML(terms);
            }

            //create unique comment hash
            var guid = DnaHasher.GenerateCommentHashValue(comment.text, commentForum.Id, CallingUser.UserID);
            //add comment to db
            try
            {
                using (IDnaDataReader reader = CreateReader("commentreplycreate"))
                {
                    reader.AddParameter("commentforumid", commentForum.Id);
                    reader.AddParameter("threadid", threadId);
                    reader.AddParameter("userid", CallingUser.UserID);
                    reader.AddParameter("content", comment.text);
                    reader.AddParameter("hash", guid);
                    reader.AddParameter("forcemoderation", forceModeration);
                    //reader.AddParameter("forcepremoderation", (commentForum.ModerationServiceGroup == ModerationStatus.ForumStatus.PreMod?1:0));
                    reader.AddParameter("ignoremoderation", ignoreModeration);
                    reader.AddParameter("isnotable", CallingUser.IsUserA(UserTypes.Notable));
                    reader.AddParameter("ipaddress", IpAddress);
                    reader.AddParameter("bbcuid", BbcUid);
                    reader.AddIntReturnValue();
                    reader.AddParameter("poststyle", (int) comment.PostStyle);
                    if (!String.IsNullOrEmpty(notes))
                    {
                        reader.AddParameter("modnotes", notes);
                    }

                    if (false == string.IsNullOrEmpty(profanityxml))
                    {
                        reader.AddParameter("profanityxml", profanityxml);
                    }

                    reader.Execute();
                    if (reader.HasRows && reader.Read())
                    {
                        //all good - create comment
                        comment.PreModPostingsModId = reader.GetInt32NullAsZero("PreModPostingModId");
                        comment.IsPreModerated = (reader.GetInt32NullAsZero("IsPreModerated") == 1);
                        comment.hidden = (comment.IsPreModerated
                                              ? CommentStatus.Hidden.Hidden_AwaitingPreModeration
                                              : CommentStatus.Hidden.NotHidden);
                        comment.User = UserReadByCallingUser(site);
                        comment.Created = new DateTimeHelper(DateTime.Now);

                        //count = reader.GetInt32NullAsZero("ThreadPostCount");

                        if (reader.GetInt32NullAsZero("postid") != 0)
                        {
                            // no id as it is may be pre moderated
                            comment.ID = reader.GetInt32NullAsZero("postid");
                            var replacement = new Dictionary<string, string>();
                            replacement.Add("sitename", site.SiteName);
                            replacement.Add("postid", comment.ID.ToString());
                            comment.ComplaintUri = UriDiscoverability.GetUriWithReplacments(BasePath,
                                                                                            SiteList.GetSiteOptionValueString(site.SiteID, "General", "ComplaintUrl")
                                                                                            , replacement);

                            replacement = new Dictionary<string, string>();
                            replacement.Add("commentforumid", commentForum.Id);
                            replacement.Add("sitename", site.SiteName);
                            comment.ForumUri = UriDiscoverability.GetUriWithReplacments(BasePath,
                                                                                        UriDiscoverability.UriType.
                                                                                            CommentForumById,
                                                                                        replacement);

                            comment.text = CommentInfo.FormatComment(comment.text, comment.PostStyle, comment.hidden, comment.User.Editor);
                        }
                        else
                        {
                            comment.ID = 0;
                        }
                    }
                    else
                    {
                        int returnValue;
                        reader.TryGetIntReturnValue(out returnValue);
                        ParseCreateCommentSpError(returnValue);
                    }
                }
            }
            catch (ApiException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ApiException(ex.Message, ex.InnerException);
            }
            //return new comment complete with id etc
            return comment;
        }
Exemplo n.º 5
0
        public void UpdateTermForModClassId_ValueInput_ReturnsNoException()
        {
            var reader = Mocks.DynamicMock<IDnaDataReader>();
            var creator = Mocks.DynamicMock<IDnaDataReaderCreator>();
            creator.Stub(x => x.CreateDnaDataReader("addtermsfilterterm")).Return(reader);

            Mocks.ReplayAll();

            var target = new Term { Value = "term" };
            target.UpdateTermForModClassId(creator, 1, 1);

            creator.AssertWasCalled(x => x.CreateDnaDataReader("addtermsfilterterm"));

        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates new post after checking relevant items...
        /// </summary>
        /// <param name="cacheManager"></param>
        /// <param name="readerCreator"></param>
        /// <param name="site"></param>
        /// <param name="viewingUser"></param>
        /// <param name="siteList"></param>
        /// <param name="forumId"></param>
        /// <param name="ThreadId"></param>
        /// <param name="_iPAddress"></param>
        /// <param name="bbcUidCookie"></param>
        public void PostToForum(ICacheManager cacheManager, IDnaDataReaderCreator readerCreator, ISite site,
            IUser viewingUser, ISiteList siteList, string _iPAddress, Guid bbcUidCookie, int forumId)
        {
            if (viewingUser.UserId == 0)
            {
                throw ApiException.GetError(ErrorType.NotAuthorized);
            }

            ForumSource forumSource = ForumSource.CreateForumSource(cacheManager, readerCreator, null, forumId, ThreadId, site.SiteID, false, false, false);
            if (forumSource == null)
            {
                throw ApiException.GetError(ErrorType.ForumUnknown);
            }

            bool isNotable = viewingUser.IsNotable;

            ForumHelper helper = new ForumHelper(readerCreator);
            bool ignoreModeration = viewingUser.IsEditor || viewingUser.IsSuperUser;
            // Check 4) check ThreadId exists and user has permission to write
            if (!ignoreModeration)
            {
                if (ThreadId != 0)
                {
                    bool canReadThread = false;
                    bool canWriteThread = false;
                    helper.GetThreadPermissions(viewingUser.UserId, ThreadId, ref canReadThread, ref canWriteThread);
                    if (!canReadThread)
                    {
                        throw ApiException.GetError(ErrorType.NotAuthorized);
                    }
                    if (!canWriteThread)
                    {
                        throw ApiException.GetError(ErrorType.ForumReadOnly);
                    }
                }
                else
                {
                    bool canReadForum = false;
                    bool canWriteForum = false;
                    helper.GetForumPermissions(viewingUser.UserId, forumId, ref canReadForum, ref canWriteForum);
                    if (!canReadForum)
                    {
                        throw ApiException.GetError(ErrorType.NotAuthorized);
                    }
                    if (!canWriteForum)
                    {
                        throw ApiException.GetError(ErrorType.ForumReadOnly);
                    }
                }
            }
        
            if (viewingUser.IsBanned)
            {
                throw ApiException.GetError(ErrorType.UserIsBanned);
            }
            
            if (!ignoreModeration && (site.IsEmergencyClosed || site.IsSiteScheduledClosed(DateTime.Now)))
            {
                throw ApiException.GetError(ErrorType.SiteIsClosed);
            }
            if (String.IsNullOrEmpty(Text))
            {
                throw ApiException.GetError(ErrorType.EmptyText);
            }
            try
            {

                int maxCharCount = siteList.GetSiteOptionValueInt(site.SiteID, "CommentForum", "MaxCommentCharacterLength");
                string tmpText = StringUtils.StripFormattingFromText(Text);
                if (maxCharCount != 0 && tmpText.Length > maxCharCount)
                {
                    throw ApiException.GetError(ErrorType.ExceededTextLimit);
                }
            }
            catch (SiteOptionNotFoundException)
            {
            }
            try
            {
                //check for option - if not set then it throws exception
                int minCharCount = siteList.GetSiteOptionValueInt(site.SiteID, "CommentForum", "MinCommentCharacterLength");
                string tmpText = StringUtils.StripFormattingFromText(Text);
                if (minCharCount != 0 && tmpText.Length < minCharCount)
                {
                    throw ApiException.GetError(ErrorType.MinCharLimitNotReached);
                }
            }
            catch (SiteOptionNotFoundException)
            {
            }

            //Only check xml parsing for richtext plain text we want what is there so smileys etc work
            //if (this.Style == PostStyle.Style.richtext)
            //{
            //    string errormessage = string.Empty;
            //    // Check to make sure that the comment is made of valid XML
            //    if (!HtmlUtils.ParseToValidGuideML(Text, ref errormessage))
            //    {
            //        throw ApiException.GetError(ErrorType.XmlFailedParse);
            //    }
            //}

            bool forceModeration;
            string matchingProfanity= string.Empty;
            string profanityxml = string.Empty;
            string postString = Subject + " " + Text;
            List<Term> terms = null;
            if (InReplyTo > 0)
            {//only check text if not first post
                postString = Text;
            }
            CheckForProfanities(site, postString, out forceModeration, out matchingProfanity, out terms, forumId);

            if (false == string.IsNullOrEmpty(matchingProfanity))
            {
                matchingProfanity = "Filtered terms: " + matchingProfanity; // Adding an extra bit of information for clarity
            }

            if (terms != null && terms.Count > 0)
            {
                profanityxml = new Term().GetProfanityXML(terms);
            }

            //check posting frequency
            if (!viewingUser.IsEditor && !viewingUser.IsSuperUser && !viewingUser.IsNotable)
            {
                SecondsToWait = CheckPostFrequency(readerCreator, viewingUser.UserId, site.SiteID);
                if (SecondsToWait != 0)
                {
                    var error =  ApiException.GetError(ErrorType.PostFrequencyTimePeriodNotExpired);
                    ApiException newError = new ApiException(
                        error.Message + " You must wait " + SecondsToWait.ToString() + " more seconds before posting.",
                        error.type);
                    throw newError;
                }
            }


            bool forcePreModeration = false;
            // PreModerate first post in discussion if site premoderatenewdiscussions option set.
            if ((InReplyTo == 0) && siteList.GetSiteOptionValueBool(site.SiteID, "Moderation", "PreModerateNewDiscussions"))
            {
                if (!ignoreModeration && !isNotable)
                {
                    forcePreModeration = true;
                }
            }

            

            if (forumSource.Type == ForumSourceType.Journal && ThreadId == 0)
            {
                CreateJournalPost(readerCreator, site.SiteID, viewingUser.UserId, viewingUser.UserName, forumId, false, _iPAddress, bbcUidCookie, forceModeration);
            }
            else
            {
                CreateForumPost(readerCreator, viewingUser.UserId, forumId, ignoreModeration, isNotable, _iPAddress, bbcUidCookie, false, false, forcePreModeration, forceModeration, matchingProfanity, profanityxml);
            }
        }