From 7e54c9b3fad48c7bce1134f7937924f120f1999c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 May 2017 09:52:57 -0400 Subject: [PATCH] More fixes, specifically with forum --- Project-Unite/ACL.cs | 161 ++++++------------- Project-Unite/Models/AdminViewModels.cs | 21 --- Project-Unite/Models/ForumCategory.cs | 50 +++--- Project-Unite/Models/IdentityModels.cs | 1 - Project-Unite/Views/Forum/Index.cshtml | 28 +++- Project-Unite/Views/Forum/ViewForum.cshtml | 171 +++++---------------- 6 files changed, 136 insertions(+), 296 deletions(-) diff --git a/Project-Unite/ACL.cs b/Project-Unite/ACL.cs index 5155573..74ac07e 100644 --- a/Project-Unite/ACL.cs +++ b/Project-Unite/ACL.cs @@ -224,37 +224,26 @@ namespace Project_Unite public static bool CanSee(string userName, string fId) { - - - if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId)) - return false; - var db = new ApplicationDbContext(); - - var usr = db.Users.Include(x => x.Roles).FirstOrDefault(u => u.UserName == userName); - - var userRoles = new List(); - foreach (var usrRole in usr.Roles) - { - userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role); + var user = db.Users.FirstOrDefault(x => x.UserName == userName); + var frm = db.ForumCategories.FirstOrDefault(x => x.Id == fId); + if (frm == null) return false; + if (user == null) { + return frm.VisibleToGuests; } - db.Dispose(); - var userRole = userRoles.OrderByDescending(m => m.Priority).First(); - - db = new ApplicationDbContext(); - - - - - var forums = db.ForumCategories; - var forum = forums.First(x => x.Id == fId); - var perms = forum.Permissions.FirstOrDefault(x => x.RoleId == userRole.Id); - if (perms == null) + else { - UpdateACLDefinitions(fId); - return true; + if (user.HighestRole.IsAdmin) + return frm.AdminPermission > 0; + if (user.HighestRole.IsDeveloper) + return frm.DeveloperPermission > 0; + if (user.HighestRole.IsModerator) + return frm.ModeratorPermission > 0; + if (user.HighestRole.IsMember) + return frm.MemberPermission > 0; + } - return (int)perms.Permissions >= (int)PermissionPreset.CanRead; + return false; } public static bool UserEmailConfirmed(string username) @@ -278,39 +267,26 @@ namespace Project_Unite public static bool CanReply(string userName, string fId) { - - - if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId)) - return false; - if (HttpContext.Current.User.Identity.IsGuest()) - return false; //obviously if this role has a global restraint for this ACL def we shouldn't let them post in ANY forum. - var db = new ApplicationDbContext(); - - var usr = db.Users.Include(x => x.Roles).FirstOrDefault(u => u.UserName == userName); - - var userRoles = new List(); - foreach (var usrRole in usr.Roles) + var user = db.Users.FirstOrDefault(x => x.UserName == userName); + var frm = db.ForumCategories.FirstOrDefault(x => x.Id == fId); + if (frm == null) return false; + if (user == null) { - userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role); + return false; } - db.Dispose(); - var userRole = userRoles.OrderByDescending(m => m.Priority).First(); - - db = new ApplicationDbContext(); - - - - - var forums = db.ForumCategories; - var forum = forums.First(x => x.Id == fId); - var perms = forum.Permissions.FirstOrDefault(x => x.RoleId == userRole.Id); - if (perms == null) + else { - UpdateACLDefinitions(fId); - return true; + if (user.HighestRole.IsAdmin) + return frm.AdminPermission > 1; + if (user.HighestRole.IsDeveloper) + return frm.DeveloperPermission > 1; + if (user.HighestRole.IsModerator) + return frm.ModeratorPermission > 1; + if (user.HighestRole.IsMember) + return frm.MemberPermission > 1; } - return perms.Permissions >= PermissionPreset.CanReply; + return false; } public static ApplicationUser GetUserInfo(string id) @@ -320,69 +296,26 @@ namespace Project_Unite public static bool CanPost(string userName, string fId) { - - - if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId)) + var db = new ApplicationDbContext(); + var user = db.Users.FirstOrDefault(x => x.UserName == userName); + var frm = db.ForumCategories.FirstOrDefault(x => x.Id == fId); + if (frm == null) return false; + if (user == null) + { return false; - if (HttpContext.Current.User.Identity.IsGuest()) - return false; //obviously if this role has a global restraint for this ACL def we shouldn't let them post in ANY forum. - - var db = new ApplicationDbContext(); - - var usr = db.Users.Include(x => x.Roles).FirstOrDefault(u => u.UserName == userName); - - var userRoles = new List(); - foreach (var usrRole in usr.Roles) - { - userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role); } - db.Dispose(); - var userRole = userRoles.OrderByDescending(m => m.Priority).First(); - - db = new ApplicationDbContext(); - - - - - var forums = db.ForumCategories; - var forum = forums.First(x => x.Id == fId); - var perms = forum.Permissions.FirstOrDefault(x=>x.RoleId==userRole.Id); - if (perms == null) + else { - UpdateACLDefinitions(fId); - return true; + if (user.HighestRole.IsAdmin) + return frm.AdminPermission > 2; + if (user.HighestRole.IsDeveloper) + return frm.DeveloperPermission > 2; + if (user.HighestRole.IsModerator) + return frm.ModeratorPermission > 2; + if (user.HighestRole.IsMember) + return frm.MemberPermission > 2; } - return perms.Permissions >= PermissionPreset.CanPost; - } - - public static void UpdateACLDefinitions(string fid) - { - var db = new ApplicationDbContext(); - var forum = db.ForumCategories.FirstOrDefault(x => x.Id == fid); - if (forum == null) - return; - int recordsAdded = 0; - - if (forum.Permissions.Length < db.Roles.Count()) - { - var roles = db.Roles.ToArray(); - foreach(var role in roles) - { - if (db.ForumPermissions.FirstOrDefault(x => x.CategoryId == fid && x.RoleId == role.Id) == null) - { - var perm = new ForumPermission(); - perm.Id = Guid.NewGuid().ToString(); - perm.CategoryId = forum.Id; - perm.RoleId = role.Id; - perm.Permissions = PermissionPreset.CanPost; - db.ForumPermissions.Add(perm); - recordsAdded++; - } - } - db.AuditLogs.Add(new AuditLog("system", AuditLogLevel.Admin, $"Automatic forum ACL update occurred - Forum: {forum.Name}, records added: {recordsAdded}.")); - db.SaveChanges(); - } - + return false; } public static bool CanManageRole(string userId, string roleId) diff --git a/Project-Unite/Models/AdminViewModels.cs b/Project-Unite/Models/AdminViewModels.cs index 23935d3..a03775e 100644 --- a/Project-Unite/Models/AdminViewModels.cs +++ b/Project-Unite/Models/AdminViewModels.cs @@ -7,27 +7,6 @@ using System.Web.Mvc; namespace Project_Unite.Models { - public class AdminAccessControlViewModel - { - public AdminAccessControlViewModel() - { - - } - - public AdminAccessControlViewModel(Dictionary modelList) - { - ACLList = new List(); - foreach(var v in modelList.Values) - { - ACLList.AddRange(v); - } - IDs = modelList.Keys.ToList(); - } - - public List IDs { get; set; } - public List ACLList { get; set; } - } - public class CreateUserModel { diff --git a/Project-Unite/Models/ForumCategory.cs b/Project-Unite/Models/ForumCategory.cs index da5a9c9..52e184e 100644 --- a/Project-Unite/Models/ForumCategory.cs +++ b/Project-Unite/Models/ForumCategory.cs @@ -39,28 +39,12 @@ namespace Project_Unite.Models public virtual string Parent { get; set; } - public ForumPermission[] Permissions { get - { - var db = new ApplicationDbContext(); - return db.ForumPermissions.Where(x => x.CategoryId == this.Id).ToArray(); - } - } - } + public int AdminPermission { get; set; } + public int DeveloperPermission { get; set; } + public int ModeratorPermission { get; set; } + public int MemberPermission { get; set; } - public class ForumPermission - { - [Key] - public string Id { get; set; } - - [Required] - public string CategoryId { get; set; } - - [Required] - public string RoleId { get; set; } - - [Required] - [EnumDataType(typeof(PermissionPreset))] - public PermissionPreset Permissions { get; set; } + public bool VisibleToGuests { get; set; } } public class ForumPost @@ -124,8 +108,32 @@ namespace Project_Unite.Models public bool IsLocked { get; set; } + public int Priority + { + get + { + int priority = 0; + if (IsSticky) + priority = 1; + if (IsAnnounce) + priority = 2; + if (IsSticky && IsAnnounce) + priority = 3; + return priority; + } + } + public DateTime StartedAt { get; set; } public string Subject { get; set; } + public bool ShouldShow + { + get + { + if (IsUnlisted == true) + return HttpContext.Current.User?.Identity?.IsModerator() == true; + return true; + } + } public string AuthorId { get; set; } public bool IsSticky { get; set; } public bool IsAnnounce { get; set; } diff --git a/Project-Unite/Models/IdentityModels.cs b/Project-Unite/Models/IdentityModels.cs index 46c8e56..a16dfe7 100644 --- a/Project-Unite/Models/IdentityModels.cs +++ b/Project-Unite/Models/IdentityModels.cs @@ -241,7 +241,6 @@ namespace Project_Unite.Models public DbSet UserPosts { get; set; } public DbSet ForumPostEdits { get; set; } public DbSet Likes { get; set; } - public DbSet ForumPermissions { get; set; } public DbSet BannedIPs { get; set; } public DbSet AuditLogs { get; set; } public System.Data.Entity.DbSet IdentityRoles { get; set; } diff --git a/Project-Unite/Views/Forum/Index.cshtml b/Project-Unite/Views/Forum/Index.cshtml index 8e26c24..b579eeb 100644 --- a/Project-Unite/Views/Forum/Index.cshtml +++ b/Project-Unite/Views/Forum/Index.cshtml @@ -2,12 +2,16 @@ @{ ViewBag.Title = "Forums"; + bool noForums = true; }

Welcome to the forums.

@foreach (var cat in Model) { + if (ACL.CanSee(User?.Identity?.Name, cat.Id)) + { + noForums = false; @@ -16,16 +20,21 @@ @foreach (var subcat in cat.Children) - { + { + if (ACL.CanSee(User?.Identity?.Name, subcat.Id)) + { - } + } + }
@cat.NameMost Recent Post
@Html.ActionLink(subcat.Name, "ViewForum", "Forum", new { id = subcat.Id }, null)

@subcat.Description

- @if(subcat.Children.Length > 0) + @if (subcat.Children.Length > 0) {

Subforums: - @foreach(var subfrm in subcat.Children) + @foreach (var subfrm in subcat.Children) { + if (ACL.CanSee(User?.Identity?.Name, subfrm.Id)) + { @Html.ActionLink(subfrm.Name, "ViewForum", "Forum", new { id = subfrm.Id }, null)  + } }

} @@ -34,6 +43,17 @@
+ } +} + +@if(noForums == true) +{ +
+
+

No forums to show! There are no forums that you have read access to. If you are a guest, please @Html.ActionLink("log in", "Login", "Account"). If not, please contact an admin.

+
+
} \ No newline at end of file diff --git a/Project-Unite/Views/Forum/ViewForum.cshtml b/Project-Unite/Views/Forum/ViewForum.cshtml index 01f1d01..e4b528f 100644 --- a/Project-Unite/Views/Forum/ViewForum.cshtml +++ b/Project-Unite/Views/Forum/ViewForum.cshtml @@ -5,160 +5,61 @@

@Model.Name

-@if (Model.Parent == "root") -{ - @Html.ActionLink("Back", "Index", "Forum", null, new { @class = "btn btn-default" }) - -} -else -{ - @Html.ActionLink("Back", "ViewForum", "Forum", new { id=Model.Parent}, new { @class = "btn btn-default" }) -} - +

@Model.Description

@if (Model.Children.Length > 0) { - - - - - - - - @foreach (var cat in Model.Children) +
+
Subforums
+
Topics
+
Posts
+
Most Recent Post
+
+ foreach (var cat in Model.Children) { - - - - - - + +
+
+ } -
SubforumsTopicsPostsMost Recent Post
@Html.ActionLink(cat.Name, "ViewForum", "Forum", new { id = cat.Id }, null) +
+
@Html.ActionLink(cat.Name, "ViewForum", "Forum", new { id = cat.Id }, null)

@cat.Description

-
+ +
@cat.Topics.Length -
}
@if (Model.Topics.Length > 0) { - - - - - - - @{ - var stickies = Model.Topics.Where(x => x.IsSticky == true); - var stickiesSorted = stickies.OrderByDescending(x => x.StartedAt); - var topicsSorted = Model.Topics.Where(x => x.IsSticky != true).OrderByDescending(x => x.StartedAt); - } - - @foreach (var topic in stickiesSorted) - { - bool showTopic = true; - if (topic.IsUnlisted == true) +
+
Topics
+
Posts
+
Most recent post
+
+ var topics = Model.Topics.Where(x => x.ShouldShow == true).OrderByDescending(x=>x.StartedAt).ThenByDescending(x=>x.Priority); + foreach(var topic in topics) { - if(!ACL.Granted(User.Identity.Name, "CanSeeUnlistedTopics")) - { - showTopic = false; - } +
+
+ @Html.ActionLink(topic.Subject, "ViewTopic", "Forum", new { id = topic.Discriminator }, null) +
+

Started by @Html.UserLink(topic.AuthorId) at @topic.StartedAt • @topic.Likes.Length • @topic.Dislikes.Length

+
+
+ @topic.Posts.Length +
+
} - - if (showTopic == true) - { - - - - - - } - } - @foreach (var topic in topicsSorted) - { - - bool showTopic = true; - if (topic.IsUnlisted == true) - { - if (!ACL.Granted(User.Identity.Name, "CanSeeUnlistedTopics")) - { - showTopic = false; - } - } - - if (showTopic == true) - { - - - - - - } - } - - -
TopicsPostsMost recent post
  - @if(topic.IsUnlisted == true) - { - - } - @if (topic.IsLocked == true) - { - - } - - @Html.ActionLink(topic.Subject, "ViewTopic", "Forum", new { id = topic.Discriminator }, null) -

Started by @Html.UserLink(topic.AuthorId) on @topic.StartedAt

-
- @topic.Posts.Length - - @if (topic.Posts.Length > 0) - { - var mostRecent = topic.Posts.OrderByDescending(x => x.PostedAt).First(); - Re: @topic.Subject - by @Html.UserLink(mostRecent.AuthorId) -

at @mostRecent.PostedAt

- } - else - { - No posts. - } -
- @if(topic.IsUnlisted == true) - { - - } - - @if (topic.IsLocked == true) - { - - } - @Html.ActionLink(topic.Subject, "ViewTopic", "Forum", new { id = topic.Discriminator }, null) -

Started by @Html.UserLink(topic.AuthorId) on @topic.StartedAt

-
- @topic.Posts.Length - - @if (topic.Posts.Length > 0) - { - var mostRecent = topic.Posts.OrderByDescending(x => x.PostedAt).First(); - Re: @topic.Subject - by @Html.UserLink(mostRecent.AuthorId) -

at @mostRecent.PostedAt

- } - else - { - No posts. - } -
} else {