public static bool CheckCategoryPermissions(this ForumRespository db, Forum_Category Category, Forum_User User, Func<Forum_Permission, bool> PermissionCheck) { if (Identity.IsAdmin) return true; // Since Administrators can change rules, they might as well ignore them. var Permissions = db.GetPermissions(Category, User); foreach (var P in Permissions) { if (PermissionCheck(P)) return true; } return false; }
public void DeleteCategory(Forum_Category ToDelete) { db.Forum_Categories.DeleteOnSubmit(ToDelete); }
public void AddCategory(Forum_Category ToAdd) { db.Forum_Categories.InsertOnSubmit(ToAdd); }
static void FillCategoryIDListRecursively(List<int> ListToFill, Forum_Category InitialCategory) { ListToFill.Add(InitialCategory.CategoryID); if (!InitialCategory.InheritPermissions) return; FillCategoryIDListRecursively(ListToFill, InitialCategory.Category1); }
public IEnumerable<Forum_Permission> GetPermissions(Forum_Category Category, Forum_User User) { if (!PermissionCache.ContainsKey(Category.CategoryID)) { var CategoryIDList = new List<int>(); FillCategoryIDListRecursively(CategoryIDList, Category); var Roles = GetUserRoles(User).Select(R => R.RoleID); PermissionCache.Add(Category.CategoryID, from L in db.Forum_PermissionsLinks where CategoryIDList.Contains(L.CategoryID) && Roles.Contains(L.RoleID) join P in db.Forum_Permissions on L.PermissionID equals P.PermissionID select P); } return PermissionCache[Category.CategoryID]; }
public IQueryable<Forum_PermissionsLink> GetPermissionLinks(Forum_Category Category) { var CategoryIDList = new List<int>(); FillCategoryIDListRecursively(CategoryIDList, Category); return from L in db.Forum_PermissionsLinks where CategoryIDList.Contains(L.CategoryID) select L; }
AdminCategory RecursivelyFillCategoryTree(ForumRespository db, Forum_Category ToFill, AdminCategoryModel model = null, int ID = 0) { AdminCategory newCategory = new AdminCategory() { AllowPosts = ToFill.AllowPosts, id = ToFill.CategoryID, InheritPermissions = ToFill.InheritPermissions, Name = ToFill.Name, Priority = ToFill.Priority }; if (newCategory.id == ID) model.CurrentCategory = newCategory; var SortedCategories = db.GetSortedCategories(ToFill.CategoryID); if (!ToFill.AllowPosts) SortedCategories = SortedCategories.OrderByDescending(C => C.AllowPosts); foreach (var Child in SortedCategories) newCategory.Children.Add(RecursivelyFillCategoryTree(db, Child, model, ID)); return newCategory; }
public ActionResult Category(int id, string NewCategory, string UpdateCategory, string MoveCategory) { var model = new AdminCategoryModel(); model.AddNavigation("Admin Panel", "Overview", "Admin", null); model.AddNavigation("Edit Category"); using (ForumRespository db = new ForumRespository()) { Forum_User CurrentUser = GetCurrentUser(db); if (!UserIdentity.IsAdmin) return AuthenticationHelper.AccessDeniedView(model); // Administrating the forum requires the user to be an Admin. HandlePermissionsLinkUpdates(); Forum_Category Root = db.GetCategoryByID((int)BuildInCategory.Root); Forum_Category CurrentCategory = id != 0 ? db.GetCategoryByID(id) : Root; if (CurrentCategory == null) return NotFoundView("Category"); bool IsRoot = Root == CurrentCategory; if (IsHttpPost && AntiForgeryTokenValid) { if (!String.IsNullOrEmpty(NewCategory)) { Forum_Category NewForumCategory = new Forum_Category(); NewForumCategory.ParentID = CurrentCategory.CategoryID; NewForumCategory.Name = "Untitled Category"; NewForumCategory.InheritPermissions = true; NewForumCategory.AllowPosts = false; db.AddCategory(NewForumCategory); db.Save(); return RedirectToAction("Category", new { id = NewForumCategory.CategoryID }); } if (!String.IsNullOrEmpty(UpdateCategory) && CurrentCategory != Root) { var Form = Request.Form; string NewName = Form["CurrentCategory.Name"]; bool InheritPermissions = !String.IsNullOrEmpty(Form["Inherit Permissions"]); bool AllowPosts = !String.IsNullOrEmpty(Form["Allow Posts"]); CurrentCategory.AllowPosts = AllowPosts; CurrentCategory.InheritPermissions = InheritPermissions; try { CurrentCategory.Priority = Convert.ToInt32(Form["CurrentCategory.Priority"]); } catch { } if (!String.IsNullOrWhiteSpace(NewName)) CurrentCategory.Name = NewName.Trim(); db.Save(); } if (!String.IsNullOrEmpty(MoveCategory) && CurrentCategory != Root) { var Form = Request.Form; int DestinationID = 0; try { DestinationID = Convert.ToInt32(Form["MoveToDestination"]); } catch {} var Parent = db.GetCategoryByID(DestinationID); if (Parent != null) { while (Parent != null) { if (Parent == CurrentCategory) break; Parent = Parent.Category1; } if (Parent == null) { CurrentCategory.ParentID = DestinationID; db.Save(); } } } } foreach (var Category in db.GetAllCategories()) { var Parent = Category; while (Parent != null) { if (Parent == CurrentCategory) break; Parent = Parent.Category1; } if (Parent != null) continue; if (Category == CurrentCategory.Category1) continue; model.MoveCategoryToOptions.Add(new AdminNamedID() { Name = Category.Name, ID = Category.CategoryID }); } model.Root = RecursivelyFillCategoryTree(db, Root, model, CurrentCategory.CategoryID); model.UserGroups = db.GetAllRoles().Where(R => R.RoleID != (int)BuildInRole.Administrator).ToClassList(R => new AdminNamedID() { ID = R.RoleID, Name = R.Name }); model.PermissionSets = db.GetAllPermissionSets().ToClassList(P => new AdminNamedID() { ID = P.PermissionID, Name = P.Name }); model.Fixed = AdminPermissionLinkEditors.FixedSet.Categories; model.FixedNamedID = new AdminNamedID() { ID = model.CurrentCategory.id, Name = model.CurrentCategory.Name }; model.PermissionLinkList = db.GetPermissionLinks(CurrentCategory).OrderBy(L => L.CategoryID).ToClassList(L => new AdminPermissionLink() { Category = new AdminNamedID() { ID = L.CategoryID, Name = db.GetCategoryByID(L.CategoryID).Name }, UserGroup = new AdminNamedID() { ID = L.RoleID, Name = db.GetRole(L.RoleID).Name }, PermissionSet = new AdminNamedID() { ID = L.PermissionID, Name = db.GetPermissionSetByID(L.PermissionID).Name }, }); return View(model); } }
partial void DeleteForum_Category(Forum_Category instance);
partial void UpdateForum_Category(Forum_Category instance);
partial void InsertForum_Category(Forum_Category instance);
private void detach_Forum_Categories(Forum_Category entity) { this.SendPropertyChanging(); entity.Category1 = null; }