示例#1
0
        public ActionResult Edit(AdminPageSave pageSave)
        {
            Page editedPage = pageSave.Id.HasValue ? db.Pages.Include(x => x.Author).Single(x => x.Id == pageSave.Id)
                                  : db.Pages.Add(new Page
            {
                CreationDate = DateTime.UtcNow,
            }).Entity;

            var newAuthor = db.Users.Single(x => x.Id == pageSave.AuthorId);

            if (CheckPagesSaveRoles(pageSave, editedPage, newAuthor) == false)
            {
                return(Forbid());
            }

            editedPage.Author = newAuthor;

            editedPage.LastModificationDate = DateTime.UtcNow;

            pageSave.FillPage(editedPage, dateTimeUtil);

            if (db.Pages.Any(x => x.Slug == editedPage.Slug && x.Id != editedPage.Id))
            {
                ModelState.AddModelError("Slug", "This slug already exists");
            }

            if (ModelState.IsValid)
            {
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(CreateAdminPageSave(editedPage)));
        }
        public virtual ActionResult ApproveSelected(IEnumerable <int> selectedComments)
        {
            List <PostComment> comments = db.PostComments.Where(x => selectedComments.Contains(x.Id)).ToList();
            var moderatedBy             = db.Users.Single(x => x.UserName == User.Identity.Name);

            foreach (PostComment postComment in comments)
            {
                postComment.ModeratedBy = moderatedBy;
                postComment.IsApproved  = true;
            }

            db.SaveChanges();

            return(Json(true));
        }
        public ActionResult Add(string zoneName, string widgetName)
        {
            if (string.IsNullOrEmpty(widgetName))
            {
                return(Json(false));
            }

            WidgetSetting widgetSetting = new WidgetSetting
            {
                Name       = widgetName,
                WidgetZone = zoneName,
                Position   = int.MaxValue
            };

            widgetSettingRepository.Add(widgetSetting);
            db.SaveChanges();
            return(View(viewsPath + widgetSetting.Name + ".cshtml", widgetSetting));
        }
示例#4
0
        public virtual ActionResult Edit([Bind(Prefix = "Post")] AdminPostSave postSave)
        {
            Post editedPost = postSave.Id.HasValue ? db.Posts.Include(x => x.Author).Include(x => x.PostTags).ThenInclude(x => x.Tag).Include(x => x.PostCategories).ThenInclude(x => x.Category).Single(x => x.Id == postSave.Id)
                                  : db.Posts.Add(new Post
            {
                CreationDate = DateTime.UtcNow,
                Slug         = postSave.Slug,
            }).Entity;

            var newAuthor = userRepository.GetById(postSave.AuthorId); //postSave.AuthorId.HasValue ? userRepository.GetById(postSave.AuthorId.Value) : userRepository.GetByUsername(User.Identity.Name);

            if (CheckPostsSaveRoles(postSave, editedPost, newAuthor) == false)
            {
                return(Forbid());
            }

            editedPost.Author = newAuthor;
            editedPost.LastModificationDate = DateTime.UtcNow;
            postSave.FillPost(editedPost, dateTimeUtil);

            if (db.Posts.Any(x => x.Slug == editedPost.Slug && x.Id != editedPost.Id))
            {
                ModelState.AddModelError("Post.Slug", "This slug already exists");
            }

            IEnumerable <string> tags = from tag in (postSave.Tags ?? string.Empty).Split(new[] { ',' })
                                        where !string.IsNullOrWhiteSpace(tag)
                                        select tag.Trim();

            var existingTags = editedPost.PostTags.Select(x => x.Tag.Name).ToList();

            foreach (var removedTag in editedPost.PostTags.Where(x => tags.Contains(x.Tag.Name, StringComparer.OrdinalIgnoreCase) == false).ToList())
            {
                editedPost.PostTags.Remove(removedTag);
            }

            foreach (var item in postRepository.GetOrCreateTags(tags.Where(x => existingTags.Contains(x, StringComparer.OrdinalIgnoreCase) == false)))
            {
                editedPost.PostTags.Add(new PostTag()
                {
                    Tag = item
                });
            }

            postSave.SelectedCategories = postSave.SelectedCategories ?? Enumerable.Empty <int>();
            var categoriesToAdd = postSave.SelectedCategories.Except(editedPost.PostCategories.Select(x => x.CategoryId));

            foreach (var removedCategory in editedPost.PostCategories.Where(x => postSave.SelectedCategories.Contains(x.CategoryId) == false).ToList())
            {
                editedPost.PostCategories.Remove(removedCategory);
            }

            foreach (Category category in db.Categories.Where(x => categoriesToAdd.Contains(x.Id)))
            {
                editedPost.PostCategories.Add(new PostCategory()
                {
                    Category = category
                });
            }

            if (ModelState.IsValid)
            {
                db.SaveChanges();
                CreatePostRevision(editedPost);
                return(RedirectToAction("Details", "Post", new { postSlug = editedPost.Slug }));
            }

            return(View(CreateAdminPostEdit(editedPost)));
        }
示例#5
0
        public async Task <ActionResult> DoComment(int id, int?parentCommentId)
        {
            if (settings.ReCaptchaEnabled && !HttpContext.UserHasClaimPermission(PermissionClaims.ModerateComments) && (await ValidateRecaptcha(Request, settings, Request.Form["g-recaptcha-response"]) == false))
            {
                return(Json(new { errorMessage = "Invalid Captcha!" }));
            }

            bool isLogged = User.Identity.IsAuthenticated;
            Post post     = db.Posts.Include(x => x.Author).WhereHaveRoles(HttpContext).SingleOrDefault(x => x.Id == id);

            if (post == null || !post.IsCommentEnabled || !settings.EnableComments)
            {
                throw new InvalidOperationException();
            }

            if (settings.CloseCommentsAfterDays.HasValue && post.PublicationDate.AddDays(settings.CloseCommentsAfterDays.Value) < DateTime.UtcNow)
            {
                throw new InvalidOperationException();
            }

            PostComment postComment = new PostComment
            {
                CreationDate = DateTime.UtcNow,
                IpAddress    = HttpContext.Connection.RemoteIpAddress.ToString(),
                Post         = post,
                IsApproved   = isLogged || !settings.ModerateComments,
            };

            if (isLogged)
            {
                postComment.Author     = db.Users.SingleOrDefault(x => x.UserName == User.Identity.Name);
                postComment.IsApproved = true;

                await TryUpdateModelAsync(postComment, "", x => x.Comment);
            }
            else
            {
                SocialUserData socialUserData = null;//TODO SocialController.GetLoggedUser();
                if (socialUserData != null)
                {
                    postComment.AuthorName    = socialUserData.Name;
                    postComment.AuthorEmail   = socialUserData.Email;
                    postComment.AuthorWebsite = socialUserData.WebSite;
                    await TryUpdateModelAsync(postComment, "", x => x.Comment);
                }
                else
                {
                    await TryUpdateModelAsync(postComment, "", x => x.AuthorName, x => x.AuthorEmail, x => x.AuthorWebsite, x => x.Comment);
                }
            }

            if (parentCommentId.HasValue)
            {
                postComment.ParentComment = db.PostComments.Single(x => x.Id == parentCommentId.Value);
            }

            db.PostComments.Add(postComment);
            db.SaveChanges();

            if (!isLogged && settings.NotifyNewComments)
            {
                string  subject   = string.Format("Comment on \"{0}\" from {1}", post.Title, settings.BlogName);
                dynamic viewModel = new ExpandoObject();
                viewModel.BlogName = settings.BlogName;
                viewModel.Post     = post;
                viewModel.Comment  = postComment;
                string body = RenderPartialViewToString("NewComment", viewModel);

                await emailSender.SendEmailAsync(settings.EmailTo, subject, body);
            }

            return(View("Comments", new[] { postComment }));
        }
示例#6
0
        public async Task <ActionResult> Edit(AdminUserSave userData)
        {
            var user = db.Users.Include(x => x.Roles).Single(x => x.Id == userData.Id);

            if (!(user.UserName != User.Identity.Name && HttpContext.UserHasClaimPermission(PermissionClaims.EditOtherUsers)) &&
                !(user.UserName == User.Identity.Name && HttpContext.UserHasClaimPermission(PermissionClaims.EditOwnUser)))
            {
                return(Forbid());
            }

            if (CheckEditRolesPermission(userData, user) == false)
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                user.UserName    = userData.Username;
                user.Email       = userData.Email;
                user.DisplayName = userData.DisplayName;
                user.FullName    = userData.FullName;
                user.BirthDate   = userData.BirthDate;
                user.Location    = userData.Location;
                user.About       = userData.About;
                user.Enabled     = true;

                var rolesRemoved = new List <string>();
                foreach (var removedRole in user.Roles.Where(x => userData.SelectedRoles.Contains(x.RoleId) == false))
                {
                    var role = await roleManager.FindByIdAsync(removedRole.RoleId);

                    rolesRemoved.Add(role.Name);
                }

                var result = await userManager.RemoveFromRolesAsync(user, rolesRemoved);

                if (result.Succeeded)
                {
                    var rolesAdded = new List <string>();
                    foreach (var addedRole in userData.SelectedRoles.Where(x => user.Roles.Any(y => y.RoleId == x) == false))
                    {
                        var role = await roleManager.FindByIdAsync(addedRole);

                        rolesAdded.Add(role.Name);
                    }

                    result = await userManager.AddToRolesAsync(user, rolesAdded);

                    if (result.Succeeded)
                    {
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }

                    AddErrors(result);
                }
            }

            userData.AllRoles = db.Roles.Where(x => x.Name != AnonymousRole).ToList();

            return(View(userData));
        }