diff options
| author | Michael <[email protected]> | 2017-05-09 16:00:37 -0400 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-05-09 16:00:37 -0400 |
| commit | c84f6193547370d6786609404e6dbc51f181d7b0 (patch) | |
| tree | b3b9cad1beadddbfe2de9f736424638275691c91 /Project-Unite/ACL.cs | |
| parent | cb653f021d2567f69a63414963b0f5ef739cd641 (diff) | |
| download | project-unite-c84f6193547370d6786609404e6dbc51f181d7b0.tar.gz project-unite-c84f6193547370d6786609404e6dbc51f181d7b0.tar.bz2 project-unite-c84f6193547370d6786609404e6dbc51f181d7b0.zip | |
Redo the permission system.
Diffstat (limited to 'Project-Unite/ACL.cs')
| -rw-r--r-- | Project-Unite/ACL.cs | 63 |
1 files changed, 30 insertions, 33 deletions
diff --git a/Project-Unite/ACL.cs b/Project-Unite/ACL.cs index ce3bb15..ed22e2a 100644 --- a/Project-Unite/ACL.cs +++ b/Project-Unite/ACL.cs @@ -9,6 +9,8 @@ using System.Diagnostics; using System.Web.Mvc.Html; using System.Data.Entity; using System.Text; +using Microsoft.AspNet.Identity.EntityFramework; +using System.Security.Principal; namespace Project_Unite { @@ -226,9 +228,7 @@ namespace Project_Unite if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId)) return false; - if (!Granted(userName, "CanPostTopics")) - 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); @@ -282,7 +282,7 @@ namespace Project_Unite if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId)) return false; - if (!Granted(userName, "CanPostTopics")) + 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(); @@ -324,7 +324,7 @@ namespace Project_Unite if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId)) return false; - if (!Granted(userName, "CanPostTopics")) + 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(); @@ -389,7 +389,7 @@ namespace Project_Unite { try { - if (!Granted(userId, "CanEditRoles")) + if (!HttpContext.Current.User.Identity.IsAdmin()) return false; var db = new ApplicationDbContext(); @@ -425,40 +425,37 @@ namespace Project_Unite } - public static bool Granted(string userName, string prop) + public static bool IsGuest(this IIdentity id) { - if (string.IsNullOrWhiteSpace(prop)) - return true; + if (HttpContext.Current.Request.IsAuthenticated) + return false; + return true; + } - try - { - var db = new ApplicationDbContext(); - var usr = db.Users.FirstOrDefault(u => u.UserName == userName); + public static bool IsModerator(this IIdentity id) + { + var db = new ApplicationDbContext(); + return db.Users.FirstOrDefault(x => x.UserName == id.Name).HighestRole.IsModerator; + } - 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(); + public static bool IsDeveloper(this IIdentity id) + { + var db = new ApplicationDbContext(); + return db.Users.FirstOrDefault(x => x.UserName == id.Name).HighestRole.IsDeveloper; + } - var t = userRole.GetType(); - foreach (var propInf in t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) - { - if (propInf.Name == prop && propInf.PropertyType == typeof(bool)) - return (bool)propInf.GetValue(userRole); - } + public static bool IsMember(this IIdentity id) + { + var db = new ApplicationDbContext(); + return db.Users.FirstOrDefault(x => x.UserName == id.Name).HighestRole.IsMember; + } - return false; - } - catch (Exception ex) - { - Debug.Print(ex.ToString()); - return false; - } + public static bool IsAdmin(this IIdentity id) + { + var db = new ApplicationDbContext(); + return db.Users.FirstOrDefault(x => x.UserName == id.Name).HighestRole.IsAdmin; } } }
\ No newline at end of file |
