public ForumAccessService()
 {
     forumAccess       = new ForumAccess();
     forumAccessList   = new List <ForumAccess>();
     groupService      = new GroupService();
     accessMaskService = new AccessMaskService();
 }
 public void UpdateForumAccess(int forumID, int groupID, int accessMaskID)
 {
     this.forumAccess              = this.GetForumAccess(forumID, groupID);
     this.forumAccess.ForumId      = forumID;
     this.forumAccess.GroupId      = groupID;
     this.forumAccess.AccessMaskId = accessMaskID;
     context.SaveChanges();
 }
Exemple #3
0
        public ActionResult EditPost(long?id, FormCollection values)
        {
            if (!id.HasValue)
            {
                return(View("NotAvailable"));
            }

            if (CurrentUser == null)
            {
                return(View("NotAuthorized"));
            }

            Post p = this.Forums.GetPost(id.Value);

            if (p == null || p.IsDeleted)
            {
                return(View("NotFound"));
            }

            Thread t         = p.Thread;
            Forum  f         = t.Forum;
            bool   firstPost = t.Posts.Where(post => post.IsDeleted == false).OrderBy(post => post.CreateDate).First().PostID == id;

            ForumAccess access = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!access.CanPost || (!access.CanModerate && (t.IsLocked || p.UserID != CurrentUser.UserID)))
            {
                return(View("NotAuthorized"));
            }

            // TODO: Validate the post

            p.Title        = values["Title"];
            p.Text         = values["Text"];
            p.ModifyDate   = DateTime.UtcNow;
            p.ModifyUserID = CurrentUser.UserID;

            if (firstPost)
            {
                t.Title = p.Title;
            }

            if (access.CanModerate)
            {
                long?level = Converter.Convert <long?>(values["Level"]);

                t.IsLocked = values["Locked"].StartsWith("true");

                if (level.HasValue)
                {
                    t.Level = level.Value;
                }
            }

            this.Db.SubmitChanges();

            return(RedirectToAction("ViewThread", new { id = t.ThreadID }));
        }
Exemple #4
0
        public ActionResult DeletePost(long?id, FormCollection values)
        {
            if (!id.HasValue)
            {
                return(View("NotAvailable"));
            }

            if (CurrentUser == null)
            {
                return(View("NotAuthorized"));
            }

            Post p = this.Forums.GetPost(id.Value);

            if (p == null || p.IsDeleted)
            {
                return(View("NotFound"));
            }

            Thread t = p.Thread;

            long firstPostId = t.Posts.Where(post => post.IsDeleted == false).OrderBy(post => post.CreateDate).FirstOrDefault().PostID;
            long lastPostId  = t.Posts.Where(post => post.IsDeleted == false).OrderByDescending(post => post.CreateDate).FirstOrDefault().PostID;

            Forum f = t.Forum;

            ForumAccess access = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!access.CanPost || (!access.CanModerate && p.PostID != lastPostId))
            {
                return(View("NotAuthorized"));
            }

            bool canDeleteThread = access.CanModerate || p.PostID == firstPostId;
            bool deleteThread    = canDeleteThread && p.PostID == firstPostId;

            if (access.CanModerate)
            {
                deleteThread = values["DeleteThread"].StartsWith("true");
            }

            if (deleteThread)
            {
                t.IsDeleted = true;
                this.Db.SubmitChanges();

                return(RedirectToAction("ViewForum", new { id = f.ForumID }));
            }
            else
            {
                p.IsDeleted = true;
                this.Db.SubmitChanges();

                return(RedirectToAction("ViewThread", new { id = t.ThreadID }));
            }
        }
Exemple #5
0
        public ActionResult ViewForum(long?id, int?page)
        {
            int pageSize = 25;

            if (!id.HasValue)
            {
                return(View("NotAvailable"));
            }

            Forum f = this.Forums.GetForum(id.Value);

            if (f == null)
            {
                return(View("NotFound"));
            }

            ForumAccess a = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!a.CanView)
            {
                return(View("NotAuthorized"));
            }

            var threadSource = this.Forums.GetForumViewableThreads(f.ForumID);

            var threadCount = threadSource.Count();
            var threads     = GetThreadInformation(threadSource, CurrentUser);
            int pages       = Pager.PageCount(threadCount, pageSize);

            page = Pager.ClampPage(page, pages);

            var threadsList = new ThreadList
            {
                Threads = threads.OrderByDescending(t => t.LastPostDate).OrderByDescending(t => t.Thread.Level).Skip((page.Value - 1) * pageSize).Take(pageSize).ToList()
            };

            var fd = new ForumDetailsModel
            {
                Forum      = f,
                UserAccess = a,
                PageInfo   = new PaginationInformation
                {
                    Pager          = this.Skins.GetDefaultForumPager(),
                    ControllerName = "Forums",
                    ActionName     = "ViewForum",
                    PageAttribute  = "page",
                    RouteValues    = new System.Web.Routing.RouteValueDictionary(new { id = id }),
                    ItemsPerPage   = pageSize,
                    Items          = threadCount,
                    CurrentPage    = page
                },
                Threads = threadsList
            };

            return(View("ViewForum", fd));
        }
        public bool AddForumAccess(int forumID, int groupID, int accessMaskID)
        {
            ForumAccess tempForumAccess = this.GetForumAccess(forumID, groupID);

            if (tempForumAccess == null)
            {
                forumAccess.ForumId      = forumID;
                forumAccess.GroupId      = groupID;
                forumAccess.AccessMaskId = accessMaskID;
                context.ForumAccess.Add(forumAccess);
                context.SaveChanges();
                return(true);
            }
            return(false);
        }
Exemple #7
0
        public ActionResult DeletePost(long?id)
        {
            if (!id.HasValue)
            {
                return(View("NotAvailable"));
            }

            if (CurrentUser == null)
            {
                return(View("NotAuthorized"));
            }

            Post p = this.Forums.GetPost(id.Value);

            if (p == null || p.IsDeleted)
            {
                return(View("NotFound"));
            }

            Thread t = p.Thread;

            long firstPostId = t.Posts.Where(post => post.IsDeleted == false).OrderBy(post => post.CreateDate).FirstOrDefault().PostID;
            long lastPostId  = t.Posts.Where(post => post.IsDeleted == false).OrderByDescending(post => post.CreateDate).FirstOrDefault().PostID;

            Forum f = t.Forum;

            ForumAccess access = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!access.CanPost || (!access.CanModerate && p.PostID != lastPostId))
            {
                return(View("NotAuthorized"));
            }

            var del = new DeletePostModel
            {
                Post       = p,
                UserAccess = access,
                FirstPost  = (p.PostID == firstPostId),
            };

            return(View("DeletePost", del));
        }
Exemple #8
0
        public ActionResult EditPost(long?id)
        {
            if (!id.HasValue)
            {
                return(View("NotAvailable"));
            }

            if (CurrentUser == null)
            {
                return(View("NotAuthorized"));
            }

            Post p = this.Forums.GetPost(id.Value);

            if (p == null || p.IsDeleted)
            {
                return(View("NotFound"));
            }

            Thread t = p.Thread;
            Forum  f = t.Forum;

            ForumAccess access = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!access.CanPost || (!access.CanModerate && (t.IsLocked || p.UserID != CurrentUser.UserID)))
            {
                return(View("NotAuthorized"));
            }

            var epm = new EditPostModel
            {
                Forum  = f,
                Thread = t,
                Post   = p,
                AvailableThreadLevels = this.Db.ThreadLevels.ToList(),
                UserAccess            = access
            };

            return(View("EditPost", epm));
        }
Exemple #9
0
        public ActionResult CreatePost(long?forum, long?thread, long?replyTo)
        {
            if (CurrentUser == null)
            {
                return(View("NotAuthorized"));
            }

            Forum  f = null;
            Thread t = null;
            Post   p = null;

            if (replyTo.HasValue)
            {
                p = this.Forums.GetPost(replyTo.Value);

                if (p == null || p.IsDeleted)
                {
                    return(View("NotFound"));
                }

                t = p.Thread;
                f = t.Forum;
            }

            if (thread.HasValue)
            {
                if (f != null)
                {
                    return(View("Error", new ErrorInfoModel
                    {
                        ErrorMessage = "You may not reply to a thread and another post at the same time."
                    }));
                }

                t = this.Forums.GetThread(thread.Value);

                if (t == null || t.IsDeleted)
                {
                    return(View("NotFound"));
                }

                f = t.Forum;
            }

            if (forum.HasValue)
            {
                if (f != null)
                {
                    return(View("Error", new ErrorInfoModel
                    {
                        ErrorMessage = "You may not create a new thread in reply to another item."
                    }));
                }

                f = this.Forums.GetForum(forum.Value);

                if (f == null)
                {
                    return(View("NotFound"));
                }
            }

            ForumAccess access = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!access.CanPost || (!access.CanModerate && t != null && t.IsLocked))
            {
                return(View("NotAuthorized"));
            }

            string title = "";
            string text  = "";

            if (p != null)
            {
                title = "Re: " + p.Title;
                text  = "[quote=" + p.User.Username + "]" + p.Text + "[/quote]";
            }

            var cpm = new CreatePostModel
            {
                Title  = title,
                Text   = text,
                Forum  = f,
                Thread = t,
                Post   = p,
                AvailableThreadLevels = this.Db.ThreadLevels.ToList(),
                UserAccess            = access
            };

            return(View("CreatePost", cpm));
        }
Exemple #10
0
        public ActionResult Post(FormCollection values)
        {
            if (CurrentUser == null)
            {
                return(View("NotAuthorized"));
            }

            long?replyTo = Converter.Convert <long?>(values["ReplyTo"]);
            long?thread  = Converter.Convert <long?>(values["Thread"]);
            long?forum   = Converter.Convert <long?>(values["Forum"]);

            Forum  f = null;
            Thread t = null;
            Post   p = null;

            if (replyTo.HasValue)
            {
                p = this.Forums.GetPost(replyTo.Value);

                if (p == null || p.IsDeleted)
                {
                    return(View("NotFound"));
                }

                t = p.Thread;
                f = t.Forum;
            }

            if (thread.HasValue)
            {
                if (f != null)
                {
                    return(View("Error"));
                }

                t = this.Forums.GetThread(thread.Value);

                if (t == null || t.IsDeleted)
                {
                    return(View("NotFound"));
                }

                f = t.Forum;
            }

            if (forum.HasValue)
            {
                if (f != null)
                {
                    return(View("Error"));
                }

                f = this.Forums.GetForum(forum.Value);

                if (f == null)
                {
                    return(View("NotFound"));
                }
            }

            ForumAccess access = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!access.CanPost || (!access.CanModerate && t != null && t.IsLocked))
            {
                return(View("NotAuthorized"));
            }

            string title = Converter.Convert <string>(values["Title"]);
            string text  = Converter.Convert <string>(values["Text"]);

            DateTime now = DateTime.UtcNow;

            if (t == null)
            {
                t = new Thread
                {
                    CreateDate = now,
                    Title      = title,
                    Views      = 0,
                    Level      = 0,
                    IsLocked   = false,
                    Forum      = f,
                };

                t.Forum = f;
                this.Db.Threads.InsertOnSubmit(t);
            }

            if (access.CanModerate)
            {
                long?level = Converter.Convert <long?>(values["Level"]);

                t.IsLocked = values["Locked"].StartsWith("true");

                if (level.HasValue)
                {
                    t.Level = level.Value;
                }
            }

            User cu = CurrentUser;

            Post newPost = new Post
            {
                CreateDate       = now,
                ModifyDate       = now,
                UserID           = cu.UserID,
                ModifyUserID     = cu.UserID,
                Text             = text,
                Title            = title,
                Thread           = t,
                ResponseToPostID = null
            };

            // TODO: Validate the post

            this.Db.Posts.InsertOnSubmit(newPost);
            this.Db.SubmitChanges();

            int pageSize = 25;

            return(Redirect(
                       Url.Action("ViewThread", new
            {
                id = t.ThreadID,
                page = Pager.PageCount(t.Posts.Where(post => post.IsDeleted == false).Count(), pageSize)
            }) + "#" + newPost.PostID));
        }
Exemple #11
0
        public ActionResult ViewThread(long?id, int?page)
        {
            int pageSize = 25;

            if (!id.HasValue)
            {
                return(View("NotAvailable"));
            }

            var t = this.Forums.GetThread(id.Value);

            if (t == null || t.IsDeleted)
            {
                return(View("NotFound"));
            }

            Forum       f = t.Forum;
            ForumAccess a = this.Security.GetUserForumAccess(CurrentUser, f);

            if (!a.CanRead)
            {
                return(View("NotAuthorized"));
            }

            lock (syncRoot)
            {
                t       = this.Forums.GetThread(id.Value);
                t.Views = t.Views + 1;
                this.Db.SubmitChanges();
            }

            if (CurrentUser != null)
            {
                ThreadRead tr = (from thr in this.Db.ThreadReads
                                 where thr.ThreadID == id && thr.UserID == CurrentUser.UserID
                                 select thr).SingleOrDefault();
                if (tr == null)
                {
                    tr = new ThreadRead
                    {
                        ThreadID = t.ThreadID,
                        UserID   = CurrentUser.UserID,
                        DateRead = DateTime.UtcNow,
                    };

                    this.Db.ThreadReads.InsertOnSubmit(tr);
                }
                else
                {
                    tr.DateRead = DateTime.UtcNow;
                }
                this.Db.SubmitChanges();
            }

            var posts     = this.GetPostsInformation(t);
            int postCount = posts.Count();
            int pages     = Pager.PageCount(postCount, pageSize);

            page = Pager.ClampPage(page, pages);

            posts = posts.OrderBy(p => p.Post.CreateDate).Skip((page.Value - 1) * pageSize).Take(pageSize);

            ForumThreadModel tm = new ForumThreadModel
            {
                Thread     = t,
                Posts      = posts.ToList(),
                UserAccess = a,
                PageInfo   = new PaginationInformation
                {
                    Pager          = this.Skins.GetDefaultThreadPager(),
                    CurrentPage    = page,
                    Items          = postCount,
                    ItemsPerPage   = pageSize,
                    ControllerName = "Forums",
                    ActionName     = "ViewThread",
                    PageAttribute  = "page",
                    RouteValues    = new System.Web.Routing.RouteValueDictionary(new { id = id })
                }
            };

            return(View("ViewThread", tm));
        }