In my VS LIGHTSWITCH 2013 portal application I allow the user to create tiles that link to other internal apps. When a new tile is create a Role with the name 'TileTitle + " User"' is created. This way I can show and hide tiles based on a users roles. However when trying to filter the tile entities in the query filter method I get some error about not being able to use the IsInRole method.
After some digging I decided to give expression trees a try, and this is what I came up with:
partial void TileLinks_Filter(ref Expression<Func<TileLink, bool>> filter)
{
ParameterExpression p = Expression.Parameter(typeof(TileLink), "e");
MemberExpression ti = Expression.Property(p, "Title");
MethodInfo m2 = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) });
Type t = this.Application.User.GetType();
MethodInfo m = t.GetMethod("IsInRole");
Expression filterExpression = Expression.IsTrue(Expression.Call(Expression.Call(m2, ti, Expression.Constant(" User")), m));
filter = Expression.Lambda<Func<TileLink, bool>>(filterExpression, p);
// e => this.Application.User.IsInRole(e.Title + " User");
// this is what I would like to do
}
This however does not work, and I am left with this very odd error message.
Method 'Boolean IsInRole(System.String)' declared on type 'System.Security.Claims.ClaimsPrincipal' cannot be called with instance of type 'System.String'
Please help me filter my data based on dynamically generated roles!