Пример #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");
        }
Пример #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);
            }
        }
Пример #3
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";
        }