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";
     }
 }
Exemple #2
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);
            }
        }
Exemple #3
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");
     }
 }
        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 });
        }
Exemple #5
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 }));
        }