Created
January 7, 2018 22:20
-
-
Save abelal83/1321323f3264dead57cc91fd15f933ae to your computer and use it in GitHub Desktop.
Serene permission service modification to allow query against AD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Serene.Administration | |
{ | |
using Serene.Administration.Entities; | |
using Serenity; | |
using Serenity.Abstractions; | |
using Serenity.Data; | |
using System; | |
using System.Collections.Generic; | |
using System.Web.Security; // added by abelal to support AD role based authentication | |
public class PermissionService : IPermissionService | |
{ | |
public bool HasPermission(string permission) | |
{ | |
if (Authorization.Username == "admin") | |
return true; | |
string[] permissionItems = permission.Split(','); | |
//bool hasPermission = false; | |
foreach (string permissionItem in permissionItems) | |
{ | |
if (Roles.IsUserInRole(permissionItem.Trim())) | |
return true; | |
if (Authorization.Username == permissionItem.Trim()) | |
return true; | |
} | |
return false; | |
} | |
//private Dictionary<string, bool> GetUserPermissions(int userId) | |
//{ | |
// var fld = UserPermissionRow.Fields; | |
// return TwoLevelCache.GetLocalStoreOnly("UserPermissions:" + userId, TimeSpan.Zero, fld.GenerationKey, () => | |
// { | |
// using (var connection = SqlConnections.NewByKey("Default")) | |
// { | |
// var result = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase); | |
// connection.List<UserPermissionRow>(q => q | |
// .Select(fld.PermissionKey) | |
// .Select(fld.Granted) | |
// .Where(new Criteria(fld.UserId) == userId)) | |
// .ForEach(x => result[x.PermissionKey] = x.Granted ?? true); | |
// return result; | |
// } | |
// }); | |
//} | |
//private HashSet<string> GetRolePermissions(int userId) | |
//{ | |
// var fld = RolePermissionRow.Fields; | |
// return TwoLevelCache.GetLocalStoreOnly("RolePermissions:" + userId, TimeSpan.Zero, fld.GenerationKey, () => | |
// { | |
// using (var connection = SqlConnections.NewByKey("Default")) | |
// { | |
// var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | |
// connection.List<RolePermissionRow>(q => q | |
// .Select(fld.PermissionKey) | |
// .Where(new Criteria(fld.RoleId) == userId)) | |
// .ForEach(x => result.Add(x.PermissionKey)); | |
// return result; | |
// } | |
// }); | |
//} | |
//private HashSet<int> GetUserRoles(int userId) | |
//{ | |
// var fld = UserRoleRow.Fields; | |
// return TwoLevelCache.GetLocalStoreOnly("UserRoles:" + userId, TimeSpan.Zero, fld.GenerationKey, () => | |
// { | |
// using (var connection = SqlConnections.NewByKey("Default")) | |
// { | |
// var result = new HashSet<int>(); | |
// connection.List<UserRoleRow>(q => q | |
// .Select(fld.RoleId) | |
// .Where(new Criteria(fld.UserId) == userId)) | |
// .ForEach(x => result.Add(x.RoleId.Value)); | |
// return result; | |
// } | |
// }); | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment