Exemplo n.º 1
0
        /// <summary>
        /// Unsubscribes from journal and returns resulting xml object
        /// </summary>
        /// <param name="creator"></param>
        /// <param name="userId"></param>
        /// <param name="threadId"></param>
        /// <param name="forumId"></param>
        /// <returns></returns>
        static public SubscribeResult SubscribeToForum(IDnaDataReaderCreator creator, int userId, int forumId, bool unSubcribe)
        {
            SubscribeResult result = new SubscribeResult();
            string spToCall = string.Empty;
            if (unSubcribe)
            {
                spToCall = "unsubscribefromforum";
                result.FromForum = forumId;
            }
            else
            {
                spToCall = "subscribetoforum";
                result.ToForum = forumId;
            }

            //get permissions
            bool canRead = false;
            bool canWrite = false;
            ForumHelper helper = new ForumHelper(creator);
            helper.GetForumPermissions(userId, forumId, ref canRead, ref canWrite);

            if (canRead)
            {
                using (IDnaDataReader reader = creator.CreateDnaDataReader(spToCall))
                {
                    reader.AddParameter("userid", userId);
                    reader.AddParameter("forumid", forumId);
                    reader.Execute();
                }
            }
            else
            {
                result.Failed = 3;
                result.Value = "You don't have permission to read this forum";
            }

            
            return result;
        }
Exemplo n.º 2
0
        public void GetForumPermissions_ValidDataSet_ReturnsExpectedResults()
        {
            int canReadOut = 0;
            int canWriteOut = 0;
            IDnaDataReader reader = mocks.DynamicMock<IDnaDataReader>();
            reader.Stub(x => x.TryGetIntOutputParameter("CanRead", out canReadOut)).Return(true).OutRef(1);
            reader.Stub(x => x.TryGetIntOutputParameter("CanWrite", out canWriteOut)).Return(true).OutRef(1);
            reader.Stub(x => x.HasRows).Return(true);
            reader.Stub(x => x.Read()).Return(true);

            IDnaDataReaderCreator creator = mocks.DynamicMock<IDnaDataReaderCreator>();
            creator.Stub(x => x.CreateDnaDataReader("getforumpermissions")).Return(reader);
            mocks.ReplayAll();
 

            int userId = 0; 
            int forumId = 0; 
            bool canRead = false; 
            bool canReadExpected = true; 
            bool canWrite = false; 
            bool canWriteExpected = true;
            ForumHelper helper = new ForumHelper(creator);
            helper.GetForumPermissions(userId, forumId, ref canRead, ref canWrite);
            Assert.AreEqual(canReadExpected, canRead);
            Assert.AreEqual(canWriteExpected, canWrite);
        }
Exemplo n.º 3
0
        public void GetForumPermissionsTest_NoData_ReturnsUnchangedDefaults()
        {
            IDnaDataReader reader = mocks.DynamicMock<IDnaDataReader>();
            reader.Stub(x => x.HasRows).Return(false);
            reader.Stub(x => x.Read()).Return(false);

            IDnaDataReaderCreator creator = mocks.DynamicMock<IDnaDataReaderCreator>();
            creator.Stub(x => x.CreateDnaDataReader("getforumpermissions")).Return(reader);
            mocks.ReplayAll();


            int userId = 0;
            int forumId = 0;
            bool canRead = false;
            bool canReadExpected = false;
            bool canWrite = false;
            bool canWriteExpected = false;
            ForumHelper helper = new ForumHelper(creator);
            helper.GetForumPermissions(userId, forumId, ref canRead, ref canWrite);
            Assert.AreEqual(canReadExpected, canRead);
            Assert.AreEqual(canWriteExpected, canWrite);
        }
Exemplo n.º 4
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);
            }
        }