2

Is it possible to make a custom QueryInterceptor in a WCF Data Service on all entites instead of only one? This is a standard QueryInterceptor:

[QueryInterceptor("Products")]
public Expression<Func<Product, bool>> OnQueryProducts()
{
    var user = HttpContext.Current.User;
    if (user.IsInRole("Administrator"))
        return (Product p) => true;
    else
        return (Product p) => false;
}

I like to do something like this:

[QueryInterceptor("*")]
public Expression<Func<Object, bool>> OnQueryProducts()
{
    var user = HttpContext.Current.User;
    if (user.IsInRole("Administrator"))
        return (Object p) => true;
    else
        return (Object p) => false;
}

Is there any way or do I have to integrate one inceptor for all of my entities?

Jan Hommes
  • 5,122
  • 4
  • 33
  • 45
  • Its possible but you need to create your own Interceptor logic that works with Web API. Overriding the default behavour...don't have time to do it right now... – Aron Dec 02 '14 at 11:02

1 Answers1

2

Unfortunately you can't use a wild card with the QueryInterceptor however you can achieve the same result as in your example by overriding the OnStartProcessingRequest method of the DataService and checking the role of the user there.

protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
    var user = HttpContext.Current.User;

    // Only Administrator users are allowed access
    if (!user.IsInRole("Administrator"))
    {
        // Any other role throws a security exception
        throw new SecurityException("You do not have permission to access this Service");
    }

    base.OnStartProcessingRequest(args);
}
Adam Cooper
  • 8,077
  • 2
  • 33
  • 51
  • Unfortunately I need some more logic (tenant handling). But I am using a query interceptor for each entity now. As you said, it is not possible at the moment. So I accepted your answer. – Jan Hommes Dec 02 '14 at 10:30