public async Task <IActionResult> Delete([FromRoute] int id)
        {
            var result = await _pollAnswerService.Delete(id);

            if (!result.Succeeded)
            {
                return(BadRequest());
            }

            return(NoContent());
        }
예제 #2
0
        /// <summary>
        /// Delete a topic
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="unitOfWork"></param>
        public void Delete(Topic topic, IUnitOfWork unitOfWork)
        {
            // First thing - Set the last post as null and clear tags
            topic.LastPost = null;
            topic.Tags.Clear();

            // Save here to clear the last post
            unitOfWork.SaveChanges();

            // TODO - Need to refactor as some of the code below is duplicated in the post delete

            // Loop through all the posts and clear the associated entities
            // then delete the posts
            if (topic.Posts != null)
            {
                var postsToDelete = new List <Post>();
                postsToDelete.AddRange(topic.Posts);
                foreach (var post in postsToDelete)
                {
                    // Posts should only be deleted from this method as it clears
                    // associated data
                    _postService.Delete(post, unitOfWork, true);
                }

                // Final clear
                topic.Posts.Clear();
            }

            unitOfWork.SaveChanges();

            // Remove all notifications on this topic too
            if (topic.TopicNotifications != null)
            {
                var notificationsToDelete = new List <TopicNotification>();
                notificationsToDelete.AddRange(topic.TopicNotifications);
                foreach (var topicNotification in notificationsToDelete)
                {
                    _topicNotificationService.Delete(topicNotification);
                }

                // Final Clear
                topic.TopicNotifications.Clear();
            }

            // Remove all favourites on this topic too
            if (topic.Favourites != null)
            {
                var toDelete = new List <Favourite>();
                toDelete.AddRange(topic.Favourites);
                foreach (var entity in toDelete)
                {
                    _favouriteService.Delete(entity);
                }

                // Final Clear
                topic.Favourites.Clear();
            }

            // Poll
            if (topic.Poll != null)
            {
                //Delete the poll answers
                var pollAnswers = topic.Poll.PollAnswers;
                if (pollAnswers.Any())
                {
                    var pollAnswersList = new List <PollAnswer>();
                    pollAnswersList.AddRange(pollAnswers);
                    foreach (var answer in pollAnswersList)
                    {
                        answer.Poll = null;
                        _pollAnswerService.Delete(answer);
                    }
                }

                topic.Poll.PollAnswers.Clear();
                topic.Poll.User = null;
                _pollService.Delete(topic.Poll);

                // Final Clear
                topic.Poll = null;
            }

            // Finally delete the topic
            _context.Topic.Remove(topic);
        }
예제 #3
0
        public void ScrubUsers(MembershipUser user, IUnitOfWork unitOfWork)
        {
            // TODO - This REALLY needs to be refactored

            // PROFILE
            user.Website   = string.Empty;
            user.Twitter   = string.Empty;
            user.Facebook  = string.Empty;
            user.Avatar    = string.Empty;
            user.Signature = string.Empty;

            // User Votes
            if (user.Votes != null)
            {
                var votesToDelete = new List <Vote>();
                votesToDelete.AddRange(user.Votes);
                foreach (var d in votesToDelete)
                {
                    _voteService.Delete(d);
                }
                user.Votes.Clear();
            }

            // User Badges
            if (user.Badges != null)
            {
                var toDelete = new List <Badge>();
                toDelete.AddRange(user.Badges);
                foreach (var obj in toDelete)
                {
                    _badgeService.Delete(obj);
                }
                user.Badges.Clear();
            }

            // User badge time checks
            if (user.BadgeTypesTimeLastChecked != null)
            {
                var toDelete = new List <BadgeTypeTimeLastChecked>();
                toDelete.AddRange(user.BadgeTypesTimeLastChecked);
                foreach (var obj in toDelete)
                {
                    _badgeService.DeleteTimeLastChecked(obj);
                }
                user.BadgeTypesTimeLastChecked.Clear();
            }

            // User category notifications
            if (user.CategoryNotifications != null)
            {
                var toDelete = new List <CategoryNotification>();
                toDelete.AddRange(user.CategoryNotifications);
                foreach (var obj in toDelete)
                {
                    _categoryNotificationService.Delete(obj);
                }
                user.CategoryNotifications.Clear();
            }

            // User PM Received
            var pmUpdate = false;

            if (user.PrivateMessagesReceived != null)
            {
                pmUpdate = true;
                var toDelete = new List <PrivateMessage>();
                toDelete.AddRange(user.PrivateMessagesReceived);
                foreach (var obj in toDelete)
                {
                    _privateMessageService.DeleteMessage(obj);
                }
                user.PrivateMessagesReceived.Clear();
            }

            // User PM Sent
            if (user.PrivateMessagesSent != null)
            {
                pmUpdate = true;
                var toDelete = new List <PrivateMessage>();
                toDelete.AddRange(user.PrivateMessagesSent);
                foreach (var obj in toDelete)
                {
                    _privateMessageService.DeleteMessage(obj);
                }
                user.PrivateMessagesSent.Clear();
            }

            if (pmUpdate)
            {
                unitOfWork.SaveChanges();
            }

            // User Favourites
            if (user.Favourites != null)
            {
                var toDelete = new List <Favourite>();
                toDelete.AddRange(user.Favourites);
                foreach (var obj in toDelete)
                {
                    _favouriteService.Delete(obj);
                }
                user.Favourites.Clear();
            }

            if (user.TopicNotifications != null)
            {
                var notificationsToDelete = new List <TopicNotification>();
                notificationsToDelete.AddRange(user.TopicNotifications);
                foreach (var topicNotification in notificationsToDelete)
                {
                    _topicNotificationService.Delete(topicNotification);
                }
                user.TopicNotifications.Clear();
            }

            // Also clear their points
            var userPoints = user.Points;

            if (userPoints.Any())
            {
                var pointsList = new List <MembershipUserPoints>();
                pointsList.AddRange(userPoints);
                foreach (var point in pointsList)
                {
                    point.User = null;
                    _membershipUserPointsService.Delete(point);
                }
                user.Points.Clear();
            }

            // Now clear all activities for this user
            var usersActivities = _activityService.GetDataFieldByGuid(user.Id);

            _activityService.Delete(usersActivities.ToList());

            // Also clear their poll votes
            var userPollVotes = user.PollVotes;

            if (userPollVotes.Any())
            {
                var pollList = new List <PollVote>();
                pollList.AddRange(userPollVotes);
                foreach (var vote in pollList)
                {
                    vote.User = null;
                    _pollVoteService.Delete(vote);
                }
                user.PollVotes.Clear();
            }

            unitOfWork.SaveChanges();


            // Also clear their polls
            var userPolls = user.Polls;

            if (userPolls.Any())
            {
                var polls = new List <Poll>();
                polls.AddRange(userPolls);
                foreach (var poll in polls)
                {
                    //Delete the poll answers
                    var pollAnswers = poll.PollAnswers;
                    if (pollAnswers.Any())
                    {
                        var pollAnswersList = new List <PollAnswer>();
                        pollAnswersList.AddRange(pollAnswers);
                        foreach (var answer in pollAnswersList)
                        {
                            answer.Poll = null;
                            _pollAnswerService.Delete(answer);
                        }
                    }

                    poll.PollAnswers.Clear();
                    poll.User = null;
                    _pollService.Delete(poll);
                }
                user.Polls.Clear();
            }

            unitOfWork.SaveChanges();

            // ######### POSTS TOPICS ########

            // Delete all topics first
            var topics = user.Topics;

            if (topics != null && topics.Any())
            {
                var topicList = new List <Topic>();
                topicList.AddRange(topics);
                foreach (var topic in topicList)
                {
                    topic.LastPost = null;
                    topic.Posts.Clear();
                    topic.Tags.Clear();
                    _topicService.Delete(topic, unitOfWork);
                }
                user.Topics.Clear();
                unitOfWork.SaveChanges();
            }

            // Now sorts Last Posts on topics and delete all the users posts
            var posts = user.Posts;

            if (posts != null && posts.Any())
            {
                var postIds = posts.Select(x => x.Id).ToList();

                // Get all categories
                var allCategories = _categoryService.GetAll();

                // Need to see if any of these are last posts on Topics
                // If so, need to swap out last post
                var lastPostTopics = _topicService.GetTopicsByLastPost(postIds, allCategories.ToList());
                foreach (var topic in lastPostTopics.Where(x => x.User.Id != user.Id))
                {
                    var lastPost = topic.Posts.Where(x => !postIds.Contains(x.Id)).OrderByDescending(x => x.DateCreated).FirstOrDefault();
                    topic.LastPost = lastPost;
                }

                unitOfWork.SaveChanges();

                user.UploadedFiles.Clear();

                // Delete all posts

                var postList = new List <Post>();
                postList.AddRange(posts);
                foreach (var post in postList)
                {
                    if (post.Files != null)
                    {
                        var files     = post.Files;
                        var filesList = new List <UploadedFile>();
                        filesList.AddRange(files);
                        foreach (var file in filesList)
                        {
                            // store the file path as we'll need it to delete on the file system
                            var filePath = file.FilePath;

                            // Now delete it
                            _uploadedFileService.Delete(file);

                            // And finally delete from the file system
                            System.IO.File.Delete(HostingEnvironment.MapPath(filePath));
                        }
                        post.Files.Clear();
                    }
                    var postEdits = new List <PostEdit>();
                    postEdits.AddRange(post.PostEdits);
                    _postEditService.Delete(postEdits);
                    post.PostEdits.Clear();
                    _postService.Delete(post, unitOfWork, true);
                }
                user.Posts.Clear();

                unitOfWork.SaveChanges();
            }
        }
예제 #4
0
        private void DeleteUsersPostsPollsVotesAndPoints(MembershipUser user, IUnitOfWork unitOfWork)
        {
            // Delete all file uploads
            var files     = _uploadedFileService.GetAllByUser(user.Id);
            var filesList = new List <UploadedFile>();

            filesList.AddRange(files);
            foreach (var file in filesList)
            {
                // store the file path as we'll need it to delete on the file system
                var filePath = file.FilePath;

                // Now delete it
                _uploadedFileService.Delete(file);

                // And finally delete from the file system
                System.IO.File.Delete(Server.MapPath(filePath));
            }

            // Delete all posts
            var posts    = user.Posts;
            var postList = new List <Post>();

            postList.AddRange(posts);
            foreach (var post in postList)
            {
                post.Files.Clear();
                _postService.Delete(post);
            }

            unitOfWork.SaveChanges();

            // Also clear their poll votes
            var userPollVotes = user.PollVotes;

            if (userPollVotes.Any())
            {
                var pollList = new List <PollVote>();
                pollList.AddRange(userPollVotes);
                foreach (var vote in pollList)
                {
                    vote.User = null;
                    _pollVoteService.Delete(vote);
                }
                user.PollVotes.Clear();
            }

            unitOfWork.SaveChanges();


            // Also clear their polls
            var userPolls = user.Polls;

            if (userPolls.Any())
            {
                var polls = new List <Poll>();
                polls.AddRange(userPolls);
                foreach (var poll in polls)
                {
                    //Delete the poll answers
                    var pollAnswers = poll.PollAnswers;
                    if (pollAnswers.Any())
                    {
                        var pollAnswersList = new List <PollAnswer>();
                        pollAnswersList.AddRange(pollAnswers);
                        foreach (var answer in pollAnswersList)
                        {
                            answer.Poll = null;
                            _pollAnswerService.Delete(answer);
                        }
                    }

                    poll.PollAnswers.Clear();
                    poll.User = null;
                    _pollService.Delete(poll);
                }
                user.Polls.Clear();
            }

            unitOfWork.SaveChanges();

            // Delete all topics
            var topics    = user.Topics;
            var topicList = new List <Topic>();

            topicList.AddRange(topics);
            foreach (var topic in topicList)
            {
                _topicService.Delete(topic);
            }

            // Also clear their points
            var userPoints = user.Points;

            if (userPoints.Any())
            {
                var pointsList = new List <MembershipUserPoints>();
                pointsList.AddRange(userPoints);
                foreach (var point in pointsList)
                {
                    point.User = null;
                    _membershipUserPointsService.Delete(point);
                }
                user.Points.Clear();
            }

            unitOfWork.SaveChanges();

            // Now clear all activities for this user
            var usersActivities = _activityService.GetDataFieldByGuid(user.Id);

            _activityService.Delete(usersActivities.ToList());
        }
예제 #5
0
        public ActionResult EditPost(EditPostViewModel editPostViewModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                // Got to get a lot of things here as we have to check permissions
                // Get the post
                var post = _postService.Get(editPostViewModel.Id);

                // Get the topic
                var topic = post.Topic;

                // get the users permissions
                var permissions = RoleService.GetPermissions(topic.Category, UsersRole);

                if (post.User.Id == LoggedOnUser.Id || permissions[AppConstants.PermissionEditPosts].IsTicked)
                {
                    // User has permission so update the post
                    post.PostContent = StringUtils.GetSafeHtml(_bannedWordService.SanitiseBannedWords(editPostViewModel.Content));
                    post.DateEdited  = DateTime.UtcNow;

                    // if topic starter update the topic
                    if (post.IsTopicStarter)
                    {
                        // if category has changed then update it
                        if (topic.Category.Id != editPostViewModel.Category)
                        {
                            var cat = _categoryService.Get(editPostViewModel.Category);
                            topic.Category = cat;
                        }

                        topic.IsLocked = editPostViewModel.IsLocked;
                        topic.IsSticky = editPostViewModel.IsSticky;
                        topic.Name     = StringUtils.GetSafeHtml(_bannedWordService.SanitiseBannedWords(editPostViewModel.Name));

                        // See if there is a poll
                        if (editPostViewModel.PollAnswers != null && editPostViewModel.PollAnswers.Count > 0)
                        {
                            // Now sort the poll answers, what to add and what to remove
                            // Poll answers already in this poll.
                            var postedIds = editPostViewModel.PollAnswers.Select(x => x.Id);
                            //var existingAnswers = topic.Poll.PollAnswers.Where(x => postedIds.Contains(x.Id)).ToList();
                            var existingAnswers     = editPostViewModel.PollAnswers.Where(x => topic.Poll.PollAnswers.Select(p => p.Id).Contains(x.Id)).ToList();
                            var newPollAnswers      = editPostViewModel.PollAnswers.Where(x => !topic.Poll.PollAnswers.Select(p => p.Id).Contains(x.Id)).ToList();
                            var pollAnswersToRemove = topic.Poll.PollAnswers.Where(x => !postedIds.Contains(x.Id)).ToList();

                            // Loop through existing and update names if need be
                            //TODO: Need to think about this in future versions if they change the name
                            //TODO: As they could game the system by getting votes and changing name?
                            foreach (var existPollAnswer in existingAnswers)
                            {
                                // Get the existing answer from the current topic
                                var pa = topic.Poll.PollAnswers.FirstOrDefault(x => x.Id == existPollAnswer.Id);
                                if (pa != null && pa.Answer != existPollAnswer.Answer)
                                {
                                    // If the answer has changed then update it
                                    pa.Answer = existPollAnswer.Answer;
                                }
                            }

                            // Loop through and remove the old poll answers and delete
                            foreach (var oldPollAnswer in pollAnswersToRemove)
                            {
                                // Delete
                                _pollAnswerService.Delete(oldPollAnswer);

                                // Remove from Poll
                                topic.Poll.PollAnswers.Remove(oldPollAnswer);
                            }

                            // Poll answers to add
                            foreach (var newPollAnswer in newPollAnswers)
                            {
                                var npa = new PollAnswer
                                {
                                    Poll   = topic.Poll,
                                    Answer = newPollAnswer.Answer
                                };
                                _pollAnswerService.Add(npa);
                                topic.Poll.PollAnswers.Add(npa);
                            }
                        }
                        else
                        {
                            // Need to check if this topic has a poll, because if it does
                            // All the answers have now been removed so remove the poll.
                            if (topic.Poll != null)
                            {
                                //Firstly remove the answers if there are any
                                if (topic.Poll.PollAnswers != null && topic.Poll.PollAnswers.Any())
                                {
                                    var answersToDelete = new List <PollAnswer>();
                                    answersToDelete.AddRange(topic.Poll.PollAnswers);
                                    foreach (var answer in answersToDelete)
                                    {
                                        // Delete
                                        _pollAnswerService.Delete(answer);

                                        // Remove from Poll
                                        topic.Poll.PollAnswers.Remove(answer);
                                    }
                                }

                                // Now delete the poll
                                var pollToDelete = topic.Poll;
                                _pollService.Delete(pollToDelete);

                                // Remove from topic.
                                topic.Poll = null;
                            }
                        }

                        // Tags
                        topic.Tags.Clear();
                        if (!string.IsNullOrEmpty(editPostViewModel.Tags))
                        {
                            _topicTagService.Add(editPostViewModel.Tags.ToLower(), topic);
                        }
                    }

                    // redirect back to topic
                    TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                    {
                        Message     = LocalizationService.GetResourceString("Post.Updated"),
                        MessageType = GenericMessages.success
                    };
                    try
                    {
                        unitOfWork.Commit();

                        // Successful, add this post to the Lucene index
                        if (_luceneService.CheckIndexExists())
                        {
                            _luceneService.AddUpdate(_luceneService.MapToModel(post));
                        }

                        return(Redirect(topic.NiceUrl));
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                        throw new Exception(LocalizationService.GetResourceString("Errors.GenericError"));
                    }
                }

                return(NoPermission(topic));
            }
        }
예제 #6
0
        public ActionResult EditPostTopic(CreateEditTopicViewModel editPostViewModel)
        {
            // Get the category
            var category = _categoryService.Get(editPostViewModel.Category);

            // First check this user is allowed to create topics in this category
            var permissions = RoleService.GetPermissions(category, UsersRole);

            // Now we have the category and permissionSet - Populate the optional permissions
            // This is just in case the viewModel is return back to the view also sort the allowedCategories
            var allowedCategories = _categoryService.GetAllowedCategories(UsersRole);

            editPostViewModel.OptionalPermissions = GetCheckCreateTopicPermissions(permissions);
            editPostViewModel.Categories          = GetBaseSelectListCategories(allowedCategories);
            editPostViewModel.IsTopicStarter      = editPostViewModel.Id == Guid.Empty;
            if (editPostViewModel.PollAnswers == null)
            {
                editPostViewModel.PollAnswers = new List <PollAnswer>();
            }
            /*---- End Re-populate ViewModel ----*/

            if (ModelState.IsValid)
            {
                // Quick check to see if user is locked out, when logged in
                if (LoggedOnUser.IsLockedOut || LoggedOnUser.DisablePosting == true || !LoggedOnUser.IsApproved)
                {
                    FormsAuthentication.SignOut();
                    return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoAccess")));
                }

                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    // Got to get a lot of things here as we have to check permissions
                    // Get the post
                    var post = _postService.Get(editPostViewModel.Id);

                    // Get the topic
                    var topic = post.Topic;

                    if (post.User.Id == LoggedOnUser.Id || permissions[AppConstants.PermissionEditPosts].IsTicked)
                    {
                        // User has permission so update the post
                        post.PostContent = StringUtils.GetSafeHtml(_bannedWordService.SanitiseBannedWords(editPostViewModel.Content));
                        post.DateEdited  = DateTime.UtcNow;

                        // if topic starter update the topic
                        if (post.IsTopicStarter)
                        {
                            // if category has changed then update it
                            if (topic.Category.Id != editPostViewModel.Category)
                            {
                                var cat = _categoryService.Get(editPostViewModel.Category);
                                topic.Category = cat;
                            }

                            topic.IsLocked = editPostViewModel.IsLocked;
                            topic.IsSticky = editPostViewModel.IsSticky;
                            topic.Name     = StringUtils.GetSafeHtml(_bannedWordService.SanitiseBannedWords(editPostViewModel.Name));

                            // See if there is a poll
                            if (editPostViewModel.PollAnswers != null && editPostViewModel.PollAnswers.Count > 0)
                            {
                                // Now sort the poll answers, what to add and what to remove
                                // Poll answers already in this poll.
                                //var existingAnswers = topic.Poll.PollAnswers.Where(x => postedIds.Contains(x.Id)).ToList();
                                var postedIds           = editPostViewModel.PollAnswers.Select(x => x.Id);
                                var topicPollAnswerIds  = topic.Poll.PollAnswers.Select(p => p.Id).ToList();
                                var existingAnswers     = editPostViewModel.PollAnswers.Where(x => topicPollAnswerIds.Contains(x.Id)).ToList();
                                var newPollAnswers      = editPostViewModel.PollAnswers.Where(x => !topicPollAnswerIds.Contains(x.Id)).ToList();
                                var pollAnswersToRemove = topic.Poll.PollAnswers.Where(x => !postedIds.Contains(x.Id)).ToList();

                                // Loop through existing and update names if need be
                                //TODO: Need to think about this in future versions if they change the name
                                //TODO: As they could game the system by getting votes and changing name?
                                foreach (var existPollAnswer in existingAnswers)
                                {
                                    // Get the existing answer from the current topic
                                    var pa = topic.Poll.PollAnswers.FirstOrDefault(x => x.Id == existPollAnswer.Id);
                                    if (pa != null && pa.Answer != existPollAnswer.Answer)
                                    {
                                        // If the answer has changed then update it
                                        pa.Answer = existPollAnswer.Answer;
                                    }
                                }

                                // Loop through and remove the old poll answers and delete
                                foreach (var oldPollAnswer in pollAnswersToRemove)
                                {
                                    // Delete
                                    _pollAnswerService.Delete(oldPollAnswer);

                                    // Remove from Poll
                                    topic.Poll.PollAnswers.Remove(oldPollAnswer);
                                }

                                // Poll answers to add
                                foreach (var newPollAnswer in newPollAnswers)
                                {
                                    var npa = new PollAnswer
                                    {
                                        Poll   = topic.Poll,
                                        Answer = newPollAnswer.Answer
                                    };
                                    _pollAnswerService.Add(npa);
                                    topic.Poll.PollAnswers.Add(npa);
                                }
                            }
                            else
                            {
                                // Need to check if this topic has a poll, because if it does
                                // All the answers have now been removed so remove the poll.
                                if (topic.Poll != null)
                                {
                                    //Firstly remove the answers if there are any
                                    if (topic.Poll.PollAnswers != null && topic.Poll.PollAnswers.Any())
                                    {
                                        var answersToDelete = new List <PollAnswer>();
                                        answersToDelete.AddRange(topic.Poll.PollAnswers);
                                        foreach (var answer in answersToDelete)
                                        {
                                            // Delete
                                            _pollAnswerService.Delete(answer);

                                            // Remove from Poll
                                            topic.Poll.PollAnswers.Remove(answer);
                                        }
                                    }

                                    // Now delete the poll
                                    var pollToDelete = topic.Poll;
                                    _pollService.Delete(pollToDelete);

                                    // Remove from topic.
                                    topic.Poll = null;
                                }
                            }

                            // Tags
                            topic.Tags.Clear();
                            if (!string.IsNullOrEmpty(editPostViewModel.Tags))
                            {
                                _topicTagService.Add(editPostViewModel.Tags.ToLower(), topic);
                            }
                        }

                        // redirect back to topic
                        TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                        {
                            Message     = LocalizationService.GetResourceString("Post.Updated"),
                            MessageType = GenericMessages.success
                        };
                        try
                        {
                            unitOfWork.Commit();
                            return(Redirect(string.Format("{0}?postbadges=true", topic.NiceUrl)));
                        }
                        catch (Exception ex)
                        {
                            unitOfWork.Rollback();
                            LoggingService.Error(ex);
                            TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                            {
                                Message     = LocalizationService.GetResourceString("Errors.GenericError"),
                                MessageType = GenericMessages.danger
                            };
                        }
                    }

                    return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission")));
                }
            }
            return(View(editPostViewModel));
        }