diff options
| author | Michael <[email protected]> | 2017-05-17 09:52:57 -0400 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-05-17 09:52:57 -0400 |
| commit | 7e54c9b3fad48c7bce1134f7937924f120f1999c (patch) | |
| tree | f922e35184e7e9f0cc90d0dc11f4541a414d35bd /Project-Unite/ACL.cs | |
| parent | 1abd027bdbd55fac59ad5bcdd8df301bb2499204 (diff) | |
| download | project-unite-7e54c9b3fad48c7bce1134f7937924f120f1999c.tar.gz project-unite-7e54c9b3fad48c7bce1134f7937924f120f1999c.tar.bz2 project-unite-7e54c9b3fad48c7bce1134f7937924f120f1999c.zip | |
More fixes, specifically with forum
Diffstat (limited to 'Project-Unite/ACL.cs')
| -rw-r--r-- | Project-Unite/ACL.cs | 159 |
1 files changed, 46 insertions, 113 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<Role>(); - 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<Role>(); - 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)) - 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<Role>(); - 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) + 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) { - UpdateACLDefinitions(fId); - return true; + return false; } - 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()) + else { - 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(); + 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 false; } public static bool CanManageRole(string userId, string roleId) |
