Skip to content

Instantly share code, notes, and snippets.

@abelal83
Created January 7, 2018 22:20
Show Gist options
  • Save abelal83/1321323f3264dead57cc91fd15f933ae to your computer and use it in GitHub Desktop.
Save abelal83/1321323f3264dead57cc91fd15f933ae to your computer and use it in GitHub Desktop.
Serene permission service modification to allow query against AD
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