Beispiel #1
0
        public string SaveCode(string snippetId, string code)
        {
            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                if (busSnippet.Load(snippetId) == null)
                {
                    throw new ArgumentException("Invalid snippetId passed.");
                }
                if (!IsEditAllowed(busSnippet.Entity) && !this.AppUserState.IsAdmin)
                {
                    throw new AccessViolationException("You are not allowed to edit this snippet.");
                }
                busSnippet.Entity.Code = StringUtils.NormalizeIndentation(code);

                if (busSnippet.IsSpam())
                {
                    throw new InvalidOperationException("Invalid content.");
                }

                if (!busSnippet.Save())
                {
                    throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
                }
            }
            return("ok");
        }
 public string SaveTitle(string snippetId,string newTitle)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         busSnippet.Entity.Title = newTitle;
         if (!busSnippet.Validate())
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         if (!busSnippet.Save())
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         return !string.IsNullOrEmpty(busSnippet.Entity.Title) ? busSnippet.Entity.Title : "No Title";
     }
 }
Beispiel #3
0
        /// <summary>
        /// Allows posting of a new code snippet.
        /// </summary>
        /// <param name="snippet"></param>
        /// <param name="sessionKey"></param>
        /// <returns></returns>
        public CodeSnippet PostNewCodeSnippet(CodeSnippet snippet, string sessionKey)
        {
            User user = this.ValidateToken(sessionKey);

            using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
            {
                if (snippet == null)
                {
                    this.ThrowException("Invalid snippet instance data passed");
                }
                CodeSnippet newSnippet = codeSnippet.NewEntity();
                // Force userId regardless of what the user has set
                newSnippet.UserId = user.Id;
                newSnippet.Author = user.Name;
                if (string.IsNullOrEmpty(newSnippet.Author))
                {
                    newSnippet.Author = snippet.Author;
                }

                if (string.IsNullOrEmpty(snippet.Language))
                {
                    snippet.Language = "NoFormat";
                }

                DataUtils.CopyObjectData(snippet, newSnippet, "Id,UserId,Entered,Views,UserId,User,Author,Comments");

                if (!codeSnippet.Validate())
                {
                    this.ThrowException("Snippet validation failed: " + codeSnippet.ValidationErrors.ToString());
                }

                if (codeSnippet.IsSpam())
                {
                    this.ThrowException("Invalid Content.");
                }

                if (!codeSnippet.Save())
                {
                    this.ThrowException("Failed to save snippet: " + codeSnippet.ErrorMessage);
                }
                return(newSnippet);
            }
        }
Beispiel #4
0
 public string SaveLanguage(string snippetId, string lang)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
         {
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         }
         busSnippet.Entity.Language = lang;
         if (!busSnippet.Save())
         {
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         }
         return("ok");
     }
 }
Beispiel #5
0
 public string SaveTags(string snippetId, string tags)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
         {
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         }
         busSnippet.Entity.Tags = tags;
         if (!busSnippet.Save())
         {
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         }
         string tagResult = busSnippet.GetTagLinkList(tags);
         return(tagResult);
     }
 }
Beispiel #6
0
 public bool ReportAbuse(string snippetId)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         var snippet = busSnippet.Entity;
         // switch value
         snippet.IsAbuse = !snippet.IsAbuse;
         if (snippet.IsAbuse)
         {
             AppWebUtils.SendEmail("CodePaste.NET Abuse: " + busSnippet.Entity.Title, "Abuse reported for this snippet \r\n\r\n" + WebUtils.ResolveServerUrl("~/" + busSnippet.Entity.Id), App.Configuration.AdminEmailAddress);
         }
         if (!busSnippet.Save())
         {
             throw new ApplicationException(busSnippet.ErrorMessage);
         }
         return(snippet.IsAbuse);
     }
 }
Beispiel #7
0
 public string SaveTitle(string snippetId, string newTitle)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
         {
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         }
         busSnippet.Entity.Title = newTitle;
         if (!busSnippet.Validate())
         {
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         }
         if (!busSnippet.Save())
         {
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         }
         return(!string.IsNullOrEmpty(busSnippet.Entity.Title) ? busSnippet.Entity.Title : "No Title");
     }
 }
Beispiel #8
0
        public string SaveMainComment(string snippetId, string comment)
        {
            busCodeSnippet busSnippet = new busCodeSnippet();

            if (busSnippet.Load(snippetId) == null)
            {
                throw new ArgumentException("Invalid snippetId passed.");
            }

            if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
            {
                throw new AccessViolationException("You are not allowed to edit this snippet.");
            }

            busSnippet.Entity.Comment = comment.Replace("\n", "\r\n");
            if (!busSnippet.Save())
            {
                throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
            }

            string tagResult = HtmlUtils.DisplayMemo(comment);

            return(tagResult);
        }
        public ActionResult New(FormCollection formValues)
        {
            ViewData["UserState"] = AppUserState;
            ViewData["languageList"] = this.GetLanguageList();

            busCodeSnippet busSnippet = new busCodeSnippet();
            CodeSnippet snippet = busSnippet.NewEntity();
            if (snippet == null)
            {
                ErrorDisplay.ShowError("Couldn't load snippet");
                return View(new CodeSnippet());
            }

            UpdateModel(snippet);                      

            if (!ValidateForSpam(snippet))
            {
                this.ErrorDisplay.ShowError("Invalid data posted back.");
                return View(snippet);
            }
            
            if (!busSnippet.Validate())
            {
                foreach (ValidationError error in busSnippet.ValidationErrors)
                {
                    this.ErrorDisplay.AddMessage(error.Message, error.ControlID);
                }
                this.ErrorDisplay.ShowError("Please correct the following:");
                return View(snippet);
            }

            // Assign the user from the authenticated user if any - otherwise blank
            // in which case the user can't edit entries.
            snippet.UserId = this.AppUserState.UserId;

            // 
            if (!string.IsNullOrEmpty(snippet.UserId))
            {
                var userBus = new busUser();
                var user = userBus.Load(snippet.UserId);
                if (user.InActive)
                {
                    ErrorDisplay.HtmlEncodeMessage = false;
                    ErrorDisplay.ShowError(
@"Your email address has not been validated yet so you
can't create posts for this account yet. Please click 
the following link and then check your email for a
message to validate your email.<br><br>
<a href='" +  Url.Content("~/Account/ResetEmailValidation") + 
"' target='emailreset'>Send Validation Request Email</a>");

                    return View(snippet);
                }
            }

            // strip of leading indentation always when capturing a new snippet
            snippet.Code = StringUtils.NormalizeIndentation(snippet.Code);                            
                       
            if (!busSnippet.Save())
            {
                this.ErrorDisplay.ShowError("Couldn't save snippet: " + busSnippet.ErrorMessage);
                return View(snippet);
            }

            return this.RedirectToAction("Show", new { id = busSnippet.Entity.Id });
        }
Beispiel #10
0
        public ActionResult New(FormCollection formValues)
        {
            ViewData["UserState"]    = AppUserState;
            ViewData["languageList"] = this.GetLanguageList();

            busCodeSnippet busSnippet = new busCodeSnippet();
            CodeSnippet    snippet    = busSnippet.NewEntity();

            if (snippet == null)
            {
                ErrorDisplay.ShowError("Couldn't load snippet");
                return(View(new CodeSnippet()));
            }

            UpdateModel(snippet);

            if (!ValidateForSpam(snippet))
            {
                this.ErrorDisplay.ShowError("Invalid data posted back.");
                return(View(snippet));
            }

            if (!busSnippet.Validate())
            {
                foreach (ValidationError error in busSnippet.ValidationErrors)
                {
                    this.ErrorDisplay.AddMessage(error.Message, error.ControlID);
                }
                this.ErrorDisplay.ShowError("Please correct the following:");
                return(View(snippet));
            }

            // Assign the user from the authenticated user if any - otherwise blank
            // in which case the user can't edit entries.
            snippet.UserId = this.AppUserState.UserId;

            //
            if (!string.IsNullOrEmpty(snippet.UserId))
            {
                var userBus = new busUser();
                var user    = userBus.Load(snippet.UserId);
                if (user.InActive)
                {
                    ErrorDisplay.HtmlEncodeMessage = false;
                    ErrorDisplay.ShowError(
                        @"Your email address has not been validated yet so you
can't create posts for this account yet. Please click 
the following link and then check your email for a
message to validate your email.<br><br>
<a href='" + Url.Content("~/Account/ResetEmailValidation") +
                        "' target='emailreset'>Send Validation Request Email</a>");

                    return(View(snippet));
                }
            }

            // strip of leading indentation always when capturing a new snippet
            snippet.Code = StringUtils.NormalizeIndentation(snippet.Code);

            if (!busSnippet.Save())
            {
                this.ErrorDisplay.ShowError("Couldn't save snippet: " + busSnippet.ErrorMessage);
                return(View(snippet));
            }

            return(this.RedirectToAction("Show", new { id = busSnippet.Entity.Id }));
        }
        public string SaveCode(string snippetId, string code )
        {
            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                if (busSnippet.Load(snippetId) == null)
                    throw new ArgumentException("Invalid snippetId passed.");
                if (!IsEditAllowed(busSnippet.Entity) && !this.AppUserState.IsAdmin)
                    throw new AccessViolationException("You are not allowed to edit this snippet.");
                busSnippet.Entity.Code = StringUtils.NormalizeIndentation(code);

                if (busSnippet.IsSpam())
                    throw new InvalidOperationException("Invalid content.");

                if (!busSnippet.Save())
                    throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
            }
            return "ok";
        }
 public bool ReportAbuse(string snippetId)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         var snippet = busSnippet.Entity;
         // switch value
         snippet.IsAbuse = !snippet.IsAbuse;
         if (snippet.IsAbuse)
         {
             AppWebUtils.SendEmail("CodePaste.NET Abuse: " + busSnippet.Entity.Title, "Abuse reported for this snippet \r\n\r\n" + WebUtils.ResolveServerUrl("~/" + busSnippet.Entity.Id), App.Configuration.AdminEmailAddress);
         }
         if (!busSnippet.Save())
             throw new ApplicationException(busSnippet.ErrorMessage);
         return snippet.IsAbuse;
     }
 }
        public string SaveMainComment(string snippetId, string comment)
        {
            busCodeSnippet busSnippet = new busCodeSnippet();
            if (busSnippet.Load(snippetId) == null)
                throw new ArgumentException("Invalid snippetId passed.");

            if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
                throw new AccessViolationException("You are not allowed to edit this snippet.");

            busSnippet.Entity.Comment = comment.Replace("\n","\r\n");
            if (!busSnippet.Save())
                throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);

            string tagResult = HtmlUtils.DisplayMemo(comment);
            return tagResult;
        }
 public string SaveTags(string snippetId, string tags)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         busSnippet.Entity.Tags = tags;
         if (!busSnippet.Save())
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         string tagResult = busSnippet.GetTagLinkList(tags);
         return tagResult;
     }
 }
 public string SaveLanguage(string snippetId, string lang)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         busSnippet.Entity.Language = lang;
         if (!busSnippet.Save())
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         return "ok";
     }
 }